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:
parent
39c864773b
commit
5d0edfc3b9
5 changed files with 38 additions and 1 deletions
5
.changeset/eight-baboons-train.md
Normal file
5
.changeset/eight-baboons-train.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix missing props (url, file) in markdown layout
|
|
@ -114,7 +114,7 @@ export default function markdown({ config }: AstroPluginOptions): Plugin {
|
||||||
export function $$loadMetadata() {
|
export function $$loadMetadata() {
|
||||||
return load().then((m) => m.$$metadata);
|
return load().then((m) => m.$$metadata);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deferred
|
// Deferred
|
||||||
export default async function load() {
|
export default async function load() {
|
||||||
return (await import(${JSON.stringify(fileId + MARKDOWN_CONTENT_FLAG)}));
|
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;
|
let { code: astroResult, metadata } = renderResult;
|
||||||
const { layout = '', components = '', setup = '', ...content } = frontmatter;
|
const { layout = '', components = '', setup = '', ...content } = frontmatter;
|
||||||
content.astro = metadata;
|
content.astro = metadata;
|
||||||
|
content.url = getFileInfo(id, config).fileUrl;
|
||||||
|
content.file = filename;
|
||||||
const prelude = `---
|
const prelude = `---
|
||||||
import Slugger from 'github-slugger';
|
import Slugger from 'github-slugger';
|
||||||
${layout ? `import Layout from '${layout}';` : ''}
|
${layout ? `import Layout from '${layout}';` : ''}
|
||||||
|
|
|
@ -338,4 +338,13 @@ describe('Astro Markdown', () => {
|
||||||
|
|
||||||
expect($('article').eq(4).text().replace(/[^❌]/g, '')).to.equal('❌❌❌');
|
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$/);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
17
packages/astro/test/fixtures/astro-markdown/src/layouts/layout-props.astro
vendored
Normal file
17
packages/astro/test/fixtures/astro-markdown/src/layouts/layout-props.astro
vendored
Normal 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>
|
4
packages/astro/test/fixtures/astro-markdown/src/pages/layout-props.md
vendored
Normal file
4
packages/astro/test/fixtures/astro-markdown/src/pages/layout-props.md
vendored
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
title: 'Hello world!'
|
||||||
|
layout: '../layouts/layout-props.astro'
|
||||||
|
---
|
Loading…
Reference in a new issue