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, + } + +}