|
| 1 | +import * as path from 'path'; |
| 2 | +import * as fs from 'node:fs/promises'; |
| 3 | +import { evaluate } from '@mdx-js/mdx'; |
| 4 | +import * as jsxRuntime from 'react/jsx-runtime'; |
| 5 | +import remarkFrontmatter from 'remark-frontmatter'; |
| 6 | +import remarkMdxFrontmatter from 'remark-mdx-frontmatter'; |
| 7 | +import remarkGfm from 'remark-gfm'; |
| 8 | +import rehypeSlug from 'rehype-slug'; |
| 9 | +import extractToc, { type Toc } from '@stefanprobst/rehype-extract-toc'; |
| 10 | +import exportToc from '@stefanprobst/rehype-extract-toc/mdx'; |
| 11 | +import { read as readVFile } from 'to-vfile'; |
| 12 | +import { matter } from 'vfile-matter'; |
| 13 | + |
| 14 | +export interface PageMetadata { |
| 15 | + title: string; |
| 16 | + description: string; |
| 17 | + components?: string; |
| 18 | + githubLabel?: string; |
| 19 | + slug: string; |
| 20 | +} |
| 21 | + |
| 22 | +async function getFileHandle(basePath: string, slug: string) { |
| 23 | + const mdxFilePath = path.join(process.env.DATA_DIR as string, basePath, slug, `${slug}.mdx`); |
| 24 | + const mdFilePath = path.join(process.env.DATA_DIR as string, basePath, slug, `${slug}.md`); |
| 25 | + |
| 26 | + try { |
| 27 | + const fileHandle = await fs.open(mdxFilePath); |
| 28 | + return { |
| 29 | + handle: fileHandle, |
| 30 | + path: mdxFilePath, |
| 31 | + [Symbol.asyncDispose]: async () => { |
| 32 | + await fileHandle.close(); |
| 33 | + }, |
| 34 | + }; |
| 35 | + } catch (ex1) { |
| 36 | + try { |
| 37 | + const fileHandle = await fs.open(mdFilePath); |
| 38 | + return { |
| 39 | + handle: fileHandle, |
| 40 | + path: mdFilePath, |
| 41 | + [Symbol.asyncDispose]: async () => { |
| 42 | + await fileHandle.close(); |
| 43 | + }, |
| 44 | + }; |
| 45 | + } catch (ex2) { |
| 46 | + throw new Error('404'); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +export async function getMarkdownPage(basePath: string, slug: string) { |
| 52 | + await using file = await getFileHandle(basePath, slug); |
| 53 | + const mdxSource = await file.handle.readFile({ |
| 54 | + encoding: 'utf-8', |
| 55 | + }); |
| 56 | + const { |
| 57 | + default: MDXContent, |
| 58 | + frontmatter, |
| 59 | + tableOfContents, |
| 60 | + } = await evaluate(mdxSource, { |
| 61 | + ...jsxRuntime, |
| 62 | + remarkPlugins: [remarkGfm, remarkFrontmatter, remarkMdxFrontmatter], |
| 63 | + rehypePlugins: [ |
| 64 | + // [rehypePrettyCode, { theme: config.shikiThemes }], |
| 65 | + rehypeSlug, |
| 66 | + extractToc, |
| 67 | + exportToc, |
| 68 | + ], |
| 69 | + }); |
| 70 | + |
| 71 | + return { |
| 72 | + metadata: { |
| 73 | + ...(frontmatter as Partial<PageMetadata>), |
| 74 | + slug, |
| 75 | + } as PageMetadata, |
| 76 | + tableOfContents: tableOfContents as Toc, |
| 77 | + MDXContent, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +export async function getMarkdownPageMetadata(basePath: string, slug: string) { |
| 82 | + await using file = await getFileHandle(basePath, slug); |
| 83 | + |
| 84 | + const vfile = await readVFile(file.path); |
| 85 | + matter(vfile); |
| 86 | + return vfile.data.matter as PageMetadata; |
| 87 | +} |
0 commit comments