-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
43 lines (35 loc) · 1003 Bytes
/
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
const codeblocks = (tree, options) => {
options = options || {}
const lang = options.lang || 'all'
const name = options.name || 'codeblocks'
const formatter = options.formatter || (v => v)
const { children } = tree
let data = {}
let i = -1
let child
if (lang === 'all') {
data[name] = {}
} else {
data[name] = []
}
while (++i < children.length) {
child = children[i]
if (child.type === 'code' && child.value) {
if (lang === 'all') {
child.lang = child.lang || '_'
data[name][child.lang] = data[name][child.lang] || []
data[name][child.lang].push(formatter(child.value))
} else {
if (child.lang === lang) {
data[name].push(formatter(child.value))
}
}
}
}
return data
}
module.exports = options => (tree, file) => {
const data = codeblocks(tree, options)
file.data = Object.assign({}, file.data, data)
}
module.exports.codeblocks = (tree, options) => codeblocks(tree, options)