Skip to content

Commit

Permalink
fix: allow imports of Markdown files as raw text (#13026)
Browse files Browse the repository at this point in the history
* fix: allow imports of Markdown files as raw text

* Switch to hasSpecialQueries

* Update test

* Update changeset
  • Loading branch information
ascorbic authored Jan 21, 2025
1 parent 0a0b197 commit 1d272f6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/large-dodos-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a regression that prevented the import of Markdown files as raw text or URLs.
4 changes: 4 additions & 0 deletions packages/astro/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions packages/astro/test/astro-markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down
Original file line number Diff line number Diff line change
@@ -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,
});
}

0 comments on commit 1d272f6

Please sign in to comment.