-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.mjs
45 lines (40 loc) · 1.53 KB
/
content.mjs
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
import { join } from "path";
import fs from "fs";
import matter from "gray-matter";
import { remark } from "remark";
import html from "remark-html";
import { compile } from "@rspress/mdx-rs";
const postsDirectory = join(process.cwd(), "content");
export function getPostSlugs() {
return fs.readdirSync(postsDirectory);
}
export async function getPostBySlug(filename) {
const fullPath = join(postsDirectory, filename);
const realSlug = filename.replace(/\.mdx?$/, "");
const fileContents = fs.readFileSync(fullPath, "utf8");
const result = await compile({
// The mdx content
value: fileContents,
// File path of the mdx file, the compiler will determine the different syntax(md/mdx) based on the file extension
filepath: fullPath,
// Whether to enable development mode, default is false
development: process.env.NODE_ENV === "development",
// Current working directory, can be empty string
root: "",
});
const frontMatterInfo = JSON.parse(result.frontmatter);
return { links: result.links, html: result.html, ...frontMatterInfo };
}
getPostBySlug("test.mdx");
// export function getAllPosts(): Post[] {
// const slugs = getPostSlugs();
// const posts = slugs
// .map((slug) => getPostBySlug(slug))
// // sort posts by date in descending order
// .sort((post1, post2) => (post1.date > post2.date ? -1 : 1));
// return posts;
// }
// export default async function markdownToHtml(markdown: string) {
// const result = await remark().use(html).process(markdown);
// return result.toString();
// }