Skip to content

Commit

Permalink
Process empty markdown body for remark/rehype plugins (#12920)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Jan 7, 2025
1 parent 216cb2b commit 8b9d530
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-years-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Processes markdown with empty body as remark and rehype plugins may add additional content or frontmatter
8 changes: 2 additions & 6 deletions packages/astro/src/vite-plugin-markdown/content-entry-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/astro-markdown-plugins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!',
);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!';
}
};
}
],
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: Test Empty Markdown
---
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>

0 comments on commit 8b9d530

Please sign in to comment.