From 01ec3ef0b1e274dc1794902935a8c01845258ad8 Mon Sep 17 00:00:00 2001 From: Charles Vandevoorde Date: Mon, 13 Jun 2022 20:24:30 +0200 Subject: [PATCH] add missing props in markdown layout 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. --- .changeset/eight-baboons-train.md | 5 +++++ .../astro/src/vite-plugin-markdown/index.ts | 4 +++- packages/astro/test/astro-markdown.test.js | 9 +++++++++ .../src/layouts/layout-props.astro | 17 +++++++++++++++++ .../astro-markdown/src/pages/layout-props.md | 4 ++++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 .changeset/eight-baboons-train.md create mode 100644 packages/astro/test/fixtures/astro-markdown/src/layouts/layout-props.astro create mode 100644 packages/astro/test/fixtures/astro-markdown/src/pages/layout-props.md 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 29bc629e354d..3c2d5c86b632 100644 --- a/packages/astro/src/vite-plugin-markdown/index.ts +++ b/packages/astro/src/vite-plugin-markdown/index.ts @@ -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)})); @@ -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}';` : ''} diff --git a/packages/astro/test/astro-markdown.test.js b/packages/astro/test/astro-markdown.test.js index 1ef8a4b9a852..350310048983 100644 --- a/packages/astro/test/astro-markdown.test.js +++ b/packages/astro/test/astro-markdown.test.js @@ -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$/); + }); }); 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; +--- + + + +
{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 000000000000..0f87c1bd0aef --- /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