-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
37 lines (28 loc) · 1002 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
const visit = require('unist-util-visit');
const isComment = /<!--(.*?)-->/u;
const getComment = /<!--([\s\S]*?)-->/u;
const plugin = (options = {}) => {
const transformer = (tree) => {
const excerpts =
options.identifier && options.identifier.length
? [options.identifier]
: ['excerpt', 'more', 'preview', 'teaser'];
let excerptIndex = -1;
visit(tree, 'html', (node) => {
if (excerptIndex === -1 && isComment.test(node.value)) {
const comment = getComment.exec(node.value);
if (comment) {
const text = comment[1].trim();
if (excerpts.includes(text)) {
excerptIndex = tree.children.indexOf(node);
}
}
}
});
if (excerptIndex > -1) {
tree.children.splice(excerptIndex);
}
};
return transformer;
};
module.exports = plugin;