0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-24 22:46:02 -05:00

Process empty markdown body for remark/rehype plugins (#12920)

This commit is contained in:
Bjorn Lu 2025-01-07 23:05:01 +08:00 committed by GitHub
parent 216cb2b9c1
commit 8b9d530378
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 30 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Processes markdown with empty body as remark and rehype plugins may add additional content or frontmatter

View file

@ -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,

View file

@ -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!',
);
});
});
});

View file

@ -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!';
}
};
}
],
},
});

View file

@ -0,0 +1,3 @@
---
title: Test Empty Markdown
---

View file

@ -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>