File size: 2,173 Bytes
1e92f2d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | /**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { compile, CompileOptions } from '@mdx-js/mdx'
import { VFile, VFileCompatible } from 'vfile'
import { matter } from 'vfile-matter'
import { createFormattedMDXError } from './format-mdx-error.js'
import { removeImportsExportsPlugin } from './plugins/remove-imports-exports.js'
// types
import type { MDXRemoteSerializeResult, SerializeOptions } from './types.js'
function getCompileOptions(
mdxOptions: SerializeOptions['mdxOptions'] = {},
rsc: boolean = false
): CompileOptions {
const areImportsEnabled = mdxOptions.useDynamicImport ?? false
// don't modify the original object when adding our own plugin
// this allows code to reuse the same options object
const remarkPlugins = [
...(mdxOptions.remarkPlugins || []),
...(areImportsEnabled ? [] : [removeImportsExportsPlugin]),
]
return {
...mdxOptions,
remarkPlugins,
outputFormat: 'function-body',
// Disable the importSource option for RSC to ensure there's no `useMDXComponents` implemented.
providerImportSource: rsc ? undefined : '@mdx-js/react',
development: process.env.NODE_ENV !== 'production',
}
}
/**
* Parses and compiles the provided MDX string. Returns a result which can be passed into <MDXRemote /> to be rendered.
*/
export async function serialize<
TScope = Record<string, unknown>,
TFrontmatter = Record<string, unknown>
>(
source: VFileCompatible,
{
scope = {},
mdxOptions = {},
parseFrontmatter = false,
}: SerializeOptions = {},
rsc: boolean = false
): Promise<MDXRemoteSerializeResult<TScope, TFrontmatter>> {
const vfile = new VFile(source)
// makes frontmatter available via vfile.data.matter
if (parseFrontmatter) {
matter(vfile, { strip: true })
}
let compiledMdx: VFile
try {
compiledMdx = await compile(vfile, getCompileOptions(mdxOptions, rsc))
} catch (error: any) {
throw createFormattedMDXError(error, String(vfile))
}
let compiledSource = String(compiledMdx)
return {
compiledSource,
frontmatter: (vfile.data.matter ?? {}) as TFrontmatter,
scope: scope as TScope,
}
}
|