Skip to content

Commit 45a3df5

Browse files
committed
refactor: hoist pre wrapper as a standalone plugin.
1 parent 1b9012e commit 45a3df5

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

lib/markdown/highlightLines.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ module.exports = md => {
88
md.renderer.rules.fence = (...args) => {
99
const [tokens, idx, options] = args
1010
const token = tokens[idx]
11-
const langName = token.info.replace(RE, '').trim()
1211

13-
if (!token.info || !RE.test(token.info)) {
14-
return `<div class="language-${langName}">${fence(...args)}</div>`
12+
const rawInfo = token.info
13+
if (!rawInfo || !RE.test(rawInfo)) {
14+
return fence(...args)
1515
}
1616

17-
const lineNumbers = RE.exec(token.info)[1]
17+
const langName = rawInfo.replace(RE, '').trim()
18+
// ensure the next plugin get the correct lang.
19+
token.info = langName
20+
21+
const lineNumbers = RE.exec(rawInfo)[1]
1822
.split(',')
1923
.map(v => v.split('-').map(v => parseInt(v, 10)))
2024

@@ -51,6 +55,6 @@ module.exports = md => {
5155
highlightedCode += `${split.code}\n`
5256
}
5357
})
54-
return `<div class="language-${langName}">${highlightedCode}</div>`
58+
return highlightedCode
5559
}
5660
}

lib/markdown/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const highlight = require('./highlight')
22
const highlightLines = require('./highlightLines')
3+
const preWrapper = require('./preWrapper')
34
const component = require('./component')
45
const hoistScriptStyle = require('./hoist')
56
const convertRouterLink = require('./link')
@@ -21,6 +22,7 @@ module.exports = ({ markdown = {}} = {}) => {
2122
// custom plugins
2223
.use(component)
2324
.use(highlightLines)
25+
.use(preWrapper)
2426
.use(convertRouterLink, Object.assign({
2527
target: '_blank',
2628
rel: 'noopener noreferrer'

lib/markdown/preWrapper.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// markdown-it plugin for wrapping <pre> ... </pre>.
2+
3+
module.exports = md => {
4+
const fence = md.renderer.rules.fence
5+
md.renderer.rules.fence = (...args) => {
6+
const [tokens, idx] = args
7+
const token = tokens[idx]
8+
const rawCode = fence(...args)
9+
return `<!--beforebegin--><div class="language-${token.info.trim()}"><!--afterbegin-->${rawCode}<!--beforeend--></div><!--afterend-->`
10+
}
11+
}

0 commit comments

Comments
 (0)