From 634281b3f4a4a3c93fd69e9c0bda634ae128c13d Mon Sep 17 00:00:00 2001 From: andwati Date: Sun, 1 Feb 2026 19:38:11 +0300 Subject: [PATCH] Add MDX content handling with serialization and file reading --- lib/book.ts | 0 lib/mdx.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 lib/book.ts create mode 100644 lib/mdx.ts diff --git a/lib/book.ts b/lib/book.ts new file mode 100644 index 0000000..e69de29 diff --git a/lib/mdx.ts b/lib/mdx.ts new file mode 100644 index 0000000..0ed0071 --- /dev/null +++ b/lib/mdx.ts @@ -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 { + metadata: TMetadata; + content: string; +} + +export type MaybeContent = Content | undefined; + +export async function getMdxContent( + ...paths: string[] +): Promise> { + 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, + } + +}