summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/src/plugins.ts
diff options
context:
space:
mode:
authorGravatar Ben Holmes <hey@bholmes.dev> 2023-01-03 16:31:19 -0500
committerGravatar GitHub <noreply@github.com> 2023-01-03 16:31:19 -0500
commite2019be6ffa46fa33d92cfd346f9ecbe51bb7144 (patch)
tree413c13945ae992c26111e78314a567f5c0136c67 /packages/integrations/mdx/src/plugins.ts
parent16c7d0bfd49d2b9bfae45385f506bcd642f9444a (diff)
downloadastro-e2019be6ffa46fa33d92cfd346f9ecbe51bb7144.tar.gz
astro-e2019be6ffa46fa33d92cfd346f9ecbe51bb7144.tar.zst
astro-e2019be6ffa46fa33d92cfd346f9ecbe51bb7144.zip
Change frontmatter injection ordering (#5687)
* feat: make user frontmatter accessible in md * test: new frontmatter injection * refactor: move injection utils to remark pkg * fix: add dist/internal to remark exports * feat: update frontmater injection in mdx * tests: new mdx injection * chore: changeset * chore: simplify frontmatter destructuring * fix: remove old _internal references * refactor: injectedFrontmatter -> remarkPluginFrontmatter * docs: add content collections change * chore: changeset heading levels
Diffstat (limited to 'packages/integrations/mdx/src/plugins.ts')
-rw-r--r--packages/integrations/mdx/src/plugins.ts76
1 files changed, 18 insertions, 58 deletions
diff --git a/packages/integrations/mdx/src/plugins.ts b/packages/integrations/mdx/src/plugins.ts
index c823c6e7c..4701b1679 100644
--- a/packages/integrations/mdx/src/plugins.ts
+++ b/packages/integrations/mdx/src/plugins.ts
@@ -2,7 +2,11 @@ import { rehypeHeadingIds } from '@astrojs/markdown-remark';
import { nodeTypes } from '@mdx-js/mdx';
import type { PluggableList } from '@mdx-js/mdx/lib/core.js';
import type { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
-import type { AstroConfig, MarkdownAstroData } from 'astro';
+import type { AstroConfig } from 'astro';
+import {
+ safelyGetAstroData,
+ InvalidAstroDataError,
+} from '@astrojs/markdown-remark/dist/internal.js';
import type { Literal, MemberExpression } from 'estree';
import { visit as estreeVisit } from 'estree-util-visit';
import { bold, yellow } from 'kleur/colors';
@@ -47,26 +51,18 @@ export function recmaInjectImportMetaEnvPlugin({
};
}
-export function remarkInitializeAstroData() {
+export function rehypeApplyFrontmatterExport() {
return function (tree: any, vfile: VFile) {
- if (!vfile.data.astro) {
- vfile.data.astro = { frontmatter: {} };
- }
- };
-}
-
-export function rehypeApplyFrontmatterExport(pageFrontmatter: Record<string, any>) {
- return function (tree: any, vfile: VFile) {
- const { frontmatter: injectedFrontmatter } = safelyGetAstroData(vfile.data);
- const frontmatter = { ...injectedFrontmatter, ...pageFrontmatter };
+ const astroData = safelyGetAstroData(vfile.data);
+ if (astroData instanceof InvalidAstroDataError)
+ throw new Error(
+ // Copied from Astro core `errors-data`
+ // TODO: find way to import error data from core
+ '[MDX] A remark or rehype plugin attempted to inject invalid frontmatter. Ensure "astro.frontmatter" is set to a valid JSON object that is not `null` or `undefined`.'
+ );
+ const { frontmatter } = astroData;
const exportNodes = [
- jsToTreeNode(
- `export const frontmatter = ${JSON.stringify(
- frontmatter
- )};\nexport const _internal = { injectedFrontmatter: ${JSON.stringify(
- injectedFrontmatter
- )} };`
- ),
+ jsToTreeNode(`export const frontmatter = ${JSON.stringify(frontmatter)};`),
];
if (frontmatter.layout) {
// NOTE(bholmesdev) 08-22-2022
@@ -151,10 +147,7 @@ export async function getRemarkPlugins(
mdxOptions: MdxOptions,
config: AstroConfig
): Promise<MdxRollupPluginOptions['remarkPlugins']> {
- let remarkPlugins: PluggableList = [
- // Set "vfile.data.astro" for plugins to inject frontmatter
- remarkInitializeAstroData,
- ];
+ let remarkPlugins: PluggableList = [];
switch (mdxOptions.extendPlugins) {
case false:
break;
@@ -217,6 +210,8 @@ export function getRehypePlugins(
// We run `rehypeHeadingIds` _last_ to respect any custom IDs set by user plugins.
rehypeHeadingIds,
rehypeInjectHeadingsExport,
+ // computed from `astro.data.frontmatter` in VFile data
+ rehypeApplyFrontmatterExport,
];
return rehypePlugins;
}
@@ -251,41 +246,6 @@ function ignoreStringPlugins(plugins: any[]) {
}
/**
- * Copied from markdown utils
- * @see "vite-plugin-utils"
- */
-function isValidAstroData(obj: unknown): obj is MarkdownAstroData {
- if (typeof obj === 'object' && obj !== null && obj.hasOwnProperty('frontmatter')) {
- const { frontmatter } = obj as any;
- try {
- // ensure frontmatter is JSON-serializable
- JSON.stringify(frontmatter);
- } catch {
- return false;
- }
- return typeof frontmatter === 'object' && frontmatter !== null;
- }
- return false;
-}
-
-/**
- * Copied from markdown utils
- * @see "vite-plugin-utils"
- */
-function safelyGetAstroData(vfileData: Data): MarkdownAstroData {
- const { astro } = vfileData;
-
- if (!astro) return { frontmatter: {} };
- if (!isValidAstroData(astro)) {
- throw Error(
- `[MDX] A remark or rehype plugin tried to add invalid frontmatter. Ensure "astro.frontmatter" is a JSON object!`
- );
- }
-
- return astro;
-}
-
-/**
* Check if estree entry is "import.meta.env.VARIABLE"
* If it is, return the variable name (i.e. "VARIABLE")
*/