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 02b26e0 commit d154e0f
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 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 Expand Up @@ -2496,3 +2498,9 @@ export namespace MarkdownToJSX {
}

export default Markdown

function trimEndAndAppend(str: string, suffix: string) {
let end = str.length
while (end > 0 && str[end - 1] <= ' ') end--
return str.slice(0, end) + suffix
}

0 comments on commit d154e0f

Please sign in to comment.