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:
parent
216cb2b9c1
commit
8b9d530378
6 changed files with 30 additions and 7 deletions
5
.changeset/pink-years-warn.md
Normal file
5
.changeset/pink-years-warn.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Processes markdown with empty body as remark and rehype plugins may add additional content or frontmatter
|
|
@ -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,
|
||||
|
|
|
@ -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!',
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -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!';
|
||||
}
|
||||
};
|
||||
}
|
||||
],
|
||||
},
|
||||
});
|
||||
|
|
3
packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/empty-content.md
vendored
Normal file
3
packages/astro/test/fixtures/content-layer-remark-plugins/src/content/docs/empty-content.md
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
title: Test Empty Markdown
|
||||
---
|
|
@ -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>
|
||||
|
|
Loading…
Add table
Reference in a new issue