Skip to content

The agent that grows with you

refactor(desktop): single-pass unbalanced-paren trim in raw-URL autolinker

harjothd51727f9ba38
diff --git a/apps/desktop/src/lib/markdown-preprocess.ts b/apps/desktop/src/lib/markdown-preprocess.ts
index 3881446b1b5..eb606f9c290 100644
--- a/apps/desktop/src/lib/markdown-preprocess.ts
+++ b/apps/desktop/src/lib/markdown-preprocess.ts
@@ -208,29 +208,32 @@ function isUrlOnlyBlock(lines: string[]): boolean {
return nonEmpty.length > 0 && nonEmpty.every(line => URL_ONLY_LINE_RE.test(line))
}
+// Strip only the *unbalanced* trailing `)` — the closing paren of a prose
+// wrapper like `(https://example.com/page)`, which the bare-URL matcher
+// greedily captures into the href. Parens that belong to the URL itself
+// (`…/wiki/Foo_(bar)`) stay intact: a trailing `)` is peeled only while the
+// URL holds more `)` than `(`. Count once, then trim from the end — the loop
+// only ever removes trailing closers, so `opens` never changes.
function trimUnbalancedTrailingParens(url: string): string {
- let result = url
-
- while (result.endsWith(')')) {
- let opens = 0
- let closes = 0
-
- for (const char of result) {
- if (char === '(') {
- opens += 1
- } else if (char === ')') {
- closes += 1
- }
+ let opens = 0
+ let closes = 0
+
+ for (const char of url) {
+ if (char === '(') {
+ opens += 1
+ } else if (char === ')') {
+ closes += 1
}
+ }
- if (closes <= opens) {
- break
- }
+ let end = url.length
- result = result.slice(0, -1)
+ while (closes > opens && url[end - 1] === ')') {
+ closes -= 1
+ end -= 1
}
- return result
+ return url.slice(0, end)
}
function autoLinkRawUrls(text: string): string {