Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add missing props in markdown layout #3588

Merged
merged 2 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -108,7 +108,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 @@ -153,6 +153,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 { slug as $$slug } from '@astrojs/markdown-remark';
${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 @@ -314,4 +314,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'
---