-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (35 loc) · 923 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
import { u } from 'unist-builder'
import { findAndReplace } from 'mdast-util-find-and-replace'
export default function remarkAutomaticGlossaryMarkup({
terms,
withinNodeTypes = ['paragraph', 'root', 'list', 'listItem'],
}) {
return (tree) => {
terms.forEach((glossaryTerm) => {
const caseInsensitiveRegex = new RegExp(
`\\b${glossaryTerm.term}(?!\\])\\b`,
'i'
)
let foundOnce = false
findAndReplace(
tree,
[
caseInsensitiveRegex,
function ($0) {
if (!foundOnce) {
foundOnce = true
return u('link', { url: `#glossary-${encodeURIComponent($0)}` }, [
u('text', $0),
])
} else {
return u('text', $0)
}
},
],
{
ignore: (node) => !withinNodeTypes.includes(node.type),
}
)
})
}
}