-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
index.js
177 lines (146 loc) Β· 4.11 KB
/
index.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
'use strict'
/**
* Module dependencies.
*/
const Config = require('markdown-it-chain')
const highlight = require('./lib/highlight')
const { PLUGINS, REQUIRED_PLUGINS } = require('./lib/constant')
const highlightLinesPlugin = require('./lib/highlightLines')
const preWrapperPlugin = require('./lib/preWrapper')
const lineNumbersPlugin = require('./lib/lineNumbers')
const componentPlugin = require('./lib/component')
const hoistScriptStylePlugin = require('./lib/hoist')
const convertRouterLinkPlugin = require('./lib/link')
const snippetPlugin = require('./lib/snippet')
const emojiPlugin = require('markdown-it-emoji')
const anchorPlugin = require('markdown-it-anchor')
const tocPlugin = require('markdown-it-table-of-contents')
const {
slugify: _slugify,
parseHeaders,
logger, chalk, normalizeConfig,
moduleResolver: { getMarkdownItResolver }
} = require('@vuepress/shared-utils')
/**
* Create markdown by config.
*/
module.exports = (markdown = {}) => {
const {
externalLinks,
anchor,
toc,
plugins,
lineNumbers,
beforeInstantiate,
afterInstantiate
} = markdown
const resolver = getMarkdownItResolver()
// allow user config slugify
const slugify = markdown.slugify || _slugify
// using chainedAPI
const config = new Config()
config
.options
.html(true)
.highlight(highlight)
.end()
.plugin(PLUGINS.COMPONENT)
.use(componentPlugin)
.end()
.plugin(PLUGINS.HIGHLIGHT_LINES)
.use(highlightLinesPlugin)
.end()
.plugin(PLUGINS.PRE_WRAPPER)
.use(preWrapperPlugin)
.end()
.plugin(PLUGINS.SNIPPET)
.use(snippetPlugin)
.end()
.plugin(PLUGINS.CONVERT_ROUTER_LINK)
.use(convertRouterLinkPlugin, [Object.assign({
target: '_blank',
rel: 'noopener noreferrer'
}, externalLinks)])
.end()
.plugin(PLUGINS.HOIST_SCRIPT_STYLE)
.use(hoistScriptStylePlugin)
.end()
.plugin(PLUGINS.EMOJI)
.use(emojiPlugin)
.end()
.plugin(PLUGINS.ANCHOR)
.use(anchorPlugin, [Object.assign({
slugify,
permalink: true,
permalinkBefore: true,
permalinkSymbol: '#'
}, anchor)])
.end()
.plugin(PLUGINS.TOC)
.use(tocPlugin, [Object.assign({
slugify,
includeLevel: [2, 3],
format: parseHeaders
}, toc)])
.end()
if (lineNumbers) {
config
.plugin(PLUGINS.LINE_NUMBERS)
.use(lineNumbersPlugin)
}
beforeInstantiate && beforeInstantiate(config)
const md = config.toMd(require('markdown-it'), markdown)
const pluginsConfig = normalizeConfig(plugins || [])
pluginsConfig.forEach(([pluginRaw, pluginOptions]) => {
const plugin = resolver.resolve(pluginRaw)
if (plugin.entry) {
md.use(plugin.entry, pluginOptions)
} else {
// TODO: error handling
}
})
afterInstantiate && afterInstantiate(md)
module.exports.dataReturnable(md)
// expose slugify
md.slugify = slugify
return md
}
module.exports.dataReturnable = function dataReturnable (md) {
// override render to allow custom plugins return data
const render = md.render
md.render = (...args) => {
md.$data = {}
md.$data.__data_block = {}
md.$dataBlock = md.$data.__data_block
const html = render.call(md, ...args)
return {
html,
data: md.$data,
dataBlockString: toDataBlockString(md.$dataBlock)
}
}
}
function toDataBlockString (ob) {
if (Object.keys(ob).length === 0) {
return ''
}
return `<data>${JSON.stringify(ob)}</data>`
}
function isRequiredPlugin (plugin) {
return REQUIRED_PLUGINS.includes(plugin)
}
function removePlugin (config, plugin) {
logger.debug(`Built-in markdown-it plugin ${chalk.green(plugin)} was removed.`)
config.plugins.delete(plugin)
}
function removeAllBuiltInPlugins (config) {
Object.keys(PLUGINS).forEach(key => {
if (!isRequiredPlugin(PLUGINS[key])) {
removePlugin(config, PLUGINS[key])
}
})
}
module.exports.isRequiredPlugin = isRequiredPlugin
module.exports.removePlugin = removePlugin
module.exports.removeAllBuiltInPlugins = removeAllBuiltInPlugins
module.exports.PLUGINS = PLUGINS