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
This commit is contained in:
parent
468aa3f308
commit
26ff429058
9 changed files with 60 additions and 6 deletions
5
.changeset/mighty-chairs-reply.md
Normal file
5
.changeset/mighty-chairs-reply.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fix not included file extension in `url` metadata for newly added markdown files
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
16
packages/astro/test/fixtures/astro-global/src/pages/omit-markdown-extensions.astro
vendored
Normal file
16
packages/astro/test/fixtures/astro-global/src/pages/omit-markdown-extensions.astro
vendored
Normal file
|
@ -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)
|
||||
})
|
||||
---
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Extensions omitted</title>
|
||||
</head>
|
||||
<body>
|
||||
<p data-any-url-contains-extension={JSON.stringify(aUrlContainsExtension)}>Placeholder</p>
|
||||
</body>
|
||||
</html>
|
6
packages/astro/test/fixtures/astro-global/src/pages/post/post-4.markdown
vendored
Normal file
6
packages/astro/test/fixtures/astro-global/src/pages/post/post-4.markdown
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
title: 'Another post'
|
||||
layout: '../../layouts/post.astro'
|
||||
---
|
||||
|
||||
# Another post
|
6
packages/astro/test/fixtures/astro-global/src/pages/post/post-5.mdown
vendored
Normal file
6
packages/astro/test/fixtures/astro-global/src/pages/post/post-5.mdown
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
title: 'Another post'
|
||||
layout: '../../layouts/post.astro'
|
||||
---
|
||||
|
||||
# Another post
|
6
packages/astro/test/fixtures/astro-global/src/pages/post/post-6.mkdn
vendored
Normal file
6
packages/astro/test/fixtures/astro-global/src/pages/post/post-6.mkdn
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
title: 'Another post'
|
||||
layout: '../../layouts/post.astro'
|
||||
---
|
||||
|
||||
# Another post
|
6
packages/astro/test/fixtures/astro-global/src/pages/post/post-7.mkd
vendored
Normal file
6
packages/astro/test/fixtures/astro-global/src/pages/post/post-7.mkd
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
title: 'Another post'
|
||||
layout: '../../layouts/post.astro'
|
||||
---
|
||||
|
||||
# Another post
|
0
packages/astro/test/fixtures/astro-global/src/pages/post/post-8.mdwn
vendored
Normal file
0
packages/astro/test/fixtures/astro-global/src/pages/post/post-8.mdwn
vendored
Normal file
Loading…
Reference in a new issue