From d154e0f013b2b807f177dced0861683e686aafb4 Mon Sep 17 00:00:00 2001 From: Evan Jacobs Date: Mon, 17 Feb 2025 13:44:11 -0500 Subject: [PATCH] eliminate some polynomial time issues --- index.tsx | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/index.tsx b/index.tsx index 43789e6..e55ad70 100644 --- a/index.tsx +++ b/index.tsx @@ -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) @@ -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) { @@ -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' + ), } }, @@ -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 +}