Skip to content

Commit

Permalink
fix not omitted extension in url metadata for newly added markdown …
Browse files Browse the repository at this point in the history
…files in development (withastro#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
  • Loading branch information
MoustaphaDev committed Oct 28, 2022
1 parent 468aa3f commit 26ff429
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-chairs-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix not included file extension in `url` metadata for newly added markdown files
4 changes: 3 additions & 1 deletion packages/astro/src/vite-plugin-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 12 additions & 5 deletions packages/astro/test/astro-global.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,31 @@ 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 () => {
await devServer.stop();
});

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', () => {
Expand Down Expand Up @@ -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);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: 'Another post'
layout: '../../layouts/post.astro'
---

# Another post
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: 'Another post'
layout: '../../layouts/post.astro'
---

# Another post
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: 'Another post'
layout: '../../layouts/post.astro'
---

# Another post
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: 'Another post'
layout: '../../layouts/post.astro'
---

# Another post
Empty file.

0 comments on commit 26ff429

Please sign in to comment.