-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
314 lines (264 loc) · 7.69 KB
/
index.ts
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { visit, type Visitor } from "unist-util-visit";
import type { Plugin, Transformer } from "unified";
import type { Node, Parent, Data } from "unist";
import type { Paragraph, Code, Root } from "mdast";
export interface CodeTitleOptions {
/**
* @defaultValue true
*/
title?: boolean;
/**
* @defaultValue "div"
*/
titleTagName?: string;
/**
* @defaultValue "remark-code-title"
*/
titleClassName?: string;
/**
* @defaultValue undefined
*/
titleProperties?: (
language?: string,
title?: string,
) => Record<string, unknown>;
/**
* @defaultValue true
*/
container?: boolean;
/**
* @defaultValue "div"
*/
containerTagName?: string;
/**
* @defaultValue "remark-code-container"
*/
containerClassName?: string;
/**
* @defaultValue undefined
*/
/**
* @defaultValue undefined
*/
containerProperties?: (
language?: string,
title?: string,
) => Record<string, unknown>;
}
const DEFAULT_SETTINGS: CodeTitleOptions = {
title: true,
titleTagName: "div",
titleClassName: "remark-code-title",
titleProperties: undefined,
container: true,
containerTagName: "div",
containerClassName: "remark-code-container",
containerProperties: undefined,
};
type T = string | null | undefined;
/**
*
* This plugin adds a title element before the code element, if the title exists in the markdown code block, and wraps them in a container.
*
* for example:
* ```javascript:title.js
* // some js code
* ```
*/
export const plugin: Plugin<[CodeTitleOptions?], Root> = (
options,
): Transformer<Root> => {
const settings = Object.assign({}, DEFAULT_SETTINGS, options);
/** for creating mdx elements just in case (for archive)
const titleNode = {
type: "mdxJsxFlowElement",
name: "div",
attributes: [
{
type: "mdxJsxAttribute",
name: "className",
value: "remark-code-title",
},
{ type: "mdxJsxAttribute", name: "data-language", value: language },
],
children: [{ type: "text", value: title }],
data: { _xdmExplicitJsx: true },
};
const containerNode = {
type: "mdxJsxFlowElement",
name: "div",
attributes: [
{
type: "mdxJsxAttribute",
name: "className",
value: "remark-code-container",
},
],
children: [titleNode, node],
data: { _xdmExplicitJsx: true },
};
*/
const constructTitle = (language: string, title: string): Paragraph => {
let properties: Record<string, unknown> | undefined;
if (settings.titleProperties) {
properties = settings.titleProperties(language, title);
Object.entries(properties).forEach(([k, v]) => {
if (typeof v === "string" && !v.length) {
properties && (properties[k] = undefined);
}
});
}
return {
type: "paragraph",
children: [{ type: "text", value: title }],
data: {
hName: settings.titleTagName,
hProperties: {
className: [settings.titleClassName],
...(properties && { ...properties }),
},
},
};
};
const constructContainer = (
children: Node<Data>[],
language: string,
title: string,
): Parent => {
let properties: Record<string, unknown> | undefined;
if (settings.containerProperties) {
properties = settings.containerProperties(language, title);
Object.entries(properties).forEach(([k, v]) => {
if (typeof v === "string" && !v.length) {
properties && (properties[k] = undefined);
}
});
}
return {
type: "container",
children,
data: {
hName: settings.containerTagName,
hProperties: {
className: [settings.containerClassName],
...(properties && { ...properties }),
},
},
};
};
const extractLanguageAndTitle = (node: Code) => {
const { lang: inputLang, meta: inputMeta } = node;
if (!inputLang && !inputMeta) {
return { language: null, title: null, meta: null };
}
let title: T = undefined;
let language: T = inputLang;
let meta: T = inputMeta?.replace(/\s+/g, " ").trim(); // remove extra space + trim + remove spaces in the curly braces
// correctness for rhypePrismPlus
if (
inputLang?.startsWith("{") ||
inputLang?.toLowerCase().startsWith("showlinenumbers")
) {
if (meta?.length) {
meta = language + meta;
} else {
meta = inputLang;
}
language = null;
title = null;
}
// another correctness for rhypePrismPlus
else if (inputLang?.includes("{")) {
const i = inputLang.search("{");
const metaPart = inputLang.slice(i, inputLang.length);
language = inputLang.slice(0, i);
if (!language.length) language = null;
if (meta?.length) {
meta = metaPart + meta;
} else {
meta = metaPart;
}
}
// another correctness for line ranges, removing all spaces within curly braces
const RE = /{([\d\s,-]+)}/g; // finds {1, 2-4} like strings for line highlighting
if (meta?.length && RE.test(meta)) {
meta = meta.replace(RE, function (match) {
return match.replace(/ /g, "");
});
}
// after correctness
const _inputLang = language;
if (_inputLang?.includes(":")) {
language = _inputLang.slice(0, _inputLang.search(":"));
if (!language.length) language = null;
title = _inputLang.slice(_inputLang.search(":") + 1, _inputLang.length);
if (!title.length && !meta?.length) {
title = null;
meta = null;
} else if (!title.length && meta?.length) {
const _meta = meta;
const firstWord = _meta.replace(/ .*/, "");
if (
firstWord.startsWith("{") ||
firstWord.toLowerCase().startsWith("showlinenumbers")
) {
title = null;
} else {
title = firstWord;
meta = meta.slice(title.length).trim();
}
}
} else if (meta?.startsWith(":")) {
meta = meta.slice(1).trim();
if (!meta.length) {
title = null;
meta = null;
} else {
const _meta = meta;
const firstWord = _meta.replace(/ .*/, "");
if (
firstWord.startsWith("{") ||
firstWord.toLowerCase().startsWith("showlinenumbers")
) {
title = null;
} else {
title = firstWord;
meta = meta.slice(title.length).trim();
}
}
}
return { title, language, meta };
};
const visitor: Visitor<Code> = (node, index, parent) => {
const { title, language, meta } = extractLanguageAndTitle(node);
// console.log({ title, language, meta });
// mutating the parent.children may effect the next iteration causing visit the same node "code"
// so, it is important to normalize the language here, otherwise may cause infinite loop
node.lang = language;
node.meta = meta;
let titleNode: Paragraph | undefined = undefined;
let containerNode: Parent | undefined = undefined;
if (settings.title && title) {
titleNode = constructTitle(language ?? "", title ?? "");
}
if (settings.container) {
containerNode = constructContainer(
titleNode ? [titleNode, node] : [node],
language ?? "",
title ?? "",
);
}
if (containerNode) {
// 1 is for replacing the "code" with the container which consists it already
parent?.children.splice(index!, 1, containerNode);
} else if (titleNode) {
// 0 is for inserting the titleNode before the "code"
parent?.children.splice(index!, 0, titleNode);
}
};
const transformer: Transformer<Root> = (tree) => {
visit(tree, "code", visitor);
};
return transformer;
};
export default plugin;