-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathpreWrapper.js
More file actions
22 lines (21 loc) · 857 Bytes
/
preWrapper.js
File metadata and controls
22 lines (21 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// markdown-it plugin for wrapping <pre> ... </pre>.
//
// If your plugin was chained before preWrapper, you can add additional element directly.
// If your plugin was chained after preWrapper, you can use these slots:
// 1. <!--beforebegin-->
// 2. <!--afterbegin-->
// 3. <!--beforeend-->
// 4. <!--afterend-->
module.exports = md => {
const wrap = (wrapped) => (...args) => {
const [tokens, idx] = args
const token = tokens[idx]
const rawCode = wrapped(...args)
const tokenInfo = token.info.trim().replace(/\"/g, '\'')
return `<!--beforebegin--><div class="language-${tokenInfo} extra-class">`
+ `<!--afterbegin-->${rawCode}<!--beforeend--></div><!--afterend-->`
}
const { fence, code_block: codeBlock } = md.renderer.rules
md.renderer.rules.fence = wrap(fence)
md.renderer.rules.code_block = wrap(codeBlock)
}