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:
Happydev 2022-10-28 15:58:34 +00:00 committed by GitHub
parent 468aa3f308
commit 26ff429058
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 60 additions and 6 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix not included file extension in `url` metadata for newly added markdown files

View file

@ -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);

View file

@ -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);
});
});
});

View 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>

View file

@ -0,0 +1,6 @@
---
title: 'Another post'
layout: '../../layouts/post.astro'
---
# Another post

View file

@ -0,0 +1,6 @@
---
title: 'Another post'
layout: '../../layouts/post.astro'
---
# Another post

View file

@ -0,0 +1,6 @@
---
title: 'Another post'
layout: '../../layouts/post.astro'
---
# Another post

View file

@ -0,0 +1,6 @@
---
title: 'Another post'
layout: '../../layouts/post.astro'
---
# Another post