Skip to content

Commit 15af271

Browse files
committed
feat($markdown): ability to disable built-in markdown extensions
chainMarkdown (config) { require('@vuepress/markdown').removeAllBuiltInPlugins(config) } Note that COMPONENT and ANCHOR plugin are required in VuePress, It is forbidden to delete them!
1 parent ca03a12 commit 15af271

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

packages/@vuepress/markdown/index.js

+36-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const Config = require('markdown-it-chain')
88
const highlight = require('./lib/highlight')
9+
const { PLUGINS, REQUIRED_PLUGINS } = require('./lib/constant')
910
const highlightLinesPlugin = require('./lib/highlightLines')
1011
const preWrapperPlugin = require('./lib/preWrapper')
1112
const lineNumbersPlugin = require('./lib/lineNumbers')
@@ -18,7 +19,7 @@ const snippetPlugin = require('./lib/snippet')
1819
const emojiPlugin = require('markdown-it-emoji')
1920
const anchorPlugin = require('markdown-it-anchor')
2021
const tocPlugin = require('markdown-it-table-of-contents')
21-
const { parseHeaders, slugify: _slugify } = require('@vuepress/shared-utils')
22+
const { parseHeaders, slugify: _slugify, logger, chalk } = require('@vuepress/shared-utils')
2223

2324
/**
2425
* Create markdown by config.
@@ -45,46 +46,46 @@ module.exports = ({
4546
.highlight(highlight)
4647
.end()
4748

48-
.plugin('component')
49+
.plugin(PLUGINS.COMPONENT)
4950
.use(componentPlugin)
5051
.end()
5152

52-
.plugin('highlight-lines')
53+
.plugin(PLUGINS.HIGHLIGHT_LINES)
5354
.use(highlightLinesPlugin)
5455
.end()
5556

56-
.plugin('pre-wrapper')
57+
.plugin(PLUGINS.PRE_WRAPPER)
5758
.use(preWrapperPlugin)
5859
.end()
5960

60-
.plugin('snippet')
61+
.plugin(PLUGINS.SNIPPET)
6162
.use(snippetPlugin)
6263
.end()
6364

64-
.plugin('convert-router-link')
65+
.plugin(PLUGINS.CONVERT_ROUTER_LINK)
6566
.use(convertRouterLinkPlugin, [Object.assign({
6667
target: '_blank',
6768
rel: 'noopener noreferrer'
6869
}, externalLinks)])
6970
.end()
7071

71-
.plugin('hoist-script-style')
72+
.plugin(PLUGINS.HOIST_SCRIPT_STYLE)
7273
.use(hoistScriptStylePlugin)
7374
.end()
7475

75-
.plugin('containers')
76+
.plugin(PLUGINS.CONTAINERS)
7677
.use(containersPlugin)
7778
.end()
7879

79-
.plugin('markdown-slots-containers')
80+
.plugin(PLUGINS.MARKDOWN_SLOTS_CONTAINERS)
8081
.use(markdownSlotsContainersPlugin)
8182
.end()
8283

83-
.plugin('emoji')
84+
.plugin(PLUGINS.EMOJI)
8485
.use(emojiPlugin)
8586
.end()
8687

87-
.plugin('anchor')
88+
.plugin(PLUGINS.ANCHOR)
8889
.use(anchorPlugin, [Object.assign({
8990
slugify,
9091
permalink: true,
@@ -93,7 +94,7 @@ module.exports = ({
9394
}, anchor)])
9495
.end()
9596

96-
.plugin('toc')
97+
.plugin(PLUGINS.TOC)
9798
.use(tocPlugin, [Object.assign({
9899
slugify,
99100
includeLevel: [2, 3],
@@ -103,7 +104,7 @@ module.exports = ({
103104

104105
if (lineNumbers) {
105106
config
106-
.plugin('line-numbers')
107+
.plugin(PLUGINS.LINE_NUMBERS)
107108
.use(lineNumbersPlugin)
108109
}
109110

@@ -143,3 +144,25 @@ function toDataBlockString (ob) {
143144
}
144145
return `<data>${JSON.stringify(ob)}</data>`
145146
}
147+
148+
function isRequiredPlugin (plugin) {
149+
return REQUIRED_PLUGINS.includes(plugin)
150+
}
151+
152+
function removePlugin (config, plugin) {
153+
logger.debug(`Built-in markdown-it plugin ${chalk.green(plugin)} was removed.`)
154+
config.plugins.delete(plugin)
155+
}
156+
157+
function removeAllBuiltInPlugins (config) {
158+
Object.keys(PLUGINS).forEach(key => {
159+
if (!isRequiredPlugin(PLUGINS[key])) {
160+
removePlugin(config, PLUGINS[key])
161+
}
162+
})
163+
}
164+
165+
module.exports.isRequiredPlugin = isRequiredPlugin
166+
module.exports.removePlugin = removePlugin
167+
module.exports.removeAllBuiltInPlugins = removeAllBuiltInPlugins
168+
module.exports.PLUGINS = PLUGINS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
exports.PLUGINS = {
2+
COMPONENT: 'component',
3+
HIGHLIGHT_LINES: 'highlight-lines',
4+
PRE_WRAPPER: 'pre-wrapper',
5+
SNIPPET: 'snippet',
6+
CONVERT_ROUTER_LINK: 'convert-router-link',
7+
HOIST_SCRIPT_STYLE: 'hoist-script-style',
8+
CONTAINERS: 'containers',
9+
MARKDOWN_SLOTS_CONTAINERS: 'markdown-slots-containers',
10+
ANCHOR: 'anchor',
11+
EMOJI: 'emoji',
12+
TOC: 'toc',
13+
LINE_NUMBERS: 'line-numbers'
14+
}
15+
16+
exports.REQUIRED_PLUGINS = [
17+
exports.PLUGINS.COMPONENT,
18+
exports.PLUGINS.ANCHOR
19+
]

0 commit comments

Comments
 (0)