-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
46 lines (41 loc) · 1.09 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
import defu from "defu";
import { visit, SKIP } from "unist-util-visit";
const defaults = {
marker: "> "
};
export default function remarkInlineCodeLang(userOptions = {}) {
const { marker } = defu(userOptions, defaults);
const markerLength = marker.length;
return async function (tree) {
visit(tree, "inlineCode", (node, index, parent) => {
const [lang, value] = node.value.split(marker);
const annotatedInlineCode = lang && value && node.value.startsWith(`${lang}${marker}`);
if (!annotatedInlineCode) {
return;
}
const columnStart = node.position.start.column - lang.length - markerLength;
const columnEnd = columnStart + value.length;
const newNode = {
type: node.type,
value: value,
data: {
hProperties: { lang }
},
position: {
start: {
line: node.position.start.line,
column: columnStart,
offset: columnStart - 1
},
end: {
line: node.position.end.line,
column: columnEnd,
offset: columnEnd - 1
}
}
};
parent.children.splice(index, 1, newNode);
return [SKIP, index];
});
};
}