diff --git a/.changeset/mighty-chairs-reply.md b/.changeset/mighty-chairs-reply.md new file mode 100644 index 000000000000..c72e330874a4 --- /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 48c13274ac17..2c09e76d4821 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 ddd24a953207..b8aa3ddb0412 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 000000000000..67f307b0f0e0 --- /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 000000000000..e9a2e4beba4b --- /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 000000000000..e9a2e4beba4b --- /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 000000000000..e9a2e4beba4b --- /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 000000000000..e9a2e4beba4b --- /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 000000000000..e69de29bb2d1