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'
|
|
|
|
|
2021-12-18 15:41:37 +01:00
|
|
|
// 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'
|
2021-12-28 20:01:59 +01:00
|
|
|
import { remarkIntroPlugin } from './remark-intro-plugin.mjs'
|
2022-05-11 16:29:46 +02:00
|
|
|
import mdxPluginToc from './mdx-plugin-toc.mjs'
|
2022-02-05 17:45:23 +01:00
|
|
|
import smartypants from 'remark-smartypants'
|
2021-12-18 15:41:37 +01:00
|
|
|
// Rehype plugins we want to use
|
|
|
|
import rehypeHighlight from 'rehype-highlight'
|
2022-02-05 17:45:23 +01:00
|
|
|
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
|
|
|
|
import rehypeSlug from 'rehype-slug'
|
2022-08-25 13:35:08 +02:00
|
|
|
import rehypeJargon from 'pkgs/rehype-jargon/src/index.mjs'
|
2022-05-29 13:19:02 +02:00
|
|
|
// Simple frontmatter extractor
|
|
|
|
import frontmatter from 'front-matter'
|
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/
|
|
|
|
*
|
|
|
|
*/
|
2022-07-17 21:25:41 +02:00
|
|
|
|
|
|
|
const jargonTransform = (term, html) => `<details class="inline jargon-details">
|
2022-07-18 18:50:34 +02:00
|
|
|
<summary class="jargon-term">
|
|
|
|
${term}
|
|
|
|
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 jargon-close" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
|
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="4" d="M6 18L18 6M6 6l12 12" />
|
|
|
|
</svg>
|
|
|
|
</summary>
|
2022-07-17 21:25:41 +02:00
|
|
|
<div class="jargon-info">
|
|
|
|
${html}</div></details>`
|
|
|
|
|
|
|
|
const mdxLoader = async (language, site, slug, jargon) => {
|
2021-12-18 09:54:53 +01:00
|
|
|
// TODO: Will this work on Windows?
|
|
|
|
const md = await fs.promises.readFile(
|
|
|
|
path.resolve(`../../markdown/${site}/${slug}/${language}.md`),
|
|
|
|
'utf-8'
|
|
|
|
)
|
2021-12-28 20:01:59 +01:00
|
|
|
const intro = []
|
2021-12-18 09:54:53 +01:00
|
|
|
const mdx = String(
|
2021-12-18 15:41:37 +01:00
|
|
|
await compile(md, {
|
|
|
|
outputFormat: 'function-body',
|
|
|
|
remarkPlugins: [
|
|
|
|
remarkFrontmatter,
|
|
|
|
remarkGfm,
|
2022-02-05 17:45:23 +01:00
|
|
|
smartypants,
|
2021-12-25 13:43:41 +01:00
|
|
|
[
|
|
|
|
remarkCopyLinkedFiles,
|
|
|
|
{
|
2022-06-17 12:02:09 +02:00
|
|
|
destinationDir: path.resolve(`../${site}/public/mdx`),
|
2021-12-25 13:43:41 +01:00
|
|
|
sourceDir: path.resolve(`../../markdown/${site}/${slug}`),
|
|
|
|
staticPath: '/mdx/',
|
2022-10-05 15:25:24 +02:00
|
|
|
},
|
2021-12-28 20:01:59 +01:00
|
|
|
],
|
2022-10-05 15:25:24 +02:00
|
|
|
[remarkIntroPlugin, { intro }],
|
2021-12-18 15:41:37 +01:00
|
|
|
],
|
|
|
|
rehypePlugins: [
|
2022-07-17 21:25:41 +02:00
|
|
|
[rehypeJargon, { jargon, transform: jargonTransform }],
|
2022-10-05 15:25:24 +02:00
|
|
|
[
|
|
|
|
rehypeHighlight,
|
|
|
|
{
|
|
|
|
plainText: ['dot', 'http'],
|
|
|
|
aliases: {
|
2022-10-09 23:48:30 +02:00
|
|
|
javascript: [
|
|
|
|
'design/src/index.mjs',
|
|
|
|
'design/src/bib.mjs',
|
|
|
|
'index.mjs',
|
|
|
|
'part.mjs',
|
|
|
|
'bib.mjs',
|
|
|
|
],
|
2022-10-05 15:25:24 +02:00
|
|
|
markdown: ['en.md'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2022-02-05 17:45:23 +01:00
|
|
|
rehypeSlug,
|
2022-10-05 15:25:24 +02:00
|
|
|
[
|
|
|
|
rehypeAutolinkHeadings,
|
|
|
|
{
|
|
|
|
behavior: 'wrap',
|
|
|
|
properties: { className: 'heading-autolink' },
|
|
|
|
},
|
|
|
|
],
|
2021-12-18 15:41:37 +01:00
|
|
|
],
|
|
|
|
})
|
2021-12-18 09:54:53 +01:00
|
|
|
)
|
|
|
|
|
2022-05-11 16:29:46 +02:00
|
|
|
// This is not ideal as we're adding a second pass but for now it will do
|
|
|
|
// See: https://github.com/remarkjs/remark-toc/issues/37
|
|
|
|
const toc = String(
|
|
|
|
await compile(md, {
|
|
|
|
outputFormat: 'function-body',
|
2022-10-05 15:25:24 +02:00
|
|
|
remarkPlugins: [remarkFrontmatter, remarkGfm, smartypants, [mdxPluginToc, { language }]],
|
|
|
|
rehypePlugins: [rehypeSlug],
|
2022-05-11 16:29:46 +02:00
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2022-05-29 13:19:02 +02:00
|
|
|
return { mdx, intro, toc, frontmatter: frontmatter(md)?.attributes }
|
2021-12-18 09:54:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default mdxLoader
|