Skip to content

Commit

Permalink
add missing props in markdown layout (#3588)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
charlesvdv authored Jul 18, 2022
1 parent 39c8647 commit 5d0edfc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/eight-baboons-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix missing props (url, file) in markdown layout
4 changes: 3 additions & 1 deletion packages/astro/src/vite-plugin-markdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}));
Expand Down Expand Up @@ -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}';` : ''}
Expand Down
9 changes: 9 additions & 0 deletions packages/astro/test/astro-markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
});
});
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: 'Hello world!'
layout: '../layouts/layout-props.astro'
---

0 comments on commit 5d0edfc

Please sign in to comment.