From 5d0edfc3b9a20bf68b16c463bbf9cbf31324143a Mon Sep 17 00:00:00 2001 From: Charles Vandevoorde Date: Tue, 19 Jul 2022 00:58:36 +0200 Subject: [PATCH] add missing props in markdown layout (#3588) 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 000000000..89a0c4033 --- /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 8bf274329..2c218610b 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 77a2a8fd1..c0726d2ca 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 000000000..a11abb8fb --- /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 000000000..0f87c1bd0 --- /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