From f632b945275c2615fc0fdf2abc831c45d0ddebcd Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Thu, 19 Dec 2024 16:49:01 +0000 Subject: [PATCH] fix: pass raw frontmatter to when parsing markdown in glob loader (#12789) --- .changeset/fresh-gifts-buy.md | 5 ++++ packages/astro/src/content/loaders/glob.ts | 2 +- .../astro/test/astro-markdown-plugins.test.js | 18 +++++++++++++ .../content-layer-remark-plugins/.gitignore | 24 +++++++++++++++++ .../astro.config.mjs | 20 ++++++++++++++ .../content-layer-remark-plugins/package.json | 16 +++++++++++ .../public/favicon.svg | 9 +++++++ .../src/content.config.ts | 16 +++++++++++ .../src/content/docs/test1.md | 6 +++++ .../src/content/docs/test2.mdx | 8 ++++++ .../src/pages/[...slug].astro | 27 +++++++++++++++++++ .../src/pages/index.astro | 12 +++++++++ pnpm-lock.yaml | 9 +++++++ 13 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 .changeset/fresh-gifts-buy.md create mode 100644 packages/astro/test/fixtures/content-layer-remark-plugins/.gitignore create mode 100644 packages/astro/test/fixtures/content-layer-remark-plugins/astro.config.mjs create mode 100644 packages/astro/test/fixtures/content-layer-remark-plugins/package.json create mode 100644 packages/astro/test/fixtures/content-layer-remark-plugins/public/favicon.svg create mode 100644 packages/astro/test/fixtures/content-layer-remark-plugins/src/content.config.ts create mode 100644 packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/test1.md create mode 100644 packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/test2.mdx create mode 100644 packages/astro/test/fixtures/content-layer-remark-plugins/src/pages/[...slug].astro create mode 100644 packages/astro/test/fixtures/content-layer-remark-plugins/src/pages/index.astro diff --git a/.changeset/fresh-gifts-buy.md b/.changeset/fresh-gifts-buy.md new file mode 100644 index 000000000000..3f863358462b --- /dev/null +++ b/.changeset/fresh-gifts-buy.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Pass raw frontmatter to remark plugins in glob loader diff --git a/packages/astro/src/content/loaders/glob.ts b/packages/astro/src/content/loaders/glob.ts index 1e080c57a627..f96ab22b7015 100644 --- a/packages/astro/src/content/loaders/glob.ts +++ b/packages/astro/src/content/loaders/glob.ts @@ -171,7 +171,7 @@ export function glob(globOptions: GlobOptions): Loader { try { rendered = await render?.({ id, - data: parsedData, + data, body, filePath, digest, diff --git a/packages/astro/test/astro-markdown-plugins.test.js b/packages/astro/test/astro-markdown-plugins.test.js index 09cb76d2decc..ec7e701722b7 100644 --- a/packages/astro/test/astro-markdown-plugins.test.js +++ b/packages/astro/test/astro-markdown-plugins.test.js @@ -120,6 +120,24 @@ describe('Astro Markdown plugins', () => { testRemark(html); testRehype(html, '#smartypants-test'); }); + + describe("content layer plugins", () => { + let fixture + before(async () => { + fixture = await loadFixture({ + root: './fixtures/content-layer-remark-plugins/', + }); + await fixture.build(); + }); + + it('passes untransformed frontmatter to remark plugins', async () => { + const html = await fixture.readFile('/test1/index.html'); + const $ = cheerio.load(html); + assert.equal($('p').text(), 'Not transformed'); + }); + + }); + }); function testRehype(html, headingId) { diff --git a/packages/astro/test/fixtures/content-layer-remark-plugins/.gitignore b/packages/astro/test/fixtures/content-layer-remark-plugins/.gitignore new file mode 100644 index 000000000000..16d54bb13c8a --- /dev/null +++ b/packages/astro/test/fixtures/content-layer-remark-plugins/.gitignore @@ -0,0 +1,24 @@ +# build output +dist/ +# generated types +.astro/ + +# dependencies +node_modules/ + +# logs +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* + + +# environment variables +.env +.env.production + +# macOS-specific files +.DS_Store + +# jetbrains setting folder +.idea/ diff --git a/packages/astro/test/fixtures/content-layer-remark-plugins/astro.config.mjs b/packages/astro/test/fixtures/content-layer-remark-plugins/astro.config.mjs new file mode 100644 index 000000000000..950fdd73ba26 --- /dev/null +++ b/packages/astro/test/fixtures/content-layer-remark-plugins/astro.config.mjs @@ -0,0 +1,20 @@ +// @ts-check +import { defineConfig } from 'astro/config'; +import mdx from '@astrojs/mdx'; + +// https://astro.build/config +export default defineConfig({ + integrations: [mdx()], + markdown: { + remarkPlugins: [ + function myRemarkPlugin() { + return function transformer(tree, file) { + tree.children.push({ + type: 'paragraph', + children: [{ type: 'text', value: file?.data?.astro?.frontmatter?.addedByTransformer ? "Transformed" : "Not transformed" }], + }); + }; + }, + ], + }, +}); diff --git a/packages/astro/test/fixtures/content-layer-remark-plugins/package.json b/packages/astro/test/fixtures/content-layer-remark-plugins/package.json new file mode 100644 index 000000000000..d00fb93e9e32 --- /dev/null +++ b/packages/astro/test/fixtures/content-layer-remark-plugins/package.json @@ -0,0 +1,16 @@ +{ + "name": "@test/content-layer-remark-plugins", + "type": "module", + "version": "0.0.1", + "private": true, + "scripts": { + "dev": "astro dev", + "build": "astro build", + "preview": "astro preview", + "astro": "astro" + }, + "dependencies": { + "@astrojs/mdx": "workspace:*", + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/content-layer-remark-plugins/public/favicon.svg b/packages/astro/test/fixtures/content-layer-remark-plugins/public/favicon.svg new file mode 100644 index 000000000000..f157bd1c5e28 --- /dev/null +++ b/packages/astro/test/fixtures/content-layer-remark-plugins/public/favicon.svg @@ -0,0 +1,9 @@ + + + + diff --git a/packages/astro/test/fixtures/content-layer-remark-plugins/src/content.config.ts b/packages/astro/test/fixtures/content-layer-remark-plugins/src/content.config.ts new file mode 100644 index 000000000000..8fc30380482e --- /dev/null +++ b/packages/astro/test/fixtures/content-layer-remark-plugins/src/content.config.ts @@ -0,0 +1,16 @@ +import { defineCollection, z } from 'astro:content'; +import { glob } from 'astro/loaders'; + +const docs = defineCollection({ + loader: glob({ pattern: '**/*.{md,mdx}', base: './src/content/docs' }), + schema: z + .object({ + title: z.string(), + }) + .transform((data) => ({ + ...data, + someOtherField: 'Added by transform', + })), +}); + +export const collections = { docs }; diff --git a/packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/test1.md b/packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/test1.md new file mode 100644 index 000000000000..a5e9bc608655 --- /dev/null +++ b/packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/test1.md @@ -0,0 +1,6 @@ +--- +title: Test Markdown +foo: bar +--- + +# Test Markdown diff --git a/packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/test2.mdx b/packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/test2.mdx new file mode 100644 index 000000000000..3b937b559ec8 --- /dev/null +++ b/packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/test2.mdx @@ -0,0 +1,8 @@ +--- +title: Test MDX +foo: bar +--- + +Hello from MDX! + +[Markdown page](/test1) diff --git a/packages/astro/test/fixtures/content-layer-remark-plugins/src/pages/[...slug].astro b/packages/astro/test/fixtures/content-layer-remark-plugins/src/pages/[...slug].astro new file mode 100644 index 000000000000..757765a4c7ea --- /dev/null +++ b/packages/astro/test/fixtures/content-layer-remark-plugins/src/pages/[...slug].astro @@ -0,0 +1,27 @@ +--- +import { getCollection, render } from 'astro:content'; + +export async function getStaticPaths() { + const docs = await getCollection('docs'); + return docs.map(doc => ({ + params: { slug: doc.id }, + props: { doc }, + })); +} + +const { doc } = Astro.props; +const { Content } = await render(doc); +--- + + + + + + + Document + + +

{doc.data.title}

+ + + diff --git a/packages/astro/test/fixtures/content-layer-remark-plugins/src/pages/index.astro b/packages/astro/test/fixtures/content-layer-remark-plugins/src/pages/index.astro new file mode 100644 index 000000000000..34ce5d6102ff --- /dev/null +++ b/packages/astro/test/fixtures/content-layer-remark-plugins/src/pages/index.astro @@ -0,0 +1,12 @@ + + + + + + Document + + +

Markdown page

+

MDX page

+ + \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4d1eb4f95e76..c5086638084b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2655,6 +2655,15 @@ importers: specifier: ^10.25.0 version: 10.25.1 + packages/astro/test/fixtures/content-layer-remark-plugins: + dependencies: + '@astrojs/mdx': + specifier: workspace:* + version: link:../../../../integrations/mdx + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/content-layer-rendering: dependencies: '@astrojs/mdx':