Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: performance regression of check html regex #1609

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions e2e/fixtures/inline-markdown/doc/inline/all.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@
## *emphasis*

## ~~delete~~

## `This is a long string to test regex performance`
4 changes: 4 additions & 0 deletions e2e/tests/inline-markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -152,6 +153,7 @@ test.describe('Inline markdown test', async () => {
'<strong>bold</strong>',
'<em>emphasis</em>',
'<del>delete</del>',
'<code>This is a long string to test regex performance</code>',
];
for (const [index, html] of aInnerHtml.entries()) {
expect(html).toContain(expectedAInnerHtml[index]);
Expand All @@ -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(
Expand All @@ -192,6 +195,7 @@ test.describe('Inline markdown test', async () => {
'<strong>bold</strong>',
'<em>emphasis</em>',
'<del>delete</del>',
'<code>This is a long string to test regex performance</code>',
].join(','),
);
});
Expand Down
8 changes: 4 additions & 4 deletions packages/theme-default/src/logic/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <IMG> as well as <img>.
const hasValidHtmlElements = /<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s*\/>)/i.test(
const hasValidHtmlElements = /<([a-z]+)([^<]*)(?:>(.*?)<\/\1>|\s*\/>)/i.test(
str,
);

Expand Down
Loading