-
Notifications
You must be signed in to change notification settings - Fork 66.8k
Expand file tree
/
Copy pathreplace-domain.js
More file actions
37 lines (32 loc) · 1015 Bytes
/
replace-domain.js
File metadata and controls
37 lines (32 loc) · 1015 Bytes
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
/**
* This makes it so that the `github.com` or `HOSTNAME` in a code snippet
* becomes replacable.
*/
import { visit } from 'unist-util-visit'
// Don't use `g` on these regexes
const VALID_REPLACEMENTS = [[/\bHOSTNAME\b/, 'HOSTNAME']]
const CODE_FENCE_KEYWORD = 'replacedomain'
const matcher = (node) => {
return (
node.type === 'element' &&
node.tagName === 'pre' &&
node.children[0]?.data?.meta[CODE_FENCE_KEYWORD]
)
}
export default function alerts() {
return (tree) => {
visit(tree, matcher, (node) => {
const code = node.children[0].children[0].value
for (const [regex, replacement] of VALID_REPLACEMENTS) {
if (regex.test(code)) {
const codeTag = node.children[0]
const replacements = codeTag.properties['data-replacedomain'] || []
if (!replacements.includes(replacement)) {
replacements.push(replacement)
codeTag.properties['data-replacedomain'] = replacements
}
}
}
})
}
}