From 5d8141bf40b900533a6b4234ad118f436ab06047 Mon Sep 17 00:00:00 2001
From: Timeless0911 <1604889533@qq.com>
Date: Wed, 20 Nov 2024 13:21:42 +0800
Subject: [PATCH 1/2] fix: performace regression of check html regex
---
e2e/fixtures/inline-markdown/doc/inline/all.mdx | 4 +++-
e2e/tests/inline-markdown.test.ts | 4 ++++
packages/theme-default/src/logic/utils.ts | 8 ++++----
3 files changed, 11 insertions(+), 5 deletions(-)
diff --git a/e2e/fixtures/inline-markdown/doc/inline/all.mdx b/e2e/fixtures/inline-markdown/doc/inline/all.mdx
index 8891e7f1d..2b7ea3785 100644
--- a/e2e/fixtures/inline-markdown/doc/inline/all.mdx
+++ b/e2e/fixtures/inline-markdown/doc/inline/all.mdx
@@ -12,6 +12,8 @@
## **bold**
-## *emphasis*
+## _emphasis_
## ~~delete~~
+
+## `This is a long string to test regex performance`
diff --git a/e2e/tests/inline-markdown.test.ts b/e2e/tests/inline-markdown.test.ts
index 23b00cddd..44cad8ce9 100644
--- a/e2e/tests/inline-markdown.test.ts
+++ b/e2e/tests/inline-markdown.test.ts
@@ -140,6 +140,7 @@ test.describe('Inline markdown test', async () => {
'bold',
'emphasis',
'delete',
+ 'This is a long string to test regex performance',
].join(','),
);
const aInnerHtml = await Promise.all(a.map(element => element.innerHTML()));
@@ -152,6 +153,7 @@ test.describe('Inline markdown test', async () => {
'bold',
'emphasis',
'delete',
+ 'This is a long string to test regex performance
',
];
for (const [index, html] of aInnerHtml.entries()) {
expect(html).toContain(expectedAInnerHtml[index]);
@@ -177,6 +179,7 @@ test.describe('Inline markdown test', async () => {
'bold',
'emphasis',
'delete',
+ 'This is a long string to test regex performance',
].join(','),
);
const asidesInnerHtml = await Promise.all(
@@ -192,6 +195,7 @@ test.describe('Inline markdown test', async () => {
'bold',
'emphasis',
'delete',
+ 'This is a long string to test regex performance
',
].join(','),
);
});
diff --git a/packages/theme-default/src/logic/utils.ts b/packages/theme-default/src/logic/utils.ts
index 500fe919f..4f1b13cf2 100644
--- a/packages/theme-default/src/logic/utils.ts
+++ b/packages/theme-default/src/logic/utils.ts
@@ -38,12 +38,12 @@ export function renderHtmlOrText(str?: string | number | null) {
}
// Parse the HTML to check for validity
- // Regular Expression: The regex /<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s*\/>)/i is designed to match basic HTML tags, including self-closing tags.
+ // Regular Expression: match basic HTML tags, including self-closing tags.
// <([a-z]+): Matches the opening tag and captures the tag name.
- // ([^<]+)*: Matches any attributes within the tag.
- // (?:>(.*)<\/\1>|\s*\/>): Matches either a closing tag with content or a self-closing tag.
+ // ([^<]*): Matches any attributes within the tag.
+ // (?:>(.*?)<\/\1>|\s*\/>): Matches either a closing tag with content or a self-closing tag.
// i Flag: Makes the regex case-insensitive, allowing it to match tags like as well as .
- const hasValidHtmlElements = /<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s*\/>)/i.test(
+ const hasValidHtmlElements = /<([a-z]+)([^<]*)(?:>(.*?)<\/\1>|\s*\/>)/i.test(
str,
);
From 7cca6e4997dbd8d793b59c0a6eaf19a3f43f208a Mon Sep 17 00:00:00 2001
From: Timeless0911 <1604889533@qq.com>
Date: Wed, 20 Nov 2024 13:29:11 +0800
Subject: [PATCH 2/2] chore: update
---
e2e/fixtures/inline-markdown/doc/inline/all.mdx | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/e2e/fixtures/inline-markdown/doc/inline/all.mdx b/e2e/fixtures/inline-markdown/doc/inline/all.mdx
index 2b7ea3785..b9887875a 100644
--- a/e2e/fixtures/inline-markdown/doc/inline/all.mdx
+++ b/e2e/fixtures/inline-markdown/doc/inline/all.mdx
@@ -12,7 +12,7 @@
## **bold**
-## _emphasis_
+## *emphasis*
## ~~delete~~