-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
compiler.js
135 lines (114 loc) Β· 3.17 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
import marked from 'marked'
import Prism from 'prismjs'
import { helper as helperTpl, tree as treeTpl } from './tpl'
import { genTree } from './gen-tree'
import { slugify, clearSlugCache } from './slugify'
import { emojify } from './emojify'
import { toURL, parse } from '../route/hash'
import { getBasePath, getPath } from '../route/util'
import { isFn, merge, cached } from '../util/core'
let markdownCompiler = marked
let contentBase = ''
let currentPath = ''
let renderer = new marked.Renderer()
const TOC = {}
let toc = []
/**
* Compile markdown content
*/
export const markdown = cached(text => {
let html = ''
if (!text) return text
html = markdownCompiler(text)
html = emojify(html)
clearSlugCache()
return html
})
markdown.renderer = renderer
markdown.init = function (config = {}, base = window.location.pathname) {
contentBase = getBasePath(base)
currentPath = parse().path
if (isFn(config)) {
markdownCompiler = config(marked, renderer)
} else {
renderer = merge(renderer, config.renderer)
marked.setOptions(merge(config, { renderer }))
}
}
markdown.update = function () {
currentPath = parse().path
}
/**
* render anchor tag
* @link https://github.com/chjj/marked#overriding-renderer-methods
*/
renderer.heading = function (text, level) {
const slug = slugify(text)
const url = toURL(currentPath, { id: slug })
toc.push({ level, slug: url, title: text })
return `<h${level} id="${slug}"><a href="${url}" data-id="${slug}" class="anchor"><span>${text}</span></a></h${level}>`
}
// highlight 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>`
}
renderer.link = function (href, title, text) {
let blank = ''
if (!/:|(\/{2})/.test(href)) {
href = toURL(href)
} else {
blank = ' target="_blank"'
}
return `<a href="${href}" title="${title || text}"${blank}>${text}</a>`
}
renderer.paragraph = function (text) {
if (/^!>/.test(text)) {
return helperTpl('tip', text)
} else if (/^\?>/.test(text)) {
return helperTpl('warn', text)
}
return `<p>${text}</p>`
}
renderer.image = function (href, title, text) {
const url = getPath(contentBase, href)
const titleHTML = title ? ` title="${title}"` : ''
return `<img src="${url}" data-origin="${href}" alt="${text}"${titleHTML}>`
}
/**
* Compile sidebar
*/
export function sidebar (text, level) {
let html = ''
if (text) {
html = markdown(text)
html = html.match(/<ul[^>]*>([\s\S]+)<\/ul>/g)[0]
} else {
const tree = genTree(toc, level)
html = treeTpl(tree, '<ul>')
}
return html
}
/**
* Compile sub sidebar
*/
export function subSidebar (el, level) {
if (el) {
toc[0] && toc[0].level === 1 && toc.shift()
const tree = genTree(TOC[currentPath] || toc, level)
el.parentNode.innerHTML += treeTpl(tree, '<ul class="app-sub-sidebar">')
}
if (toc.length) {
TOC[currentPath] = toc.slice()
}
toc = []
}
/**
* Compile cover page
*/
export function cover (text) {
const cacheToc = toc.slice()
const html = markdown(text)
toc = cacheToc.slice()
return html
}