refactor: use mdx-bundler frontmatter

This commit is contained in:
Timothy Lin 2021-07-04 15:03:52 +08:00
parent 0aab8b55cf
commit b9ad2a8145

View File

@ -48,8 +48,6 @@ export async function getFileBySlug(type, slug) {
? fs.readFileSync(mdxPath, 'utf8') ? fs.readFileSync(mdxPath, 'utf8')
: fs.readFileSync(mdPath, 'utf8') : fs.readFileSync(mdPath, 'utf8')
const { data, content } = matter(source)
// https://github.com/kentcdodds/mdx-bundler#nextjs-esbuild-enoent // https://github.com/kentcdodds/mdx-bundler#nextjs-esbuild-enoent
if (process.platform === 'win32') { if (process.platform === 'win32') {
process.env.ESBUILD_BINARY_PATH = path.join( process.env.ESBUILD_BINARY_PATH = path.join(
@ -68,7 +66,7 @@ export async function getFileBySlug(type, slug) {
) )
} }
const { code } = await bundleMDX(source, { const { frontmatter, code } = await bundleMDX(source, {
// mdx imports can be automatically source from the components directory // mdx imports can be automatically source from the components directory
cwd: path.join(process.cwd(), 'components'), cwd: path.join(process.cwd(), 'components'),
xdmOptions(options) { xdmOptions(options) {
@ -114,10 +112,10 @@ export async function getFileBySlug(type, slug) {
return { return {
mdxSource: code, mdxSource: code,
frontMatter: { frontMatter: {
readingTime: readingTime(content), readingTime: readingTime(code),
slug: slug || null, slug: slug || null,
fileName: fs.existsSync(mdxPath) ? `${slug}.mdx` : `${slug}.md`, fileName: fs.existsSync(mdxPath) ? `${slug}.mdx` : `${slug}.md`,
...data, ...frontmatter,
}, },
} }
} }