This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
64 lines (55 loc) · 2.06 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
59
60
61
62
63
64
'use strict'
const toString = require('mdast-util-to-string')
const visit = require('unist-util-visit')
// TODO: pretty old? https://www.npmjs.com/package/speakingurl
// NOTE: consider https://github.com/sindresorhus/normalize-url
// NOTE: follow [sindresorhus/slugify#7](https://github.com/sindresorhus/slugify/issues/7) not supporting i18n
// check out
// https://www.npmjs.com/package/url-slug
// https://github.com/Trott/node-slug
// https://github.com/lovell/limax/blob/master/lib/limax.js even tho it uses speakingurl under the hood
const speakingurl = require(`speakingurl`)
const typesToSearch = ['heading', 'figure', 'table']
/**
* support duplicate headings like github-slugger appending a counter `-n`
* use cases https://github.com/markedjs/marked/issues/879
*
* example implementation
* https://github.com/Flet/github-slugger/blob/master/index.js
* test cases https://github.com/Flet/github-slugger/blob/master/test/index.js#L66
*
* https://www.halostatue.ca/posts/markdown-generating-heading-ids/
*
* and custom IDs / https://github.com/imcuttle/remark-heading-id
*/
/**
* inspired by https://github.com/remarkjs/remark-slug
*/
module.exports = (
{ markdownAST, markdownNode, ...props },
pluginOptions = {}
) => {
if (markdownNode && markdownNode.fields && markdownNode.fields.lang) {
const counterDb = {}
visit(markdownAST, typesToSearch, node => {
if (node.type === 'figure') console.log('\n', node, '\n', toString(node))
const lang = markdownNode.fields.lang || `en`
if (!node.id) {
const data = node.data || (node.data = {})
const props = data.hProperties || (data.hProperties = {})
let id = props.id // || node.type !== 'heading' ? 'figure' : ''
id = id
? speakingurl(id, { lang })
: speakingurl(toString(node), { lang })
counterDb[id] =
typeof counterDb[id] === 'number' ? counterDb[id] + 1 : 1
if (counterDb[id] > 1) {
id = `${id}-${counterDb[id]}`
}
node.id = id
props.id = id
}
})
}
return markdownAST
}