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
36const visit = require('unist-util-visit');
function inject(node, id) {
delete node.data.hProperties.id;
const text = node.children[0] && node.children[0].value;
node.data.hChildren = [
{
type: 'element',
tagName: 'a',
properties: { id, className: ['anchor-link'] },
},
{
type: 'text',
value: text,
},
{
type: 'element',
tagName: 'a',
children: [{ type: 'text', value: '#' }],
properties: { href: `#${id}`, className: ['anchor-link-style'] },
},
];
}
module.exports = () => (tree) => {
visit(tree, 'heading', (node) => {
const { data } = node;
const id = data && data.hProperties && data.hProperties.id;
if (id) {
inject(node, id);
}
});
};