-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
137 lines (106 loc) · 3.17 KB
/
index.mjs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { getValue, parse, stringify } from 'markdown-code-block-meta';
import {
directiveFromMarkdown,
directiveToMarkdown,
} from 'mdast-util-directive';
import { fromMarkdown } from 'mdast-util-from-markdown';
import { toMarkdown } from 'mdast-util-to-markdown';
import { directive } from 'micromark-extension-directive';
import { visit } from 'unist-util-visit';
function ast2md(ast) {
return toMarkdown(
{ type: 'root', children: [ast] },
{
extensions: [directiveToMarkdown()],
},
).trim();
}
function md2ast(md) {
return fromMarkdown(md, {
extensions: [directive()],
mdastExtensions: [directiveFromMarkdown()],
});
}
function visitCode(tree, key, visitor) {
visit(
tree,
({ type, meta }) => type === 'code' && meta && parse(meta).has(key),
(node, index, parent) => {
const meta = parse(node.meta);
const lang = getValue(meta.get(key)) || 'markdown';
if (key === 'code-eval-copy' && ['md', 'markdown'].includes(lang)) {
const list = md2ast(node.value).children;
parent.children.splice(index + 1, 0, ...list);
meta.delete(key);
node.meta = stringify(meta);
return;
}
meta.delete(key);
const copyToBefore = meta.has('copy-to-before');
if (copyToBefore) {
meta.delete('copy-to-before');
}
const newMeta = new Map();
if (key.includes('copy')) {
const tabKey = 'copy-as-tab';
const hasTab = meta.has(tabKey);
const copyAsTab = meta.get(tabKey);
if (hasTab && key !== 'code-alias-copy') {
meta.delete(tabKey);
}
if (hasTab) {
newMeta.set('tab', copyAsTab);
}
}
visitor({
node,
index,
parent,
lang,
copyToBefore,
meta,
newMeta,
});
},
);
}
/* eslint-disable no-param-reassign */
export function remarkCodeExample({ metas = {} } = {}) {
return (tree) => {
visitCode(
tree,
'code-alias-copy',
({ node, index, parent, lang, copyToBefore = false, meta }) => {
node.meta = stringify(meta);
const newIndex = copyToBefore ? index : index + 1;
parent.children.splice(newIndex, 0, {
...node,
lang,
value: node.value,
});
},
);
visitCode(
tree,
'code-example-copy',
({ node, index, parent, lang, copyToBefore = false, meta, newMeta }) => {
const newIndex = copyToBefore ? index : index + 1;
const extra = Object.entries(metas?.[lang] ?? {});
parent.children.splice(newIndex, 0, {
type: 'code',
lang,
value: ast2md({ ...node, meta: stringify(meta) }),
meta: stringify(new Map([...extra, ...newMeta])),
});
node.meta = stringify(new Map([...meta, ...newMeta]));
},
);
visitCode(tree, 'code-example', ({ node, lang, meta, newMeta }) => {
node.value = ast2md({ ...node, meta: stringify(meta) });
node.lang = lang;
const extra = Object.entries(metas?.[lang] ?? {});
node.meta = stringify(new Map([...extra, ...newMeta]));
});
visitCode(tree, 'code-eval-copy');
};
}