-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-mdx.ts
244 lines (222 loc) Β· 6.98 KB
/
build-mdx.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
import fs from 'node:fs';
import path from 'node:path';
import { parseArgs } from 'node:util';
import { PostFrontMatter } from '@/types/post';
import { getHeadings } from '@/utils/posts';
import { NodeResolvePlugin } from '@esbuild-plugins/node-resolve';
import { globalExternals } from '@fal-works/esbuild-plugin-global-externals';
import mdxESBuild from '@mdx-js/esbuild';
import { transformerColorizedBrackets } from '@shikijs/colorized-brackets';
import rehypeShiki from '@shikijs/rehype';
import {
transformerMetaHighlight,
transformerMetaWordHighlight,
transformerNotationDiff,
} from '@shikijs/transformers';
import { transformerTwoslash } from '@shikijs/twoslash';
import esbuild, { type BuildOptions } from 'esbuild';
import FastGlob from 'fast-glob';
import grayMatter, { type GrayMatterFile } from 'gray-matter';
import remarkFrontmatter from 'remark-frontmatter';
import remarkGfm from 'remark-gfm';
import remarkMdxFrontmatter from 'remark-mdx-frontmatter';
import type { ShikiTransformer } from 'shiki';
const DIST_PATH = path.resolve(process.cwd(), 'dist');
const transformCodeblockTitle = (): ShikiTransformer => {
function parseMetaProps(meta: string) {
if (!meta) return null;
const match = meta.match(/title="([^"]+)"/);
if (!match) return null;
return match[1];
}
return {
name: '@custom/transformers:codeblock-title',
root(hast) {
if (!this.options.meta?.__raw) return hast;
const title = parseMetaProps(this.options.meta.__raw);
if (!title) return hast;
const titleBlock = {
type: 'element',
tagName: 'div',
properties: {
'data-codeblock-title': '',
},
children: [{ type: 'text', value: title }],
};
this.root.children.unshift(
titleBlock as (typeof this.root.children)[number],
);
return hast;
},
};
};
/**
* Based on mdx-bundler's `bundleMDX` function by Kent C. Dodds
* @see https://github.com/kentcdodds/mdx-bundler
*/
function getBuildOptions({ sourcePaths }: { sourcePaths: string[] }) {
const entryPoints = [] as Array<{
entryPath: string;
slug: string;
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
grayMatter: GrayMatterFile<any>;
}>;
for (const source of sourcePaths) {
entryPoints.push({
slug: path.parse(source).dir.split('/').pop()!,
entryPath: source,
grayMatter: grayMatter.read(source),
});
}
const buildOptions: BuildOptions = {
entryPoints: entryPoints.map((entry) => entry.entryPath),
write: false,
outdir: DIST_PATH,
absWorkingDir: process.cwd(),
define: {
'process.env.NODE_ENV': JSON.stringify(
// biome-ignore lint/nursery/noProcessEnv: needed
process.env.NODE_ENV ?? 'production',
),
},
loader: {
'.png': 'dataurl',
'.jpg': 'dataurl',
'.jpeg': 'dataurl',
'.webp': 'dataurl',
},
plugins: [
globalExternals({
react: {
varName: 'React',
type: 'cjs',
},
'react-dom': {
varName: 'ReactDOM',
type: 'cjs',
},
'react/jsx-runtime': {
varName: '_jsx_runtime',
type: 'cjs',
},
}),
NodeResolvePlugin({
extensions: ['.js', '.ts', '.jsx', '.tsx'],
// resolveOptions: { basedir: cwd },
}),
mdxESBuild({
remarkPlugins: [
remarkFrontmatter,
[remarkMdxFrontmatter, { name: 'frontmatter' }],
remarkGfm,
],
rehypePlugins: [
[
rehypeShiki,
{
defaultLanguage: 'plaintext',
inline: 'tailing-curly-colon',
theme: 'vitesse-dark',
transformers: [
transformerTwoslash({
twoslashOptions: {
handbookOptions: {
noErrorValidation: true,
},
},
}),
transformerColorizedBrackets(),
transformCodeblockTitle(),
transformerMetaHighlight(),
transformerMetaWordHighlight(),
transformerNotationDiff({
matchAlgorithm: 'v3',
}),
],
},
],
],
}),
{
name: 'output-mdx-and-metadata',
setup(build) {
build.onStart(() => {
console.info('[mdx] π Building files...');
});
build.onEnd(async (result) => {
if (result.errors.length > 0) {
console.error(result.errors);
throw new Error('[mdx] Failed to bundle MDX content');
}
if (fs.existsSync(DIST_PATH)) {
await fs.promises.rm(DIST_PATH, { recursive: true });
} else {
await fs.promises.mkdir(DIST_PATH, { recursive: true });
}
console.info('[mdx] πΏ Writing files...');
await Promise.all(
result.outputFiles?.map(async (file) => {
const slug = path.parse(file.path).dir.split('/').pop();
if (!slug) {
throw new Error('[mdx] Failed to bundle MDX content');
}
const grayMatter = entryPoints.find(
(entry) => entry.slug === slug,
)?.grayMatter;
if (!grayMatter) {
throw new Error('[mdx] Failed to bundle MDX content');
}
const frontmatter = PostFrontMatter.safeParse(grayMatter?.data);
if (!frontmatter.success) {
throw new Error('[mdx] Failed to bundle MDX content');
}
const metadata = {
...frontmatter.data,
slug,
toc: getHeadings(grayMatter.content),
};
const metadataPath = file.path.replace(/\.js$/, '.json');
await fs.promises.mkdir(path.parse(metadataPath).dir, {
recursive: true,
});
await Promise.all([
fs.promises.writeFile(file.path, file.contents),
fs.promises.writeFile(
metadataPath,
JSON.stringify(metadata, null, 2),
),
]);
}) ?? [],
);
console.info('[mdx] β
Finished building');
});
},
},
],
footer: {
js: 'return Component;',
},
bundle: true,
format: 'iife',
globalName: 'Component',
minify: true,
};
return buildOptions;
}
const { values: cmdArgs } = parseArgs({
options: {
watch: {
type: 'boolean',
short: 'w',
},
},
});
const sourcePaths = FastGlob.globSync(
path.resolve(process.cwd(), './app/contents/**/index.mdx'),
);
if (cmdArgs.watch) {
const ctx = await esbuild.context(getBuildOptions({ sourcePaths }));
await ctx.watch();
} else {
await esbuild.build(getBuildOptions({ sourcePaths }));
}