From 8b9d53037879cd7ca7bee4d20b4e6f08e984a7df Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Tue, 7 Jan 2025 23:05:01 +0800 Subject: [PATCH] Process empty markdown body for remark/rehype plugins (#12920) --- .changeset/pink-years-warn.md | 5 +++++ .../src/vite-plugin-markdown/content-entry-type.ts | 8 ++------ packages/astro/test/astro-markdown-plugins.test.js | 10 ++++++++++ .../content-layer-remark-plugins/astro.config.mjs | 8 ++++++++ .../src/content/docs/empty-content.md | 3 +++ .../src/pages/[...slug].astro | 3 ++- 6 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 .changeset/pink-years-warn.md create mode 100644 packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/empty-content.md diff --git a/.changeset/pink-years-warn.md b/.changeset/pink-years-warn.md new file mode 100644 index 000000000000..aa2c6ccd920e --- /dev/null +++ b/.changeset/pink-years-warn.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Processes markdown with empty body as remark and rehype plugins may add additional content or frontmatter diff --git a/packages/astro/src/vite-plugin-markdown/content-entry-type.ts b/packages/astro/src/vite-plugin-markdown/content-entry-type.ts index 3ca85a35655c..6f248853f51e 100644 --- a/packages/astro/src/vite-plugin-markdown/content-entry-type.ts +++ b/packages/astro/src/vite-plugin-markdown/content-entry-type.ts @@ -20,12 +20,8 @@ export const markdownContentEntryType: ContentEntryType = { async getRenderFunction(config) { const processor = await createMarkdownProcessor(config.markdown); return async function renderToString(entry) { - if (!entry.body) { - return { - html: '', - }; - } - const result = await processor.render(entry.body, { + // Process markdown even if it's empty as remark/rehype plugins may add content or frontmatter dynamically + const result = await processor.render(entry.body ?? '', { frontmatter: entry.data, // @ts-expect-error Internal API fileURL: entry.filePath ? pathToFileURL(entry.filePath) : undefined, diff --git a/packages/astro/test/astro-markdown-plugins.test.js b/packages/astro/test/astro-markdown-plugins.test.js index 3429c2ed7b70..78db0e6dcbf6 100644 --- a/packages/astro/test/astro-markdown-plugins.test.js +++ b/packages/astro/test/astro-markdown-plugins.test.js @@ -135,6 +135,16 @@ describe('Astro Markdown plugins', () => { const $ = cheerio.load(html); assert.equal($('p').text(), 'Not transformed'); }); + + it('processes empty markdown content with remark plugins', async () => { + const html = await fixture.readFile('/empty-content/index.html'); + const $ = cheerio.load(html); + assert.equal($('h1').text(), 'Test Empty Markdown'); + assert.equal( + $('#frontmatter-custom-property').text(), + 'Generated property via remark plugin!', + ); + }); }); }); 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 index 950fdd73ba26..22efd09740ef 100644 --- a/packages/astro/test/fixtures/content-layer-remark-plugins/astro.config.mjs +++ b/packages/astro/test/fixtures/content-layer-remark-plugins/astro.config.mjs @@ -15,6 +15,14 @@ export default defineConfig({ }); }; }, + function addFrontmatter() { + return function (tree, file) { + if (file.data.astro?.frontmatter) { + file.data.astro.frontmatter.customProperty = + 'Generated property via remark plugin!'; + } + }; + } ], }, }); diff --git a/packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/empty-content.md b/packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/empty-content.md new file mode 100644 index 000000000000..840d7d64ac72 --- /dev/null +++ b/packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/empty-content.md @@ -0,0 +1,3 @@ +--- +title: Test Empty Markdown +--- 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 index 757765a4c7ea..b25523258acc 100644 --- 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 @@ -10,7 +10,7 @@ export async function getStaticPaths() { } const { doc } = Astro.props; -const { Content } = await render(doc); +const { Content, remarkPluginFrontmatter } = await render(doc); --- @@ -22,6 +22,7 @@ const { Content } = await render(doc);

{doc.data.title}

+
{remarkPluginFrontmatter?.customProperty}