From 26ff429058c6244767276b9fa20ef58987be13ee Mon Sep 17 00:00:00 2001 From: Happydev <81974850+MoustaphaDev@users.noreply.github.com> Date: Fri, 28 Oct 2022 15:58:34 +0000 Subject: [PATCH] fix not omitted extension in `url` metadata for newly added markdown files in development (#5238) * test: add tests * test: add test case * test: update test * fix: add new extensions to regex used to removed * chore: add changeset * test: update test --- .changeset/mighty-chairs-reply.md | 5 +++++ packages/astro/src/vite-plugin-utils/index.ts | 4 +++- packages/astro/test/astro-global.test.js | 17 ++++++++++++----- .../src/pages/omit-markdown-extensions.astro | 16 ++++++++++++++++ .../astro-global/src/pages/post/post-4.markdown | 6 ++++++ .../astro-global/src/pages/post/post-5.mdown | 6 ++++++ .../astro-global/src/pages/post/post-6.mkdn | 6 ++++++ .../astro-global/src/pages/post/post-7.mkd | 6 ++++++ .../astro-global/src/pages/post/post-8.mdwn | 0 9 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 .changeset/mighty-chairs-reply.md create mode 100644 packages/astro/test/fixtures/astro-global/src/pages/omit-markdown-extensions.astro create mode 100644 packages/astro/test/fixtures/astro-global/src/pages/post/post-4.markdown create mode 100644 packages/astro/test/fixtures/astro-global/src/pages/post/post-5.mdown create mode 100644 packages/astro/test/fixtures/astro-global/src/pages/post/post-6.mkdn create mode 100644 packages/astro/test/fixtures/astro-global/src/pages/post/post-7.mkd create mode 100644 packages/astro/test/fixtures/astro-global/src/pages/post/post-8.mdwn diff --git a/.changeset/mighty-chairs-reply.md b/.changeset/mighty-chairs-reply.md new file mode 100644 index 0000000000..c72e330874 --- /dev/null +++ b/.changeset/mighty-chairs-reply.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix not included file extension in `url` metadata for newly added markdown files diff --git a/packages/astro/src/vite-plugin-utils/index.ts b/packages/astro/src/vite-plugin-utils/index.ts index 48c13274ac..2c09e76d48 100644 --- a/packages/astro/src/vite-plugin-utils/index.ts +++ b/packages/astro/src/vite-plugin-utils/index.ts @@ -9,7 +9,9 @@ export function getFileInfo(id: string, config: AstroConfig) { const fileId = id.split('?')[0]; let fileUrl = fileId.includes('/pages/') - ? fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.(md|astro)$/, '') + ? fileId + .replace(/^.*?\/pages\//, sitePathname) + .replace(/(\/index)?\.(md|markdown|mdown|mkdn|mkd|mdwn|md|astro)$/, '') : undefined; if (fileUrl && config.trailingSlash === 'always') { fileUrl = appendForwardSlash(fileUrl); diff --git a/packages/astro/test/astro-global.test.js b/packages/astro/test/astro-global.test.js index ddd24a9532..b8aa3ddb04 100644 --- a/packages/astro/test/astro-global.test.js +++ b/packages/astro/test/astro-global.test.js @@ -15,12 +15,9 @@ describe('Astro Global', () => { describe('dev', () => { let devServer; - let $; before(async () => { devServer = await fixture.startDevServer(); - const html = await fixture.fetch('/blog/?foo=42').then((res) => res.text()); - $ = cheerio.load(html); }); after(async () => { @@ -28,11 +25,21 @@ describe('Astro Global', () => { }); it('Astro.request.url', async () => { + const html = await fixture.fetch('/blog/?foo=42').then((res) => res.text()); + const $ = cheerio.load(html); expect($('#pathname').text()).to.equal('/blog/'); expect($('#searchparams').text()).to.equal('{}'); expect($('#child-pathname').text()).to.equal('/blog/'); expect($('#nested-child-pathname').text()).to.equal('/blog/'); }); + + it('Astro.glob() returned `url` metadata of each markdown file extensions DOES NOT include the extension', async () => { + const html = await fixture.fetch('/blog/omit-markdown-extensions/').then((res) => res.text()); + const $ = cheerio.load(html); + expect($('[data-any-url-contains-extension]').data('any-url-contains-extension')).to.equal( + false + ); + }); }); describe('build', () => { @@ -65,8 +72,8 @@ describe('Astro Global', () => { it('Astro.glob() correctly returns meta info for MD and Astro files', async () => { const html = await fixture.readFile('/glob/index.html'); const $ = cheerio.load(html); - expect($('[data-file]').length).to.equal(3); - expect($('.post-url[href]').length).to.equal(3); + expect($('[data-file]').length).to.equal(8); + expect($('.post-url[href]').length).to.equal(8); }); }); }); diff --git a/packages/astro/test/fixtures/astro-global/src/pages/omit-markdown-extensions.astro b/packages/astro/test/fixtures/astro-global/src/pages/omit-markdown-extensions.astro new file mode 100644 index 0000000000..67f307b0f0 --- /dev/null +++ b/packages/astro/test/fixtures/astro-global/src/pages/omit-markdown-extensions.astro @@ -0,0 +1,16 @@ +--- +const markdownPosts = await Astro.glob('./post/**/*.{markdown,mdown,mkdn,mkd,mdwn,md}'); +const markdownExtensions = /(\.(markdown|mdown|mkdn|mkd|mdwn|md))$/g +const aUrlContainsExtension = markdownPosts.some((page:any)=> { + return markdownExtensions.test(page.url) + }) +--- + + + + Extensions omitted + + +

Placeholder

+ + diff --git a/packages/astro/test/fixtures/astro-global/src/pages/post/post-4.markdown b/packages/astro/test/fixtures/astro-global/src/pages/post/post-4.markdown new file mode 100644 index 0000000000..e9a2e4beba --- /dev/null +++ b/packages/astro/test/fixtures/astro-global/src/pages/post/post-4.markdown @@ -0,0 +1,6 @@ +--- +title: 'Another post' +layout: '../../layouts/post.astro' +--- + +# Another post diff --git a/packages/astro/test/fixtures/astro-global/src/pages/post/post-5.mdown b/packages/astro/test/fixtures/astro-global/src/pages/post/post-5.mdown new file mode 100644 index 0000000000..e9a2e4beba --- /dev/null +++ b/packages/astro/test/fixtures/astro-global/src/pages/post/post-5.mdown @@ -0,0 +1,6 @@ +--- +title: 'Another post' +layout: '../../layouts/post.astro' +--- + +# Another post diff --git a/packages/astro/test/fixtures/astro-global/src/pages/post/post-6.mkdn b/packages/astro/test/fixtures/astro-global/src/pages/post/post-6.mkdn new file mode 100644 index 0000000000..e9a2e4beba --- /dev/null +++ b/packages/astro/test/fixtures/astro-global/src/pages/post/post-6.mkdn @@ -0,0 +1,6 @@ +--- +title: 'Another post' +layout: '../../layouts/post.astro' +--- + +# Another post diff --git a/packages/astro/test/fixtures/astro-global/src/pages/post/post-7.mkd b/packages/astro/test/fixtures/astro-global/src/pages/post/post-7.mkd new file mode 100644 index 0000000000..e9a2e4beba --- /dev/null +++ b/packages/astro/test/fixtures/astro-global/src/pages/post/post-7.mkd @@ -0,0 +1,6 @@ +--- +title: 'Another post' +layout: '../../layouts/post.astro' +--- + +# Another post diff --git a/packages/astro/test/fixtures/astro-global/src/pages/post/post-8.mdwn b/packages/astro/test/fixtures/astro-global/src/pages/post/post-8.mdwn new file mode 100644 index 0000000000..e69de29bb2