-
Notifications
You must be signed in to change notification settings - Fork 80
/
md.server.ts
434 lines (386 loc) · 12.2 KB
/
md.server.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*!
* Forked from https://github.com/ryanflorence/md/blob/master/index.ts
*
* Adapted from
* - ggoodman/nostalgie
* - MIT https://github.com/ggoodman/nostalgie/blob/45f3f6356684287a214dab667064ec9776def933/LICENSE
* - https://github.com/ggoodman/nostalgie/blob/45f3f6356684287a214dab667064ec9776def933/src/worker/mdxCompiler.ts
*/
import { getHighlighter, toShikiTheme } from "shiki";
import rangeParser from "parse-numeric-range";
import parseFrontMatter from "front-matter";
import type * as Hast from "hast";
import type * as Unist from "unist";
import type * as Shiki from "shiki";
import type * as Unified from "unified";
import themeJson from "../../data/base16.json";
export interface ProcessorOptions {
resolveHref?(href: string): string;
}
let processor: Awaited<ReturnType<typeof getProcessor>>;
export async function processMarkdown(
content: string,
options?: ProcessorOptions,
) {
processor = processor || (await getProcessor(options));
let { attributes, body: raw } = parseFrontMatter(content);
let vfile = await processor.process(raw);
let html = vfile.value.toString();
return { attributes, raw, html };
}
export async function getProcessor(options?: ProcessorOptions) {
let [
{ unified },
{ default: remarkGfm },
{ default: remarkParse },
{ default: remarkRehype },
{ default: rehypeSlug },
{ default: rehypeStringify },
{ default: rehypeAutolinkHeadings },
plugins,
] = await Promise.all([
import("unified"),
import("remark-gfm"),
import("remark-parse"),
import("remark-rehype"),
import("rehype-slug"),
import("rehype-stringify"),
import("rehype-autolink-headings"),
loadPlugins(),
]);
return unified()
.use(remarkParse)
.use(plugins.stripLinkExtPlugin, options)
.use(plugins.remarkCodeBlocksShiki, options)
.use(remarkGfm)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeStringify, { allowDangerousHtml: true })
.use(rehypeSlug)
.use(rehypeAutolinkHeadings);
}
type InternalPlugin<
Input extends string | Unist.Node | undefined,
Output,
> = Unified.Plugin<[ProcessorOptions?], Input, Output>;
export async function loadPlugins() {
let [{ visit, SKIP }, { htmlEscape }] = await Promise.all([
import("unist-util-visit"),
import("escape-goat"),
]);
const stripLinkExtPlugin: InternalPlugin<UnistNode.Root, UnistNode.Root> = (
options = {},
) => {
return async function transformer(tree: UnistNode.Root) {
visit(tree, "link", (node, index, parent) => {
if (
options.resolveHref &&
typeof node.url === "string" &&
isRelativeUrl(node.url)
) {
if (parent && index != null) {
parent.children[index] = {
...node,
url: options.resolveHref(node.url),
};
return SKIP;
}
}
});
};
};
const remarkCodeBlocksShiki: InternalPlugin<
UnistNode.Root,
UnistNode.Root
> = (options) => {
let theme: ReturnType<typeof toShikiTheme>;
let highlighterPromise: ReturnType<typeof getHighlighter>;
return async function transformer(tree: UnistNode.Root) {
theme = theme || toShikiTheme(themeJson as any);
highlighterPromise =
highlighterPromise || getHighlighter({ themes: [theme] });
let highlighter = await highlighterPromise;
let fgColor = convertFakeHexToCustomProp(
highlighter.getForegroundColor(theme.name) || "",
);
let langs: Shiki.Lang[] = [
"js",
"json",
"jsx",
"ts",
"tsx",
"markdown",
"shellscript",
"html",
"css",
"diff",
"mdx",
"prisma",
];
let langSet = new Set(langs);
let transformTasks: Array<() => Promise<void>> = [];
visit(tree, "code", (node) => {
if (
!node.lang ||
!node.value ||
!langSet.has(node.lang as Shiki.Lang)
) {
return;
}
if (node.lang === "js") node.lang = "javascript";
if (node.lang === "ts") node.lang = "typescript";
let language = node.lang;
let code = node.value;
let {
addedLines,
highlightLines,
nodeProperties,
removedLines,
startingLineNumber,
usesLineNumbers,
} = getCodeBlockMeta();
transformTasks.push(highlightNodes);
return SKIP;
async function highlightNodes() {
let tokens = getThemedTokens({ code, language });
let children = tokens.map(
(lineTokens, zeroBasedLineNumber): Hast.Element => {
let children = lineTokens.map(
(token): Hast.Text | Hast.Element => {
let color = convertFakeHexToCustomProp(token.color || "");
let content: Hast.Text = {
type: "text",
// Do not escape the _actual_ content
value: token.content,
};
return color && color !== fgColor
? {
type: "element",
tagName: "span",
properties: {
style: `color: ${htmlEscape(color)}`,
},
children: [content],
}
: content;
},
);
children.push({
type: "text",
value: "\n",
});
let isDiff = addedLines.length > 0 || removedLines.length > 0;
let diffLineNumber = startingLineNumber - 1;
let lineNumber = zeroBasedLineNumber + startingLineNumber;
let highlightLine = highlightLines?.includes(lineNumber);
let removeLine = removedLines.includes(lineNumber);
let addLine = addedLines.includes(lineNumber);
if (!removeLine) {
diffLineNumber++;
}
return {
type: "element",
tagName: "span",
properties: {
className: "codeblock-line",
dataHighlight: highlightLine ? "true" : undefined,
dataLineNumber: usesLineNumbers ? lineNumber : undefined,
dataAdd: isDiff ? addLine : undefined,
dataRemove: isDiff ? removeLine : undefined,
dataDiffLineNumber: isDiff ? diffLineNumber : undefined,
},
children,
};
},
);
let nodeValue = {
type: "element",
tagName: "pre",
properties: {
...nodeProperties,
dataLineNumbers: usesLineNumbers ? "true" : "false",
dataLang: htmlEscape(language),
style: `color: ${htmlEscape(fgColor)};`,
},
children: [
{
type: "element",
tagName: "code",
children,
},
],
};
let data = node.data ?? {};
(node as any).type = "element";
(node as any).tagName = "div";
let properties =
data.hProperties && typeof data.hProperties === "object"
? data.hProperties
: {};
data.hProperties = {
...properties,
dataCodeBlock: "",
...nodeProperties,
dataLineNumbers: usesLineNumbers ? "true" : "false",
dataLang: htmlEscape(language),
};
data.hChildren = [nodeValue];
node.data = data;
}
function getCodeBlockMeta() {
// TODO: figure out how this is ever an array?
let meta = Array.isArray(node.meta) ? node.meta[0] : node.meta;
let metaParams = new URLSearchParams();
if (meta) {
let linesHighlightsMetaShorthand = meta.match(/^\[(.+)\]$/);
if (linesHighlightsMetaShorthand) {
metaParams.set("lines", linesHighlightsMetaShorthand[0]);
} else {
metaParams = new URLSearchParams(meta.split(/\s+/).join("&"));
}
}
let addedLines = parseLineHighlights(metaParams.get("add"));
let removedLines = parseLineHighlights(metaParams.get("remove"));
let highlightLines = parseLineHighlights(metaParams.get("lines"));
let startValNum = metaParams.has("start")
? Number(metaParams.get("start"))
: 1;
let startingLineNumber = Number.isFinite(startValNum)
? startValNum
: 1;
let usesLineNumbers = !metaParams.has("nonumber");
let nodeProperties: { [key: string]: string } = {};
metaParams.forEach((val, key) => {
if (key === "lines") return;
nodeProperties[`data-${key}`] = val;
});
return {
addedLines,
highlightLines,
nodeProperties,
removedLines,
startingLineNumber,
usesLineNumbers,
};
}
});
await Promise.all(transformTasks.map((exec) => exec()));
function getThemedTokens({
code,
language,
}: {
code: string;
language: Shiki.Lang;
}) {
return highlighter.codeToThemedTokens(code, language, theme.name, {
includeExplanation: false,
});
}
};
};
return {
stripLinkExtPlugin,
remarkCodeBlocksShiki,
};
}
////////////////////////////////////////////////////////////////////////////////
function parseLineHighlights(param: string | null) {
if (!param) return [];
let range = param.match(/^\[(.+)\]$/);
if (!range) return [];
return rangeParser(range[1]);
}
// The theme actually stores #FFFF${base-16-color-id} because vscode-textmate
// requires colors to be valid hex codes, if they aren't, it changes them to a
// default, so this is a mega hack to trick it.
function convertFakeHexToCustomProp(color: string) {
return color.replace(/^#FFFF(.+)/, "var(--base$1)");
}
function isRelativeUrl(test: string) {
// Probably fragile but should work well enough.
// It would be nice if the consumer could provide a baseURI we could do
// something like:
// new URL(baseURI).origin === new URL(test, baseURI).origin
let regexp = new RegExp("^(?:[a-z]+:)?//", "i");
return !regexp.test(test);
}
////////////////////////////////////////////////////////////////////////////////
export namespace UnistNode {
export type Content = Flow | Phrasing | Html;
export interface Root extends Unist.Parent {
type: "root";
children: Flow[];
}
export type Flow =
| Blockquote
| Heading
| ParagraphNode
| Link
| Pre
| Code
| Image
| Element
| Html;
export interface Html extends Unist.Node {
type: "html";
value: string;
}
export interface Element extends Unist.Parent {
type: "element";
tagName?: string;
}
export interface CodeElement extends Element {
tagName: "code";
data?: {
meta?: string;
};
properties?: {
className?: string[];
};
}
export interface PreElement extends Element {
tagName: "pre";
}
export interface Image extends Unist.Node {
type: "image";
title: null;
url: string;
alt?: string;
}
export interface Blockquote extends Unist.Parent {
type: "blockquote";
children: Flow[];
}
export interface Heading extends Unist.Parent {
type: "heading";
depth: number;
children: UnistNode.Phrasing[];
}
interface ParagraphNode extends Unist.Parent {
type: "paragraph";
children: Phrasing[];
}
export interface Pre extends Unist.Parent {
type: "pre";
children: Phrasing[];
}
export interface Code extends Unist.Parent {
type: "code";
value?: string;
lang?: Shiki.Lang;
meta?: string | string[];
}
export type Phrasing = Text | Emphasis;
export interface Emphasis extends Unist.Parent {
type: "emphasis";
children: Phrasing[];
}
export interface Link extends Unist.Parent {
type: "link";
children: Flow[];
url?: string;
}
export interface Text extends Unist.Literal {
type: "text";
value: string;
}
}