-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmarkdown.tsx
More file actions
42 lines (36 loc) · 1.08 KB
/
markdown.tsx
File metadata and controls
42 lines (36 loc) · 1.08 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
import fs from "fs-extra";
import matter from "gray-matter";
import { MDXRemoteSerializeResult } from "next-mdx-remote";
import { serialize } from "next-mdx-remote/serialize";
import remarkMath from "remark-math";
import rehypeKatex from "rehype-katex";
import loadCodePlugin from "../remarkPlugins/loadCode";
import extractImages from "../remarkPlugins/extractImages";
import { MarkdownPageProps } from "./markdown-page";
export async function serializeMarkdownDocument(
doc: string,
filepath?: string
): Promise<MDXRemoteSerializeResult> {
return serialize(doc, {
mdxOptions: {
remarkPlugins: [loadCodePlugin, extractImages, remarkMath],
rehypePlugins: [rehypeKatex],
filepath,
},
});
}
export async function getPropsForMarkdownFile(
filePath: string
): Promise<MarkdownPageProps> {
const rawContents = await fs.readFile(filePath, "utf8");
const {
content,
data: { title = "NO TITLE", summary = null },
} = matter(rawContents);
const source = await serializeMarkdownDocument(content, filePath);
return {
source,
title,
summary,
};
}