-
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
import { type BunPlugin, plugin } from "bun";
import type * as esbuild from "esbuild";
import type * as Hast from "hast";
import type * as Mdast from "mdast";
import { common, createStarryNight } from "@wooorm/starry-night";
export interface OutlineItem {
id: string;
content: string;
level: number;
children: OutlineItem[];
}
// StarryNight crashes when tries to create an instance other than top-level.
// maybe its hack or WASM (Oniguruma) related bug.
const starryNight = await createStarryNight(common);
export const markdownPlugin = {
name: "Markdown loader",
async setup(build) {
const { file } = await import("bun");
const { fromMarkdown } = await import("mdast-util-from-markdown");
const { toHast, defaultHandlers } = await import("mdast-util-to-hast");
const { toString: mdastToString } = await import("mdast-util-to-string");
const { raw } = await import("hast-util-raw");
const { toHtml } = await import("hast-util-to-html");
const { find } = await import("unist-util-find");
const { toString: hastToString } = await import("hast-util-to-string");
build.onLoad({ filter: /\.md$/ }, async ({ path }) => {
const mdast = fromMarkdown(await file(path).text());
const outline: OutlineItem[] = [];
const outlineStack: OutlineItem[] = [];
const hast = raw(
toHast(mdast, {
// The function cannot properly handle CommonMark without this stupid option
allowDangerousHtml: true,
handlers: {
heading(state, node: Mdast.Heading) {
const text = mdastToString(node);
const id = encodeURIComponent(
text.replace(/[ ,.]/g, "_").toLowerCase(),
);
while (
outlineStack[outlineStack.length - 1] &&
outlineStack[outlineStack.length - 1].level >= node.depth
) {
const removed = outlineStack.pop()!;
const parent = outlineStack[outlineStack.length - 1];
if (!parent) {
outline.push(removed);
break;
}
parent.children.push(removed);
}
outlineStack.push({
id,
content: text,
level: node.depth,
children: [],
});
const before = defaultHandlers.heading(state, node);
const after: Hast.Element = {
type: "element",
tagName: before.tagName,
properties: { id },
children: [
{
type: "element",
tagName: "a",
properties: { href: `#${id}` },
children: before.children,
},
],
};
state.patch(node, after);
return after;
},
code(state, node: Mdast.Code) {
const value = node.value ? node.value + "\n" : "";
const scope = node.lang
? starryNight.flagToScope(node.lang)
: null;
const children: Hast.ElementContent[] = scope
? starryNight
.highlight(value, scope)
.children.filter(
(child): child is Hast.ElementContent =>
child.type === "element" ||
child.type === "text" ||
child.type === "raw",
)
: [
{
type: "text",
value,
},
];
const code: Hast.Element = {
type: "element",
tagName: "code",
properties: {},
children,
data: node.meta ? { meta: node.meta } : {},
};
state.patch(node, code);
const pre: Hast.Element = {
type: "element",
tagName: "pre",
properties: {},
children: [state.applyData(node, code)],
};
state.patch(node, pre);
return pre;
},
},
}),
);
while (outlineStack.length > 0) {
const removed = outlineStack.pop()!;
const parent = outlineStack[outlineStack.length - 1];
if (!parent) {
outline.push(removed);
break;
}
parent.children.push(removed);
}
const h1 = find(
hast,
// @ts-expect-error: unist-util-find has broken type definition
(node) => node.type === "element" && node.tagName === "h1",
);
// @ts-expect-error: unist-util-find has broken type definition
const title = h1 ? hastToString(h1) : null;
const html = toHtml(hast);
return {
loader: "js",
contents:
"export default " +
JSON.stringify(html) +
"; export const title = " +
JSON.stringify(title) +
"; export const outline = " +
JSON.stringify(outline),
};
});
},
} satisfies BunPlugin & esbuild.Plugin;
await plugin(markdownPlugin);