forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.js
251 lines (205 loc) Β· 6.06 KB
/
compiler.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import marked from 'marked'
import Prism from 'prismjs'
import { helper as helperTpl, tree as treeTpl } from './tpl'
import { genTree } from './gen-tree'
import { slugify } from './slugify'
import { emojify } from './emojify'
import { isAbsolutePath, getPath } from '../router/util'
import { isFn, merge, cached } from '../util/core'
const cachedLinks = {}
function getAndRemoveConfig (str = '') {
const config = {}
if (str) {
str = str
.replace(/:([\w-]+)=?([\w-]+)?/g, (m, key, value) => {
config[key] = value || true
return ''
})
.trim()
}
return { str, config }
}
export class Compiler {
constructor (config, router) {
this.config = config
this.router = router
this.cacheTree = {}
this.toc = []
this.linkTarget = config.externalLinkTarget || '_blank'
this.contentBase = router.getBasePath()
const renderer = this._initRenderer()
let compile
const mdConf = config.markdown || {}
if (isFn(mdConf)) {
compile = mdConf(marked, renderer)
} else {
marked.setOptions(
merge(mdConf, {
renderer: merge(renderer, mdConf.renderer)
})
)
compile = marked
}
this.compile = cached(text => {
let html = ''
if (!text) return text
html = compile(text)
html = config.noEmoji ? html : emojify(html)
slugify.clear()
return html
})
}
matchNotCompileLink (link) {
const links = this.config.noCompileLinks || []
for (var i = 0; i < links.length; i++) {
const n = links[i]
const re = cachedLinks[n] || (cachedLinks[n] = new RegExp(`^${n}$`))
if (re.test(link)) {
return link
}
}
}
_initRenderer () {
const renderer = new marked.Renderer()
const { linkTarget, router, contentBase } = this
const _self = this
const origin = {}
/**
* render anchor tag
* @link https://github.com/chjj/marked#overriding-renderer-methods
*/
origin.heading = renderer.heading = function (text, level) {
const nextToc = { level, title: text }
if (/{docsify-ignore}/g.test(text)) {
text = text.replace('{docsify-ignore}', '')
nextToc.title = text
nextToc.ignoreSubHeading = true
}
if (/{docsify-ignore-all}/g.test(text)) {
text = text.replace('{docsify-ignore-all}', '')
nextToc.title = text
nextToc.ignoreAllSubs = true
}
const slug = slugify(text)
const url = router.toURL(router.getCurrentPath(), { id: slug })
nextToc.slug = url
_self.toc.push(nextToc)
return `<h${level} id="${slug}"><a href="${url}" data-id="${slug}" class="anchor"><span>${text}</span></a></h${level}>`
}
// highlight code
origin.code = renderer.code = function (code, lang = '') {
const hl = Prism.highlight(
code,
Prism.languages[lang] || Prism.languages.markup
)
return `<pre v-pre data-lang="${lang}"><code class="lang-${lang}">${hl}</code></pre>`
}
origin.link = renderer.link = function (href, title = '', text) {
let attrs = ''
const { str, config } = getAndRemoveConfig(title)
title = str
if (
!/:|(\/{2})/.test(href) &&
!_self.matchNotCompileLink(href) &&
!config.ignore
) {
href = router.toURL(href, null, router.getCurrentPath())
} else {
attrs += ` target="${linkTarget}"`
}
if (config.target) {
attrs += ' target=' + config.target
}
if (config.disabled) {
attrs += ' disabled'
href = 'javascript:void(0)'
}
if (title) {
attrs += ` title="${title}"`
}
return `<a href="${href}"${attrs}>${text}</a>`
}
origin.paragraph = renderer.paragraph = function (text) {
if (/^!>/.test(text)) {
return helperTpl('tip', text)
} else if (/^\?>/.test(text)) {
return helperTpl('warn', text)
}
return `<p>${text}</p>`
}
origin.image = renderer.image = function (href, title, text) {
let url = href
let attrs = ''
const { str, config } = getAndRemoveConfig(title)
title = str
if (config['no-zoom']) {
attrs += ' data-no-zoom'
}
if (title) {
attrs += ` title="${title}"`
}
if (!isAbsolutePath(href)) {
url = getPath(contentBase, href)
}
return `<img src="${url}"data-origin="${href}" alt="${text}"${attrs}>`
}
const CHECKED_RE = /^\[([ x])\] +/
origin.listitem = renderer.listitem = function (text) {
const checked = CHECKED_RE.exec(text)
if (checked) {
text = text.replace(CHECKED_RE, `<input type="checkbox" ${checked[1] === 'x' ? 'checked' : ''} />`)
}
return `<li${checked ? ` class="task-list-item"` : ''}>${text}</li>\n`
}
renderer.origin = origin
return renderer
}
/**
* Compile sidebar
*/
sidebar (text, level) {
const currentPath = this.router.getCurrentPath()
let html = ''
if (text) {
html = this.compile(text)
html = html && html.match(/<ul[^>]*>([\s\S]+)<\/ul>/g)[0]
} else {
const tree = this.cacheTree[currentPath] || genTree(this.toc, level)
html = treeTpl(tree, '<ul>')
this.cacheTree[currentPath] = tree
}
return html
}
/**
* Compile sub sidebar
*/
subSidebar (level) {
if (!level) {
this.toc = []
return
}
const currentPath = this.router.getCurrentPath()
const { cacheTree, toc } = this
toc[0] && toc[0].ignoreAllSubs && toc.splice(0)
toc[0] && toc[0].level === 1 && toc.shift()
for (let i = 0; i < toc.length; i++) {
toc[i].ignoreSubHeading && toc.splice(i, 1) && i--
}
const tree = cacheTree[currentPath] || genTree(toc, level)
cacheTree[currentPath] = tree
this.toc = []
return treeTpl(tree, '<ul class="app-sub-sidebar">')
}
article (text) {
return this.compile(text)
}
/**
* Compile cover page
*/
cover (text) {
const cacheToc = this.toc.slice()
const html = this.compile(text)
this.toc = cacheToc.slice()
return html
}
}