0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

fix: strip query string before checking md extension (#12712)

This commit is contained in:
Matt Kane 2024-12-11 10:21:44 +00:00 committed by GitHub
parent 97c9265754
commit b01c74aecc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 35 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a bug which misidentified pages as markdown if a query string ended in a markdown extension

View file

@ -18,9 +18,11 @@ export function isURL(value: unknown): value is URL {
} }
/** Check if a file is a markdown file based on its extension */ /** Check if a file is a markdown file based on its extension */
export function isMarkdownFile(fileId: string, option?: { suffix?: string }): boolean { export function isMarkdownFile(fileId: string, option?: { suffix?: string }): boolean {
// Strip query string
const id = fileId.split("?")[0];
const _suffix = option?.suffix ?? ''; const _suffix = option?.suffix ?? '';
for (let markdownFileExtension of SUPPORTED_MARKDOWN_FILE_EXTENSIONS) { for (let markdownFileExtension of SUPPORTED_MARKDOWN_FILE_EXTENSIONS) {
if (fileId.endsWith(`${markdownFileExtension}${_suffix}`)) return true; if (id.endsWith(`${markdownFileExtension}${_suffix}`)) return true;
} }
return false; return false;
} }

View file

@ -1,5 +1,5 @@
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import { before, describe, it } from 'node:test'; import { before, describe, it, after } from 'node:test';
import * as cheerio from 'cheerio'; import * as cheerio from 'cheerio';
import { fixLineEndings, loadFixture } from './test-utils.js'; import { fixLineEndings, loadFixture } from './test-utils.js';
@ -154,4 +154,25 @@ describe('Astro Markdown', () => {
assert.ok(title.includes('import.meta.env.TITLE')); assert.ok(title.includes('import.meta.env.TITLE'));
}); });
}); });
describe('dev', () => {
let devServer;
before(async () => {
devServer = await fixture.startDevServer();
});
it('ignores .md extensions on query params', async () => {
const res = await fixture.fetch('/false-positive?page=page.md');
assert.ok(res.ok);
const html = await res.text();
const $ = cheerio.load(html);
assert.equal($('p').text(), 'the page is not markdown');
});
after(async () => {
await devServer.stop();
});
});
}); });

View file

@ -0,0 +1,5 @@
---
let page = "not markdown"
---
<p>the page is {page}</p>