Skip to content

Commit 7f9f249

Browse files
committed
fix: refine MarkdownV2 escaping to prevent excessive backslashes
1 parent 5a1dba7 commit 7f9f249

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

packages/shared/src/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,16 @@ export async function markdownToHtml(s: string): Promise<string> {
193193

194194
export function sanitizeMarkdownV2(text: string, isInsideLink = false): string {
195195
// Standard MarkdownV2 characters that MUST be escaped outside of code/pre
196-
let escaped = text.replace(/([_*[\]()~`>#+\-=|{}.!\\])/g, '\\$1');
196+
// First, we unescape any existing escapes to avoid double-escaping
197+
// We keep the full list of potentially escaped characters here for robustness
198+
let unescaped = text.replace(/\\([_*[\]()~`>#+\-=|{}.!\\])/g, '$1');
199+
200+
// We only escape characters that are truly likely to cause parsing issues or are essential markup
201+
// Reduced list: _ * [ ] ~ ` \ |
202+
let escaped = unescaped.replace(/([_*[\]~`\\|])/g, '\\$1');
203+
197204
if (isInsideLink) {
198-
// Inside links, additional characters need escaping
205+
// Inside links, additional characters need escaping to avoid breaking the link syntax
199206
escaped = escaped.replace(/([()])/g, '\\$1');
200207
}
201208
return escaped;
@@ -223,7 +230,7 @@ export async function markdownToMarkdownV2(s: string): Promise<string> {
223230
for (let i = 0; i < items.length; i++) {
224231
const item = items[i];
225232
const prefix = ordered
226-
? `${start !== '' && start !== undefined ? Number(start) + i : i + 1}\\. `
233+
? `${start !== '' && start !== undefined ? Number(start) + i : i + 1}. `
227234
: '• ';
228235
result += `${prefix}${renderer.listitem(item)}\n`;
229236
}

0 commit comments

Comments
 (0)