diff --git a/.changeset/eight-baboons-train.md b/.changeset/eight-baboons-train.md new file mode 100644 index 000000000000..89a0c4033800 --- /dev/null +++ b/.changeset/eight-baboons-train.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix missing props (url, file) in markdown layout diff --git a/packages/astro/src/vite-plugin-markdown/index.ts b/packages/astro/src/vite-plugin-markdown/index.ts index 8bf27432932b..2c218610b58c 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -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}';` : ''} diff --git a/packages/astro/test/astro-markdown.test.js b/packages/astro/test/astro-markdown.test.js index 77a2a8fd1e4b..c0726d2caf55 100644 --- a/packages/astro/test/astro-markdown.test.js +++ b/packages/astro/test/astro-markdown.test.js @@ -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$/); + }); }); diff --git a/packages/astro/test/fixtures/astro-markdown/src/layouts/layout-props.astro b/packages/astro/test/fixtures/astro-markdown/src/layouts/layout-props.astro new file mode 100644 index 000000000000..a11abb8fb5d1 --- /dev/null +++ b/packages/astro/test/fixtures/astro-markdown/src/layouts/layout-props.astro @@ -0,0 +1,17 @@ +--- + interface Props { + url: string; + file: string; + title: string; + } + + const { title, url, file } = Astro.props.content as Props; +--- + + +
+