-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
33 lines (28 loc) · 968 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
const title = require('title')
const visit = require('unist-util-visit')
const headingsByLevel = level => {
return Array.from(Array(level), (h, idx) => `h${idx+1}`)
}
module.exports.transform = ({ special, maxDepth = 6, frontMatter = ['title'] } = {}, posts) => {
const excludeFrontMatter = !frontMatter || !frontMatter.length
const headings = headingsByLevel(maxDepth)
const opts = { special }
return posts.map(post => {
if (!excludeFrontMatter) {
for (const field of frontMatter ) {
if (post.data[field]) {
post.data[field] = title(post.data[field], opts)
}
}
}
visit(post.content, 'element', (node) => {
if(headings.includes(node.tagName)) {
visit(node, 'text', (textNode, textIndex, textParent) => {
textParent.children.splice(textIndex, 1, {...textNode, value: title(textNode.value, opts)})
return textIndex + 1
})
}
})
return post
});
}