From 198d2f000f9ae2e3cd7211fe9883206d4420fdd5 Mon Sep 17 00:00:00 2001 From: Momo <35096042+momothemage@users.noreply.github.com> Date: Sun, 19 Jul 2026 11:33:05 +0800 Subject: [PATCH] fix(telegram): preserve authored file-reference links (#105911) * fix(telegram): preserve authored file links * fix(telegram): preserve authored file links after wrapping * refactor(telegram): rely on markdown link provenance --------- Co-authored-by: Peter Steinberger --- extensions/telegram/src/format-render.ts | 6 ++- extensions/telegram/src/format.ts | 26 ++++----- .../telegram/src/format.wrap-md.test.ts | 43 ++++++++------- extensions/telegram/src/rich-blocks.test.ts | 7 +++ extensions/telegram/src/rich-blocks.ts | 33 +++++++++--- packages/markdown-core/src/ir-spans.ts | 42 ++++++++++++--- packages/markdown-core/src/ir.links.test.ts | 53 +++++++++++++++++++ packages/markdown-core/src/ir.ts | 30 ++++++----- .../src/render-aware-chunking.ts | 27 +++++++--- packages/markdown-core/src/render.ts | 32 ++++++++--- 10 files changed, 224 insertions(+), 75 deletions(-) create mode 100644 packages/markdown-core/src/ir.links.test.ts diff --git a/extensions/telegram/src/format-render.ts b/extensions/telegram/src/format-render.ts index 3530bf595d4a..10f8d6f1fec8 100644 --- a/extensions/telegram/src/format-render.ts +++ b/extensions/telegram/src/format-render.ts @@ -15,7 +15,11 @@ export function renderTelegramMarkdownIR( ir: MarkdownIR, options: { escapeText: (text: string) => string; - buildLink: (link: MarkdownLinkSpan, text: string) => TelegramRenderLink | null; + buildLink: ( + link: MarkdownLinkSpan, + text: string, + context: { origin: "authored" | "linkify" }, + ) => TelegramRenderLink | null; buildCodeBlockOpen: (span: { language?: string }) => string; }, ): string { diff --git a/extensions/telegram/src/format.ts b/extensions/telegram/src/format.ts index d036d9c6c806..663fa353b091 100644 --- a/extensions/telegram/src/format.ts +++ b/extensions/telegram/src/format.ts @@ -49,7 +49,11 @@ function isTelegramRichLinkHref(href: string): boolean { * * Excluded: .ai, .io, .tv, .fm (popular domain TLDs like x.ai, vercel.io, github.io) */ -function buildTelegramLink(link: MarkdownLinkSpan, text: string) { +function buildTelegramLink( + link: MarkdownLinkSpan, + text: string, + context: { origin: "authored" | "linkify" }, +) { const href = link.href.trim(); if (!href) { return null; @@ -64,7 +68,7 @@ function buildTelegramLink(link: MarkdownLinkSpan, text: string) { } // Suppress auto-linkified file references (e.g. README.md → http://README.md) const label = text.slice(link.start, link.end); - if (isAutoLinkedFileRef(href, label)) { + if (context.origin === "linkify" && isAutoLinkedFileRef(href, label)) { return null; } const safeHref = escapeHtmlAttr(href); @@ -178,7 +182,6 @@ function escapeRegex(str: string): string { return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); } -const AUTO_LINKED_ANCHOR_PATTERN = /]*>\1<\/a>/gi; const HTML_MODE_TAG_PATTERN = /^<(\/?)([a-zA-Z][a-zA-Z0-9-]*)([^<>]*)>$/; const ESCAPED_HTML_TAG_PATTERN = /<(\/?)([a-zA-Z][a-zA-Z0-9-]*)(.*?)>/g; const TELEGRAM_HTML_ANCHOR_PATTERN = @@ -499,15 +502,6 @@ function wrapSegmentFileRefs( } export function wrapFileReferencesInHtml(html: string): string { - // Safety-net: de-linkify auto-generated anchors where href="http://