Skip to content

Commit

Permalink
fix: strip query string before checking md extension (#12712)
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic authored Dec 11, 2024
1 parent 97c9265 commit b01c74a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-ligers-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug which misidentified pages as markdown if a query string ended in a markdown extension
4 changes: 3 additions & 1 deletion packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ export function isURL(value: unknown): value is URL {
}
/** Check if a file is a markdown file based on its extension */
export function isMarkdownFile(fileId: string, option?: { suffix?: string }): boolean {
// Strip query string
const id = fileId.split("?")[0];
const _suffix = option?.suffix ?? '';
for (let markdownFileExtension of SUPPORTED_MARKDOWN_FILE_EXTENSIONS) {
if (fileId.endsWith(`${markdownFileExtension}${_suffix}`)) return true;
if (id.endsWith(`${markdownFileExtension}${_suffix}`)) return true;
}
return false;
}
Expand Down
23 changes: 22 additions & 1 deletion packages/astro/test/astro-markdown.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 { fixLineEndings, loadFixture } from './test-utils.js';

Expand Down Expand Up @@ -154,4 +154,25 @@ describe('Astro Markdown', () => {
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();
});

});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
let page = "not markdown"
---

<p>the page is {page}</p>

0 comments on commit b01c74a

Please sign in to comment.