UNPKG

4.42 kBJavaScriptView Raw
1import markdownIt from 'markdown-it'
2import emoji from 'markdown-it-emoji'
3import subscript from 'markdown-it-sub'
4import superscript from 'markdown-it-sup'
5import footnote from 'markdown-it-footnote'
6import deflist from 'markdown-it-deflist'
7import abbreviation from 'markdown-it-abbr'
8import insert from 'markdown-it-ins'
9import mark from 'markdown-it-mark'
10import toc from 'markdown-it-toc-and-anchor'
11import katex from 'markdown-it-katex-newcommand'
12import tasklists from 'markdown-it-task-lists'
13import container from 'markdown-it-container'
14import prism from 'markdown-it-prism'
15import preWrapper from '@mathssyfy/markdown-it-prewrapper'
16import anchorPlugin from 'markdown-it-anchor'
17import tocPlugin from 'markdown-it-table-of-contents'
18
19//tests
20import lineNumbers from './lineNumbers' //Probleme avec babel
21import hightlightLines from './hightlightLines'//Meme problème
22
23///
24
25import './scss/theme.scss'
26import { propsMakrdown } from './propsMakrdown';
27
28
29export default {
30 md: new markdownIt(),
31
32 template: '<div><slot></slot></div>',
33
34 data() {
35 return {
36 sourceData: this.source,
37 }
38 },
39
40 props: propsMakrdown,
41
42 computed: {
43 tocLastLevelComputed() {
44 return this.tocLastLevel > this.tocFirstLevel ? this.tocLastLevel : this.tocFirstLevel + 1
45 }
46 },
47
48 render(createElement) {
49 this.md = new markdownIt()
50 this.md.set({
51 html: this.html,
52 xhtmlOut: this.xhtmlOut,
53 breaks: this.breaks,
54 linkify: this.linkify,
55 typographer: this.typographer,
56 langPrefix: this.langPrefix,
57 quotes: this.quotes,
58 })
59 mdAnchor();
60
61 ifToc();
62
63 let outHtml = this.show ?
64 this.md.render(
65 this.prerender(this.sourceData)
66 ) : ''
67 outHtml = this.postrender(outHtml);
68
69 this.$emit('rendered', outHtml)
70 return createElement(
71 'div', {
72 domProps: {
73 innerHTML: outHtml,
74 },
75 },
76 )
77 },
78
79 beforeMount() {
80 if (this.$slots.default) {
81 this.sourceData = ''
82 for (let slot of this.$slots.default) {
83 this.sourceData += slot.text
84 }
85 }
86
87 this.$watch('source', () => {
88 this.sourceData = this.prerender(this.source)
89 this.$forceUpdate()
90 })
91
92 this.watches.forEach((v) => {
93 this.$watch(v, () => {
94 this.$forceUpdate()
95 })
96 })
97 },
98}
99
100function mdAnchor() {
101 this.md.renderer.rules.table_open = () => `<table class="${this.tableClass}">\n`;
102 let defaultLinkRenderer = this.md.renderer.rules.link_open ||
103 function (tokens, idx, options, env, self) {
104 return self.renderToken(tokens, idx, options);
105 };
106 this.md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
107 Object.keys(this.anchorAttributes).map((attribute) => {
108 let aIndex = tokens[idx].attrIndex(attribute);
109 let value = this.anchorAttributes[attribute];
110 if (aIndex < 0) {
111 tokens[idx].attrPush([attribute, value]); // add new attribute
112 }
113 else {
114 tokens[idx].attrs[aIndex][1] = value;
115 }
116 });
117 return defaultLinkRenderer(tokens, idx, options, env, self);
118 };
119}
120
121function ifToc() {
122 if (this.toc) {
123 this.md.use(toc, {
124 tocClassName: this.tocClass,
125 tocFirstLevel: this.tocFirstLevel,
126 tocLastLevel: this.tocLastLevelComputed,
127 anchorLink: this.tocAnchorLink,
128 anchorLinkSymbol: this.tocAnchorLinkSymbol,
129 anchorLinkSpace: this.tocAnchorLinkSpace,
130 anchorClassName: this.tocAnchorClass,
131 anchorLinkSymbolClassName: this.tocAnchorLinkClass,
132 tocCallback: (tocMarkdown, tocArray, tocHtml) => {
133 if (tocHtml) {
134 if (this.tocId && document.getElementById(this.tocId)) {
135 document.getElementById(this.tocId).innerHTML = tocHtml;
136 }
137 this.$emit('toc-rendered', tocHtml);
138 }
139 },
140 });
141 }
142}
143
144// makdown-it-container
145function createContainer (klass, defaultTitle) {
146 return [container, klass, {
147 render (tokens, idx) {
148 const token = tokens[idx]
149 const info = token.info.trim().slice(klass.length).trim()
150 if (token.nesting === 1) {
151 return `<div class="${klass} custom-block"><p class="custom-block-title">${info || defaultTitle}</p>\n`
152 } else {
153 return `</div>\n`
154 }
155 }
156 }]
157}
\No newline at end of file