-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmd-parser.js
134 lines (119 loc) · 3.67 KB
/
md-parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const It = require('markdown-it')
const hljs = require('highlight.js')
const fs = require('fs')
const path = require('path')
const yaml = require('js-yaml')
const config = yaml.load(
fs.readFileSync(path.resolve(global.ConfigDir, 'parser.module.yaml'), {
encoding: 'utf8',
})
)
// const unescapeAll = require('markdown-it/common/utils')
let renderFunction = {}
let it = It({
highlight: function (str, lang, env) {
if (lang) {
let renderMatch = lang.match(/^render\((.+)\)/)
if (!!renderMatch && !!renderFunction[renderMatch[1]]) {
return `<pre class="render"></pre>${renderFunction[
renderMatch[1]
](str, env)}<pre class="render"></pre>`
}
if (hljs.getLanguage(lang)) {
try {
return `<pre class="hljs"><code class="hljs language-${lang}">${
hljs.highlight(lang, str).value
}</code></pre>`
} catch (__) {}
}
}
return `<pre class="hljs"><code>${it.utils.escapeHtml(
str
)}</code></pre>`
},
})
it.staticPaths = []
it['style-url'] = []
it['script-url'] = []
it['offline-style-url'] = []
it['offline-script-url'] = []
it.renderer.rules.fence = function (tokens, idx, options, env, slf) {
var token = tokens[idx],
info = token.info ? it.utils.unescapeAll(token.info).trim() : '',
langName = '',
highlighted,
i,
tmpAttrs,
tmpToken
if (info) {
langName = info.split(/\s+/g)[0]
}
if (options.highlight) {
highlighted =
options.highlight(token.content, langName, env) ||
escapeHtml(token.content)
} else {
highlighted = escapeHtml(token.content)
}
if (highlighted.indexOf('<pre') === 0) {
return highlighted + '\n'
}
// If language exists, inject class gently, without modifying original token.
// May be, one day we will add .clone() for token and simplify this part, but
// now we prefer to keep things local.
if (info) {
i = token.attrIndex('class')
tmpAttrs = token.attrs ? token.attrs.slice() : []
if (i < 0) {
tmpAttrs.push(['class', options.langPrefix + langName])
} else {
tmpAttrs[i][1] += ' ' + options.langPrefix + langName
}
// Fake token just to render attributes
tmpToken = {
attrs: tmpAttrs,
}
return (
'<pre><code' +
slf.renderAttrs(tmpToken) +
'>' +
highlighted +
'</code></pre>\n'
)
}
return (
'<pre><code' +
slf.renderAttrs(token) +
'>' +
highlighted +
'</code></pre>\n'
)
}
for (let c of config) {
if (!!c.module) {
if (c.type === 'render') {
renderFunction[c.name] = require(c.module)
} else {
it.use(require(c.module))
}
}
if (!!c.static && c.static.length != 0) {
for (let s of c.static) {
if (fs.existsSync(path.resolve('md-plugin-file', s))) {
it.staticPaths.push([s, path.resolve('md-plugin-file', s)])
}
}
}
it['style-url'].push(...(c['style-url'] || []))
it['script-url'].push(...(c['script-url'] || []))
it['offline-style-url'].push(...(c['offline-style-url'] || []))
it['offline-script-url'].push(...(c['offline-script-url'] || []))
}
it.renderReset = () => {
for (let f in renderFunction) {
if (typeof renderFunction[f].reset === 'function') {
renderFunction[f].reset()
}
}
}
module.exports = it