From cbe4316e4b51e54dafb101afb0326d260e4fb191 Mon Sep 17 00:00:00 2001 From: chengyongru <2755839590@qq.com> Date: Fri, 21 Aug 2026 23:46:59 +0800 Subject: [PATCH] perf(tui): avoid repeated LaTeX scans --- tui/src/latex.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tui/src/latex.ts b/tui/src/latex.ts index 10f4badfc..c0b147775 100644 --- a/tui/src/latex.ts +++ b/tui/src/latex.ts @@ -292,7 +292,7 @@ export function renderLatexAsUnicode(markdown: string): string { } const explicitOpening = explicitMathOpeningAt(markdown, cursor) - const math = mathSpanAt(markdown, cursor, missingClosers) + const math = mathSpanAt(markdown, cursor, explicitOpening, missingClosers) if (math) { const converted = convertMath(math.content) if (converted !== null) { @@ -365,10 +365,9 @@ function explicitMathOpeningAt(markdown: string, start: number): string | null { function mathSpanAt( markdown: string, start: number, + explicitOpening: string | null, missingClosers: Set, ): MathSpan | null { - const explicitOpening = explicitMathOpeningAt(markdown, start) - if (explicitOpening === "\\[") { return delimitedMath(markdown, start, "\\[", "\\]", false, missingClosers) } @@ -527,10 +526,14 @@ function markdownDestinationEnd(source: string, start: number): number | null { if (angle) return start + angle.length } - const remainder = source.slice(start) - if (/^(?:https?:\/\/|mailto:)/u.test(remainder)) { - const match = remainder.match(/^[^\s<>]+/u) - return match ? start + match[0].length : null + if ( + source.startsWith("http://", start) + || source.startsWith("https://", start) + || source.startsWith("mailto:", start) + ) { + let end = start + while (end < source.length && !/[\s<>]/u.test(source[end] ?? "")) end += 1 + return end } return null }