-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
mdx.ts
259 lines (228 loc) 路 7.19 KB
/
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { bundleMDX } from "mdx-bundler";
import fs from "fs";
import matter from "gray-matter";
import path from "path";
import { getAllFilesRecursively, slugify } from "_utils/file_helpers";
import { remarkCodeSandboxURLUpdater } from "./remarkCodeSandboxURLUpdater";
// Remark packages
import remarkGfm from "remark-gfm";
import oembedTransformer from "@remark-embedder/transformer-oembed";
import remarkEmbedder from "@remark-embedder/core";
import type { TransformerInfo } from "@remark-embedder/core";
import { remarkMdxImages } from "remark-mdx-images";
// Rehype packages
import rehypePrismPlus from "rehype-prism-plus";
import rehypeSlug from "rehype-slug";
import rehypeAutolinkHeadings from "rehype-autolink-headings";
import type * as U from "unified";
const root = path.resolve(process.cwd(), "../");
const docsPath = path.join(root, "docs");
function handleEmbedderError({ url }: { url: string }) {
return `<p>Error embedding <a href="${url}">${url}</a>.`;
}
type GottenHTML = string | null;
function handleEmbedderHtml(html: GottenHTML, info: TransformerInfo) {
if (!html) return null;
const url = new URL(info.url);
// matches youtu.be and youtube.com
if (/youtu\.?be/.test(url.hostname)) {
// this allows us to set youtube embeds to 100% width and the
// height will be relative to that width with a good aspect ratio
return makeEmbed(html, "youtube");
}
if (url.hostname.includes("codesandbox.io")) {
return makeEmbed(html, "codesandbox");
}
return html;
}
function makeEmbed(html: string, type: string) {
return `
<div class="embed" data-embed-type="${type}">
<div style="padding-bottom: 18px;">
${html}
</div>
</div>
`;
}
const remarkPlugins: U.PluggableList = [
[
// @ts-expect-error
remarkEmbedder,
{
handleError: handleEmbedderError,
handleHTML: handleEmbedderHtml,
transformers: [oembedTransformer],
},
],
];
export function getAllDocs() {
const files = getAllFilesRecursively(docsPath);
// Only want to return docs/path and ignore root, replace is needed to work on Windows
return files.map((file) =>
file.slice(docsPath.length + 1).replace(/\\/g, "/")
);
}
export function formatSlug(slug: string) {
return slug.replace(/\.(mdx|md)/, "");
}
export function getSlugs(p: string) {
return formatSlug(p).split("/").map(slugify);
}
export function dateSortDesc(a: any, b: any) {
if (a > b) return -1;
if (a < b) return 1;
return 0;
}
function getSourceFromSlug(slug: string) {
const mdxPath = path.join(docsPath, slug);
return fs.readFileSync(mdxPath, "utf8");
}
export async function getDocBySlug(slug: string) {
const mdxPath = path.join(docsPath, `${slug}.mdx`);
const mdPath = path.join(docsPath, `${slug}.md`);
const source = fs.existsSync(mdxPath)
? fs.readFileSync(mdxPath, "utf8")
: fs.readFileSync(mdPath, "utf8");
// https://github.com/kentcdodds/mdx-bundler#nextjs-esbuild-enoent
if (process.platform === "win32") {
process.env.ESBUILD_BINARY_PATH = path.join(
root,
"node_modules",
"esbuild",
"esbuild.exe"
);
} else {
process.env.ESBUILD_BINARY_PATH = path.join(
root,
"node_modules",
"esbuild",
"bin",
"esbuild"
);
}
// Parsing frontmatter here to pass it in as options to rehype plugin
const { data: frontmatter } = matter(source);
const cwd = path.dirname(mdxPath);
const { code } = await bundleMDX({
source,
// mdx imports can be automatically source from the components directory
// cwd: path.join(root, "components"),
cwd,
xdmOptions(options) {
// this is the recommended way to add custom remark/rehype plugins:
// The syntax might look weird, but it protects you in case we add/remove
// plugins in the future.
options.remarkPlugins = [
...(options.remarkPlugins ?? []),
remarkCodeSandboxURLUpdater,
rehypeSlug,
[rehypeAutolinkHeadings, { behavior: "wrap" }],
remarkGfm,
remarkMdxImages,
...remarkPlugins,
];
options.rehypePlugins = [
...(options.rehypePlugins ?? []),
rehypeSlug,
rehypeAutolinkHeadings,
[rehypePrismPlus, { ignoreMissing: true }],
];
return options;
},
esbuildOptions: (options) => {
options.loader = {
...options.loader,
".js": "jsx",
".ts": "tsx",
".svg": "dataurl",
".png": "dataurl",
};
options.outdir = path.join(root, "build");
// Set the public path to /img
options.publicPath = "/docs/img";
// Set write to true so that esbuild will output the files.
options.write = true;
return options;
},
});
return {
mdxSource: code,
frontMatter: {
slug: slug || null,
fileName: fs.existsSync(mdxPath) ? `${slug}.mdx` : `${slug}.md`,
...frontmatter,
date: frontmatter.date ? new Date(frontmatter.date).toISOString() : null,
},
};
}
export async function getAllFilesFrontMatter(folder: string) {
const prefixPaths = path.join(docsPath, folder);
const files = getAllFilesRecursively(prefixPaths);
const allFrontMatter: any[] = [];
files.forEach((file) => {
// Replace is needed to work on Windows
const fileName = file.slice(prefixPaths.length + 1).replace(/\\/g, "/");
// Remove Unexpected File
if (path.extname(fileName) !== ".md" && path.extname(fileName) !== ".mdx") {
return;
}
const source = fs.readFileSync(file, "utf8");
const { data: frontmatter } = matter(source);
if (frontmatter.draft !== true) {
allFrontMatter.push({
...frontmatter,
slug: getSlugs(fileName),
date: frontmatter.date
? new Date(frontmatter.date).toISOString()
: null,
});
}
});
return allFrontMatter.sort((a, b) => dateSortDesc(a.date, b.date));
}
const removeExtension = (path: string) => {
return path.replace(/\.[^/.]+$/, "");
};
const getTitle = (path: string) => {
return removeExtension(path.split("/").pop() || "");
};
function prepareDoc(doc: string) {
const slugs = getSlugs(doc);
const href = `/docs/${slugs.map(slugify).join("/")}`;
const source = getSourceFromSlug(doc);
const { data: frontmatter } = matter(source);
const title = frontmatter.title ?? getTitle(doc);
// console.log(frontmatter);
return {
title,
href,
slug: slugs[slugs.length - 1],
};
}
export function getDocsMap(): Record<string, Navigation> {
const docs = getAllDocs();
return docs.reduce((acc, d) => {
const doc = prepareDoc(d);
return { ...acc, [doc.slug]: doc as Navigation };
}, {});
}
export function getDocsNav(): Record<string, Navigation[]> {
const pages = getDocsMap();
return {
Introduction: [pages["getting-started"]],
Basic: [pages["proxy"], pages["useSnapshot"]],
Advanced: [pages["ref"], pages["subscribe"], pages["snapshot"]],
Utils: [
pages["subscribeKey"],
pages["watch"],
pages["devtools"],
pages["derive"],
pages["proxyWithComputed"],
pages["proxyWithHistory"],
pages["proxySet"],
pages["proxyMap"],
],
Hacks: [pages["getVersion"], pages["unstable_buildProxyFunction"]],
Resources: [pages["community"], pages["libraries"], pages["learn"]],
};
}