1
0
Fork 0

feat(fs.dev): Loading MDX is now ok

This commit is contained in:
Joost De Cock 2021-12-18 09:54:53 +01:00
parent 092c81f535
commit c5e971e8a7
11 changed files with 231 additions and 49 deletions

View file

@ -0,0 +1,33 @@
// We need fs and path to read from disk
import fs from 'fs'
import path from 'path'
// MDX compiler
import { compile } from '@mdx-js/mdx'
/*
* Summary: Loads markdown from disk and compiles it as MDX.
*
* @param (string) language - language to load (eg: 'en')
* @param (string) site - site to load (either 'dev' or 'org')
* @param (string) slug - slug of the page (eg: 'guides/patterns')
*
* @link https://mdxjs.com/guides/mdx-on-demand/
*
*/
const mdxLoader = async (language, site, slug) => {
// TODO: Will this work on Windows?
const md = await fs.promises.readFile(
path.resolve(`../../markdown/${site}/${slug}/${language}.md`),
'utf-8'
)
const mdx = String(
await compile(md, { outputFormat: 'function-body' })
)
return mdx
}
export default mdxLoader