0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00

add missing props in markdown layout (#3588)

The `url` props was missing but should there according to [this
document](https://docs.astro.build/en/guides/markdown-content/#markdown-layouts).

The `file` props was not initially there but is quite useful when you
need to resolve file which are relative to the markdown file itself.
This commit is contained in:
Charles Vandevoorde 2022-07-19 00:58:36 +02:00 committed by GitHub
parent 39c864773b
commit 5d0edfc3b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix missing props (url, file) in markdown layout

View file

@ -114,7 +114,7 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
export function $$loadMetadata() {
return load().then((m) => m.$$metadata);
}
// Deferred
export default async function load() {
return (await import(${JSON.stringify(fileId + MARKDOWN_CONTENT_FLAG)}));
@ -162,6 +162,8 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
let { code: astroResult, metadata } = renderResult;
const { layout = '', components = '', setup = '', ...content } = frontmatter;
content.astro = metadata;
content.url = getFileInfo(id, config).fileUrl;
content.file = filename;
const prelude = `---
import Slugger from 'github-slugger';
${layout ? `import Layout from '${layout}';` : ''}

View file

@ -338,4 +338,13 @@ describe('Astro Markdown', () => {
expect($('article').eq(4).text().replace(/[^❌]/g, '')).to.equal('❌❌❌');
});
it('Generate the right props for the layout', async () => {
const html = await fixture.readFile('/layout-props/index.html');
const $ = cheerio.load(html);
expect($('#title').text()).to.equal('Hello world!');
expect($('#url').text()).to.equal('/layout-props');
expect($('#file').text()).to.match(/.*\/layout-props.md$/);
});
});

View file

@ -0,0 +1,17 @@
---
interface Props {
url: string;
file: string;
title: string;
}
const { title, url, file } = Astro.props.content as Props;
---
<html>
<body>
<div id="title">{title}</div>
<div id="url">{url}</div>
<div id="file">{file}</div>
</body>
</html>

View file

@ -0,0 +1,4 @@
---
title: 'Hello world!'
layout: '../layouts/layout-props.astro'
---