diff --git a/.changeset/eight-baboons-train.md b/.changeset/eight-baboons-train.md new file mode 100644 index 0000000000..89a0c40338 --- /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 8bf2743293..2c218610b5 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 77a2a8fd1e..c0726d2caf 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 0000000000..a11abb8fb5 --- /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; +--- + + + +
{title}
+
{url}
+
{file}
+ + \ No newline at end of file diff --git a/packages/astro/test/fixtures/astro-markdown/src/pages/layout-props.md b/packages/astro/test/fixtures/astro-markdown/src/pages/layout-props.md new file mode 100644 index 0000000000..0f87c1bd0a --- /dev/null +++ b/packages/astro/test/fixtures/astro-markdown/src/pages/layout-props.md @@ -0,0 +1,4 @@ +--- +title: 'Hello world!' +layout: '../layouts/layout-props.astro' +--- \ No newline at end of file