summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Bjorn Lu <bjornlu.dev@gmail.com> 2025-01-07 23:05:01 +0800
committerGravatar GitHub <noreply@github.com> 2025-01-07 23:05:01 +0800
commit8b9d53037879cd7ca7bee4d20b4e6f08e984a7df (patch)
tree5fde0294505ac06f8d356ca133ea3a0f59f872ae
parent216cb2b9c130a3be25f05c76948fddeca8f08173 (diff)
downloadastro-8b9d53037879cd7ca7bee4d20b4e6f08e984a7df.tar.gz
astro-8b9d53037879cd7ca7bee4d20b4e6f08e984a7df.tar.zst
astro-8b9d53037879cd7ca7bee4d20b4e6f08e984a7df.zip
Process empty markdown body for remark/rehype plugins (#12920)
-rw-r--r--.changeset/pink-years-warn.md5
-rw-r--r--packages/astro/src/vite-plugin-markdown/content-entry-type.ts8
-rw-r--r--packages/astro/test/astro-markdown-plugins.test.js10
-rw-r--r--packages/astro/test/fixtures/content-layer-remark-plugins/astro.config.mjs8
-rw-r--r--packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/empty-content.md3
-rw-r--r--packages/astro/test/fixtures/content-layer-remark-plugins/src/pages/[...slug].astro3
6 files changed, 30 insertions, 7 deletions
diff --git a/.changeset/pink-years-warn.md b/.changeset/pink-years-warn.md
new file mode 100644
index 000000000..aa2c6ccd9
--- /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 3ca85a356..6f248853f 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 3429c2ed7..78db0e6dc 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 950fdd73b..22efd0974 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 000000000..840d7d64a
--- /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 757765a4c..b25523258 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);
---
<!DOCTYPE html>
@@ -22,6 +22,7 @@ const { Content } = await render(doc);
</head>
<body>
<h1>{doc.data.title}</h1>
+ <div id="frontmatter-custom-property">{remarkPluginFrontmatter?.customProperty}</div>
<Content />
</body>
</html>