From e26bbbb62fd5a186f35cdcef55c8871833c36869 Mon Sep 17 00:00:00 2001 From: Neil Date: Fri, 18 Sep 2026 05:09:49 -0700 Subject: [PATCH] fix(editor): retain balanced markdown destinations --- .../editor/rich-markdown-literal-serialization.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/components/editor/rich-markdown-literal-serialization.ts b/src/renderer/src/components/editor/rich-markdown-literal-serialization.ts index 01f09842ef3..dbcba3eb5b2 100644 --- a/src/renderer/src/components/editor/rich-markdown-literal-serialization.ts +++ b/src/renderer/src/components/editor/rich-markdown-literal-serialization.ts @@ -54,6 +54,7 @@ function hasBare(markdown: string, chars: string): boolean { } function destNeedsEscape(dest: string): boolean { + let depth = 0 let oddBackslash = false for (let i = 0; i < dest.length; i += 1) { const character = dest[i] @@ -62,11 +63,16 @@ function destNeedsEscape(dest: string): boolean { if (!bare) { continue } - if (character === '(' || character === ')') { - return true + if (character === '(') { + depth += 1 + } else if (character === ')') { + if (depth === 0) { + return true + } + depth -= 1 } } - return false + return depth > 0 } function escapeLineLeading(markdown: string): string {