File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed
Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ import { fileURLToPath } from 'node:url';
44import type { AstroSettings } from '../types/astro.js' ;
55import type { AstroConfig } from '../types/public/config.js' ;
66import 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' ;
79import { 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. */
2035export function arraify < T > ( target : T | T [ ] ) : T [ ] {
2136 return Array . isArray ( target ) ? target : [ target ] ;
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ import type { Plugin } from 'vite';
99import { safeParseFrontmatter } from '../content/utils.js' ;
1010import { AstroError , AstroErrorData } from '../core/errors/index.js' ;
1111import type { Logger } from '../core/logger/core.js' ;
12- import { isPage } from '../core/util.js' ;
12+ import { isMarkdownFile , isPage } from '../core/util.js' ;
1313import { normalizePath } from '../core/viteUtils.js' ;
1414import { shorthash } from '../runtime/server/shorthash.js' ;
1515import 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 ) ;
You can’t perform that action at this time.
0 commit comments