From 1d272f6a5a3af16ad2ab9af41b7193ce67964b69 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Tue, 21 Jan 2025 12:24:11 +0000 Subject: [PATCH] fix: allow imports of Markdown files as raw text (#13026) * fix: allow imports of Markdown files as raw text * Switch to hasSpecialQueries * Update test * Update changeset --- .changeset/large-dodos-bake.md | 5 +++++ packages/astro/src/core/util.ts | 4 ++++ packages/astro/test/astro-markdown.test.js | 9 +++++++++ .../astro-markdown/src/pages/raw-content.json.js | 5 ++++- 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 .changeset/large-dodos-bake.md diff --git a/.changeset/large-dodos-bake.md b/.changeset/large-dodos-bake.md new file mode 100644 index 0000000000..a007e9c6b7 --- /dev/null +++ b/.changeset/large-dodos-bake.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a regression that prevented the import of Markdown files as raw text or URLs. diff --git a/packages/astro/src/core/util.ts b/packages/astro/src/core/util.ts index 1bd53d779b..5c29fc900e 100644 --- a/packages/astro/src/core/util.ts +++ b/packages/astro/src/core/util.ts @@ -6,6 +6,7 @@ import type { AstroConfig } from '../types/public/config.js'; import type { RouteData } from '../types/public/internal.js'; import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './constants.js'; import { removeQueryString, removeTrailingForwardSlash, slash } from './path.js'; +import { hasSpecialQueries } from '../vite-plugin-utils/index.js'; /** Returns true if argument is an object of any prototype/class (but not null). */ export function isObject(value: unknown): value is Record { @@ -18,6 +19,9 @@ 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 { + if (hasSpecialQueries(fileId)) { + return false; + } const id = removeQueryString(fileId); const _suffix = option?.suffix ?? ''; for (let markdownFileExtension of SUPPORTED_MARKDOWN_FILE_EXTENSIONS) { diff --git a/packages/astro/test/astro-markdown.test.js b/packages/astro/test/astro-markdown.test.js index 9d41673418..f9236084d8 100644 --- a/packages/astro/test/astro-markdown.test.js +++ b/packages/astro/test/astro-markdown.test.js @@ -24,6 +24,15 @@ describe('Astro Markdown', () => { ); }); + it('Allows ?raw and ?url imports', async () => { + const { rawImport, url } = JSON.parse(await fixture.readFile('/raw-content.json')); + assert.equal( + fixLineEndings(rawImport).trim(), + `# Basic page\n\nLets make sure raw and compiled content look right!`, + ); + assert.ok(url.startsWith("data:text/markdown;base64,")); + }); + it('Exposes compiled HTML content', async () => { const { compiled } = JSON.parse(await fixture.readFile('/raw-content.json')); diff --git a/packages/astro/test/fixtures/astro-markdown/src/pages/raw-content.json.js b/packages/astro/test/fixtures/astro-markdown/src/pages/raw-content.json.js index b1292cc323..e24d905ab7 100644 --- a/packages/astro/test/fixtures/astro-markdown/src/pages/raw-content.json.js +++ b/packages/astro/test/fixtures/astro-markdown/src/pages/raw-content.json.js @@ -1,8 +1,11 @@ import { compiledContent, rawContent } from './basic.md'; - +import md from './basic.md?raw'; +import url from './basic.md?url'; export async function GET() { return Response.json({ raw: rawContent(), compiled: await compiledContent(), + rawImport: md, + url, }); }