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
This commit is contained in:
Matt Kane 2025-01-21 12:24:11 +00:00 committed by GitHub
parent 0a0b1978a7
commit 1d272f6a5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 22 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes a regression that prevented the import of Markdown files as raw text or URLs.

View file

@ -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<string, any> {
@ -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) {

View file

@ -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'));

View file

@ -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,
});
}