Skip to content

Commit

Permalink
eliminate some polynomial time issues
Browse files Browse the repository at this point in the history
  • Loading branch information
quantizor committed Feb 17, 2025
1 parent 07875ff commit d96a8d8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-bugs-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'markdown-to-jsx': patch
---

Replace some regexes with optimized functions to avoid polynomial time scenarios. Also fixes compatibility issues in some older browsers with the `trimEnd` API.
16 changes: 9 additions & 7 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,10 @@ function generateListRule(
let adjustedContent
if (thisItemIsAParagraph) {
state.inline = false
adjustedContent = content.replace(LIST_ITEM_END_R, '\n\n')
adjustedContent = trimEnd(content) + '\n\n'
} else {
state.inline = true
adjustedContent = content.replace(LIST_ITEM_END_R, '')
adjustedContent = trimEnd(content)
}

const result = parse(adjustedContent, state)
Expand Down Expand Up @@ -576,7 +576,9 @@ const BLOCK_SYNTAXES = [
]

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

function containsBlockSyntax(input: string) {
Expand Down Expand Up @@ -1408,10 +1410,10 @@ export function compiler(
parse(capture /*, parse, state*/) {
return {
lang: undefined,
text: capture[0]
.replace(/^ {4}/gm, '')
.replace(/\n+$/, '')
.replace(TEXT_UNESCAPE_R, '$1'),
text: trimEnd(capture[0].replace(/^ {4}/gm, '')).replace(
TEXT_UNESCAPE_R,
'$1'
),
}
},

Expand Down

0 comments on commit d96a8d8

Please # to comment.