1
0
Fork 0
freesewing/sites/shared/mdx/compile.mjs

115 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-12-18 09:54:53 +01:00
import path from 'path'
// MDX compiler & runner
import * as runtime from 'react/jsx-runtime'
import { compile, run } from '@mdx-js/mdx'
// Remark plugins from the ecosystem
import remarkFrontmatter from 'remark-frontmatter'
import remarkMdxFrontmatter from 'remark-mdx-frontmatter'
import remarkGfm from 'remark-gfm'
2021-12-25 13:43:41 +01:00
import remarkCopyLinkedFiles from 'remark-copy-linked-files'
import smartypants from 'remark-smartypants'
// FreeSewing custom remark plugins
import { remarkIntroAsFrontmatter } from './remark-intro-as-frontmatter.mjs'
import { remarkTocAsFrontmatter } from './remark-toc-as-frontmatter.mjs'
import { remarkGithubImages } from './remark-github-images.mjs'
// Rehype plugins from the ecosystem
import rehypeHighlight from 'rehype-highlight'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypeSlug from 'rehype-slug'
import rehypeHighlightLines from 'pkgs/rehype-highlight-lines/src/index.mjs'
2021-12-18 09:54:53 +01:00
/*
2023-10-07 21:19:38 +02:00
* Compiles markdown/mdx to a function body
2021-12-18 09:54:53 +01:00
*/
2023-10-07 21:19:38 +02:00
export const compileMdx = async ({
md, // A string holding the markdown
site, // The site folder, one of 'org' or 'dev'
slug, // The slug to the page below the folder (like 'guides/plugins')
fromGithub = false, // Set this to true when dynamically loading mdx from Github
2023-10-07 21:19:38 +02:00
}) => {
2021-12-18 09:54:53 +01:00
const mdx = String(
await compile(md, {
outputFormat: 'function-body',
2023-10-30 17:46:39 +01:00
baseUrl: import.meta.url,
development: false,
remarkPlugins: [
remarkFrontmatter,
remarkMdxFrontmatter,
remarkGfm,
smartypants,
fromGithub
? remarkGithubImages
: [
remarkCopyLinkedFiles,
{
destinationDir: path.resolve(`../${site}/public/mdx`),
sourceDir: path.resolve(`../../markdown/${site}/${slug}`),
staticPath: '/mdx/',
},
],
remarkTocAsFrontmatter,
2023-10-07 21:19:38 +02:00
remarkIntroAsFrontmatter,
],
rehypePlugins: [
[
rehypeHighlight,
{
plainText: ['dot', 'http', 'mermaid'],
aliases: {
2022-10-09 23:48:30 +02:00
javascript: [
'design/src/index.mjs',
'design/src/part.mjs',
2022-10-09 23:48:30 +02:00
'design/src/bib.mjs',
'index.mjs',
'part.mjs',
'bib.mjs',
],
2022-11-19 18:11:04 +01:00
json: [
'200.json',
'201.json',
'204.json',
'400.json',
'401.json',
'403.json',
'404.json',
'500.json',
],
markdown: ['en.md'],
},
},
],
[
rehypeHighlightLines,
{
highlightClass: ['highlight-lines', 'border-l-4'],
strikeoutClass: [
'strikeout-lines',
'bg-orange-300',
'bg-opacity-5',
'border-l-4',
'opacity-80',
'line-through',
'decoration-orange-500',
],
},
],
rehypeSlug,
[
rehypeAutolinkHeadings,
{
behavior: 'wrap',
properties: { className: 'heading-autolink' },
},
],
],
})
2021-12-18 09:54:53 +01:00
)
/*
* Pull frontmatter out of mdx content
*/
const { frontmatter } = await run(mdx, runtime)
return { mdx, frontmatter }
2021-12-18 09:54:53 +01:00
}