Skip to content

Commit d96a8d8

Browse files
committed
eliminate some polynomial time issues
1 parent 07875ff commit d96a8d8

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

.changeset/hungry-bugs-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'markdown-to-jsx': patch
3+
---
4+
5+
Replace some regexes with optimized functions to avoid polynomial time scenarios. Also fixes compatibility issues in some older browsers with the `trimEnd` API.

index.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,10 @@ function generateListRule(
509509
let adjustedContent
510510
if (thisItemIsAParagraph) {
511511
state.inline = false
512-
adjustedContent = content.replace(LIST_ITEM_END_R, '\n\n')
512+
adjustedContent = trimEnd(content) + '\n\n'
513513
} else {
514514
state.inline = true
515-
adjustedContent = content.replace(LIST_ITEM_END_R, '')
515+
adjustedContent = trimEnd(content)
516516
}
517517

518518
const result = parse(adjustedContent, state)
@@ -576,7 +576,9 @@ const BLOCK_SYNTAXES = [
576576
]
577577

578578
function trimEnd(str: string) {
579-
return str.replace(/\s*$/, '')
579+
let end = str.length
580+
while (end > 0 && str[end - 1] <= ' ') end--
581+
return str.slice(0, end)
580582
}
581583

582584
function containsBlockSyntax(input: string) {
@@ -1408,10 +1410,10 @@ export function compiler(
14081410
parse(capture /*, parse, state*/) {
14091411
return {
14101412
lang: undefined,
1411-
text: capture[0]
1412-
.replace(/^ {4}/gm, '')
1413-
.replace(/\n+$/, '')
1414-
.replace(TEXT_UNESCAPE_R, '$1'),
1413+
text: trimEnd(capture[0].replace(/^ {4}/gm, '')).replace(
1414+
TEXT_UNESCAPE_R,
1415+
'$1'
1416+
),
14151417
}
14161418
},
14171419

0 commit comments

Comments
 (0)