From b01c74aeccc4ec76b64fa75d163df58274b37970 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Wed, 11 Dec 2024 10:21:44 +0000 Subject: [PATCH] fix: strip query string before checking md extension (#12712) --- .changeset/tidy-ligers-tan.md | 5 ++++ packages/astro/src/core/util.ts | 4 +++- packages/astro/test/astro-markdown.test.js | 23 ++++++++++++++++++- .../src/pages/false-positive.astro | 5 ++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 .changeset/tidy-ligers-tan.md create mode 100644 packages/astro/test/fixtures/astro-markdown/src/pages/false-positive.astro diff --git a/.changeset/tidy-ligers-tan.md b/.changeset/tidy-ligers-tan.md new file mode 100644 index 0000000000..9891877bf8 --- /dev/null +++ b/.changeset/tidy-ligers-tan.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a bug which misidentified pages as markdown if a query string ended in a markdown extension diff --git a/packages/astro/src/core/util.ts b/packages/astro/src/core/util.ts index f9dd0ced8b..458fb9bdc3 100644 --- a/packages/astro/src/core/util.ts +++ b/packages/astro/src/core/util.ts @@ -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; } diff --git a/packages/astro/test/astro-markdown.test.js b/packages/astro/test/astro-markdown.test.js index d0a49c873d..0dae61ebd6 100644 --- a/packages/astro/test/astro-markdown.test.js +++ b/packages/astro/test/astro-markdown.test.js @@ -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'; @@ -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(); + }); + + }); }); diff --git a/packages/astro/test/fixtures/astro-markdown/src/pages/false-positive.astro b/packages/astro/test/fixtures/astro-markdown/src/pages/false-positive.astro new file mode 100644 index 0000000000..2f39eceadb --- /dev/null +++ b/packages/astro/test/fixtures/astro-markdown/src/pages/false-positive.astro @@ -0,0 +1,5 @@ +--- +let page = "not markdown" +--- + +

the page is {page}