-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
58 lines (50 loc) · 1.46 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
/**
* @typedef {import('mdast').Root} Root
*
* @typedef Options
* Configuration.
* @property {string[]} [include]
* If this option is defined, this plugin will only highlight languages that
* *are* in this list.
* @property {string[]} [exclude]
* If this option is defined, this plugin will only highlight languages that
* *are not* in this list.
* @property {string} [prefix]
* If this option is defined, this plugin will use this prefix for classes
* instead of `hljs-`.
*/
import {lowlight} from 'lowlight/lib/all.js'
import {visit} from 'unist-util-visit'
/**
* Plugin to enable, disable, and ignore messages.
*
* @type {import('unified').Plugin<[Options?]|void[], Root>}
*/
export default function remarkHighlightjs(options = {}) {
const {include, exclude, prefix} = options
return (tree) => {
visit(tree, 'code', (node) => {
let {lang, data} = node
if (
!lang ||
(include && !include.includes(lang)) ||
(exclude && exclude.includes(lang))
) {
return
}
if (!data) {
data = {}
node.data = data
}
const props = /** @type {import('hast').Properties} */ (
data.hProperties || (data.hProperties = {})
)
data.hChildren = lowlight.highlight(lang, node.value, {prefix}).children
props.className = [
'hljs',
...(Array.isArray(props.className) ? props.className : []),
'language-' + lang
]
})
}
}