diff options
Diffstat (limited to 'packages/integrations/mdx/src')
-rw-r--r-- | packages/integrations/mdx/src/astro-data-utils.ts | 32 | ||||
-rw-r--r-- | packages/integrations/mdx/src/index.ts | 13 |
2 files changed, 5 insertions, 40 deletions
diff --git a/packages/integrations/mdx/src/astro-data-utils.ts b/packages/integrations/mdx/src/astro-data-utils.ts index 3300c7b55..7c7f6119b 100644 --- a/packages/integrations/mdx/src/astro-data-utils.ts +++ b/packages/integrations/mdx/src/astro-data-utils.ts @@ -1,6 +1,4 @@ import type { MarkdownAstroData } from 'astro'; -import { name as isValidIdentifierName } from 'estree-util-is-identifier-name'; -import type { MdxjsEsm } from 'mdast-util-mdx'; import type { Data, VFile } from 'vfile'; import { jsToTreeNode } from './utils.js'; @@ -12,35 +10,13 @@ export function remarkInitializeAstroData() { }; } -export function rehypeApplyFrontmatterExport( - pageFrontmatter: Record<string, any>, - exportName = 'frontmatter' -) { +const EXPORT_NAME = 'frontmatter'; + +export function rehypeApplyFrontmatterExport(pageFrontmatter: Record<string, any>) { return function (tree: any, vfile: VFile) { - if (!isValidIdentifierName(exportName)) { - throw new Error( - `[MDX] ${JSON.stringify( - exportName - )} is not a valid frontmatter export name! Make sure "frontmatterOptions.name" could be used as a JS export (i.e. "export const frontmatterName = ...")` - ); - } const { frontmatter: injectedFrontmatter } = safelyGetAstroData(vfile.data); const frontmatter = { ...injectedFrontmatter, ...pageFrontmatter }; - let exportNodes: MdxjsEsm[] = []; - if (!exportName) { - exportNodes = Object.entries(frontmatter).map(([k, v]) => { - if (!isValidIdentifierName(k)) { - throw new Error( - `[MDX] A remark or rehype plugin tried to inject ${JSON.stringify( - k - )} as a top-level export, which is not a valid export name.` - ); - } - return jsToTreeNode(`export const ${k} = ${JSON.stringify(v)};`); - }); - } else { - exportNodes = [jsToTreeNode(`export const ${exportName} = ${JSON.stringify(frontmatter)};`)]; - } + const exportNodes = [jsToTreeNode(`export const ${EXPORT_NAME} = ${JSON.stringify(frontmatter)};`)]; tree.children = exportNodes.concat(tree.children); }; } diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index 2c7247237..d88e67016 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -4,7 +4,6 @@ import type { AstroConfig, AstroIntegration } from 'astro'; import { parse as parseESM } from 'es-module-lexer'; import rehypeRaw from 'rehype-raw'; import remarkGfm from 'remark-gfm'; -import type { RemarkMdxFrontmatterOptions } from 'remark-mdx-frontmatter'; import remarkShikiTwoslash from 'remark-shiki-twoslash'; import remarkSmartypants from 'remark-smartypants'; import { VFile } from 'vfile'; @@ -19,12 +18,6 @@ type WithExtends<T> = T | { extends: T }; type MdxOptions = { remarkPlugins?: WithExtends<MdxRollupPluginOptions['remarkPlugins']>; rehypePlugins?: WithExtends<MdxRollupPluginOptions['rehypePlugins']>; - /** - * Configure the remark-mdx-frontmatter plugin - * @see https://github.com/remcohaszing/remark-mdx-frontmatter#options for a full list of options - * @default {{ name: 'frontmatter' }} - */ - frontmatterOptions?: RemarkMdxFrontmatterOptions; }; const DEFAULT_REMARK_PLUGINS: MdxRollupPluginOptions['remarkPlugins'] = [ @@ -119,11 +112,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration { ...mdxPluginOpts, rehypePlugins: [ ...(mdxPluginOpts.rehypePlugins ?? []), - () => - rehypeApplyFrontmatterExport( - frontmatter, - mdxOptions.frontmatterOptions?.name - ), + () => rehypeApplyFrontmatterExport(frontmatter), ], }); |