1
0
Fork 0
freesewing/packages/freesewing.shared/mdx/loader.js

57 lines
1.4 KiB
JavaScript
Raw Normal View History

2021-12-18 09:54:53 +01:00
// 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'
// Remark plugins we want to use
import remarkFrontmatter from 'remark-frontmatter'
import remarkGfm from 'remark-gfm'
2021-12-25 13:43:41 +01:00
import remarkCopyLinkedFiles from 'remark-copy-linked-files'
// Rehype plugins we want to use
import rehypeHighlight from 'rehype-highlight'
2021-12-18 09:54:53 +01:00
/*
* 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',
remarkPlugins: [
remarkFrontmatter,
remarkGfm,
2021-12-25 13:43:41 +01:00
[
remarkCopyLinkedFiles,
{
destinationDir: path.resolve(`../freesewing.${site}/public/mdx`),
sourceDir: path.resolve(`../../markdown/${site}/${slug}`),
staticPath: '/mdx/',
}
]
],
rehypePlugins: [
rehypeHighlight
],
})
2021-12-18 09:54:53 +01:00
)
return mdx
}
export default mdxLoader