Repository navigation
Expand file tree
/
Copy pathcontent.server.ts
More file actions
121 lines (111 loc) 路 3.01 KB
/
Copy pathcontent.server.ts
File metadata and controls
121 lines (111 loc) 路 3.01 KB
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
import { bundleMDX } from "mdx-bundler";
import * as fs from "fs";
import * as path from "path";
import glob from "glob";
import matter from "gray-matter";
import rehypePrettyCode from "rehype-pretty-code";
import { getId } from "./utils";
const CONTENT_FOLDER = `${process.cwd()}/_dist-content`;
const POST_FILENAME = "index.mdx";
export type Heading = {
id: string;
text: string;
};
export type Post = {
code: string;
frontmatter: Record<string, any>;
slug: string;
headings: Heading[];
};
export type PostMetadata = {
frontmatter: Record<string, any>;
slug: string;
};
const theme = {
light: JSON.parse(
fs.readFileSync(`${process.cwd()}/assets/light-colorblind.json`, "utf8")
),
dark: JSON.parse(
fs.readFileSync(`${process.cwd()}/assets/dark-default.json`, "utf8")
),
};
/**
* Gets the post from `dist/content` and bundles it using MDX bundler
*/
export const getPost = async (slug: string): Promise<Post> => {
const postFolder = path.join(CONTENT_FOLDER, slug);
const mdxSource = (
await fs.promises.readFile(path.join(postFolder, POST_FILENAME))
).toString();
const mdxOut = await bundleMDX({
source: mdxSource,
cwd: postFolder,
mdxOptions(options) {
/* options.rehypePlugins = [
...(options.rehypePlugins ?? []),
[
rehypePrettyCode,
{
theme,
onVisitHighlightedLine(node) {
// Each line node by default has `class="line"`.
node.properties.className.push("highlighted");
},
},
],
]; */
return options;
},
esbuildOptions(options) {
/**
* Babel saves React files with a .js extension, so we have to explicitly tell
* esbuild to parse these files assuming it contains JSX
*/
options.loader = {
".js": "jsx",
};
options.tsconfig = `${process.cwd()}/tsconfig-esbuild.json`;
return options;
},
});
return { ...mdxOut, slug, headings: getHeadings(mdxSource) };
};
const getHeadings = (content: string): Heading[] => {
return (
content
.split("\n")
/* I only care about h2s, so explicitly look for 2 hashtags */
.filter((line) => line.match(/^##\s/))
.map((line) => line.replace("##", "").trim())
.map((text) => ({
text,
id: getId(text),
}))
);
};
export const getAllPosts = async (): Promise<PostMetadata[]> => {
const paths = await getAllPostPaths();
return Promise.all(
paths.map(async (postPath) => {
const file = await fs.promises.readFile(
path.join(process.cwd(), postPath)
);
const { data: frontmatter } = matter(file.toString());
return {
slug: postPath.replace("content/", "").replace("/index.mdx", ""),
frontmatter,
};
})
);
};
const getAllPostPaths = () => {
return new Promise<string[]>((resolve, reject) => {
glob("content/**/index.mdx", (err, matches) => {
if (err) {
reject(err);
} else {
resolve(matches);
}
});
});
};