Skip to content

Commit 71a0b26

Browse files
fix: unsufficient regex
1 parent 3dc7098 commit 71a0b26

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

packages/astro/src/core/util.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { fileURLToPath } from 'node:url';
44
import type { AstroSettings } from '../types/astro.js';
55
import type { AstroConfig } from '../types/public/config.js';
66
import type { RouteData } from '../types/public/internal.js';
7+
import { hasSpecialQueries } from '../vite-plugin-utils/index.js';
8+
import { SUPPORTED_MARKDOWN_FILE_EXTENSIONS } from './constants.js';
79
import { removeQueryString, removeTrailingForwardSlash, slash } from './path.js';
810

911
/** Returns true if argument is an object of any prototype/class (but not null). */
@@ -16,6 +18,19 @@ export function isURL(value: unknown): value is URL {
1618
return Object.prototype.toString.call(value) === '[object URL]';
1719
}
1820

21+
/** Check if a file is a markdown file based on its extension */
22+
export function isMarkdownFile(fileId: string, option?: { suffix?: string }): boolean {
23+
if (hasSpecialQueries(fileId)) {
24+
return false;
25+
}
26+
const id = removeQueryString(fileId);
27+
const _suffix = option?.suffix ?? '';
28+
for (let markdownFileExtension of SUPPORTED_MARKDOWN_FILE_EXTENSIONS) {
29+
if (id.endsWith(`${markdownFileExtension}${_suffix}`)) return true;
30+
}
31+
return false;
32+
}
33+
1934
/** Wraps an object in an array. If an array is passed, ignore it. */
2035
export function arraify<T>(target: T | T[]): T[] {
2136
return Array.isArray(target) ? target : [target];

packages/astro/src/vite-plugin-markdown/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { Plugin } from 'vite';
99
import { safeParseFrontmatter } from '../content/utils.js';
1010
import { AstroError, AstroErrorData } from '../core/errors/index.js';
1111
import type { Logger } from '../core/logger/core.js';
12-
import { isPage } from '../core/util.js';
12+
import { isMarkdownFile, isPage } from '../core/util.js';
1313
import { normalizePath } from '../core/viteUtils.js';
1414
import { shorthash } from '../runtime/server/shorthash.js';
1515
import type { AstroSettings } from '../types/astro.js';
@@ -68,6 +68,11 @@ export default function markdown({ settings, logger }: AstroPluginOptions): Plug
6868
},
6969
},
7070
async handler(id) {
71+
// The id filter also matches file extensions in search params which we do not want.
72+
// So we check another time, but at least it will run for less ids
73+
if (!isMarkdownFile(id)) {
74+
return;
75+
}
7176
const { fileId, fileUrl } = getFileInfo(id, settings.config);
7277
const rawFile = await fs.promises.readFile(fileId, 'utf-8');
7378
const raw = safeParseFrontmatter(rawFile, id);

0 commit comments

Comments
 (0)