Skip to content

Commit

Permalink
fix(core): remove global to avoid ambiguity in non-greedy match in fl…
Browse files Browse the repository at this point in the history
…at mdx (#680)
  • Loading branch information
10Derozan authored Feb 28, 2024
1 parent f426d2f commit 74751bd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-flies-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rspress/core': patch
---

fix(core): remove global to avoid ambiguity in non-greedy match in flat mdx
4 changes: 3 additions & 1 deletion packages/core/src/node/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { RSPRESS_TEMP_DIR } from '@rspress/shared';

export const isProduction = () => process.env.NODE_ENV === 'production';

export const importStatementRegex = /import\s+(.*?)\s+from\s+['"](.*?)['"];?/gm;
// Keep the quotation marks consistent before and after.
export const importStatementRegex =
/import\s+(.*?)\s+from\s+(['"])(.*?)(?:"|');?/gm;

// @ts-expect-error
const dirname = path.dirname(fileURLToPath(new URL(import.meta.url)));
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/node/utils/flattenMdxContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ export async function flattenMdxContent(
alias: Record<string, string | string[]>,
): Promise<string> {
// Performance optimization: if the content does not contain any import statement, we can skip the parsing process
// So we need to check this match

if (!importStatementRegex.test(content)) {
// Create new regExp to avoid the regex cache the last match index
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test#using_test_on_a_regex_with_the_global_flag
const regex = new RegExp(importStatementRegex);
if (!regex.test(content)) {
return content;
}
let result = content;
Expand Down

0 comments on commit 74751bd

Please sign in to comment.