Add MDX content handling with serialization and file reading

This commit is contained in:
2026-02-01 19:38:11 +03:00
parent 6b4d8484f8
commit 634281b3f4
2 changed files with 42 additions and 0 deletions
View File
+42
View File
@@ -0,0 +1,42 @@
import { serialize } from "next-mdx-remote/serialize";
import path from "path";
import fs from "fs";
import remarkGfm from "remark-gfm";
export interface Content<TMetadata = { [key: string]: any }> {
metadata: TMetadata;
content: string;
}
export type MaybeContent<TMetadata> = Content<TMetadata> | undefined;
export async function getMdxContent<TMetadata>(
...paths: string[]
): Promise<MaybeContent<TMetadata>> {
const contentPath = path.join(process.cwd(), "content", ...paths);
if (!fs.existsSync(contentPath)) {
return undefined;
}
const content = fs.readFileSync(contentPath, "utf-8");
const source = await serialize(content, {
parseFrontmatter: true,
mdxOptions: {
remarkPlugins: [remarkGfm],
development: false,
},
});
const footnotes = /_jsx\(_components.h2, {.*?children: "Footnotes".*?}\), "\\n", /gs;
const hr = ``
const compiledSource = source.compiledSource.replace(footnotes, hr);
return{
metadata: source.frontmatter as TMetadata,
content: compiledSource,
}
}