From 6bf342a861a008844e2874d29751a5720f44d8ac Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Thu, 27 Aug 2026 23:12:50 -0400 Subject: [PATCH] refactor(core): consolidate HTML inline handling (#45649) --- packages/core/src/tool/html-markdown.ts | 41 +++++++++---------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/packages/core/src/tool/html-markdown.ts b/packages/core/src/tool/html-markdown.ts index a4ea1611bdf..ea18530f17e 100644 --- a/packages/core/src/tool/html-markdown.ts +++ b/packages/core/src/tool/html-markdown.ts @@ -197,6 +197,12 @@ export function convertHTMLToMarkdown(html: string) { .trim() .replace(/([\\"])/g, "\\$1")}"` : "" + const inlineMarker = (name: string) => { + if (name === "strong" || name === "b") return "**" + if (name === "em" || name === "i") return "*" + if (name === "s" || name === "strike" || name === "del") return "~~" + return undefined + } const finishCode = (code: NonNullable) => { if (code.inline && !code.text) return let backticks = 0 @@ -321,20 +327,9 @@ export function convertHTMLToMarkdown(html: string) { block() return } - if (name === "strong" || name === "b") { - inline("**", true) - frame.marker = { index: output.length - 1, block: blockCount, previous: activeMarker } - activeMarker = frame.marker - return - } - if (name === "em" || name === "i") { - inline("*", true) - frame.marker = { index: output.length - 1, block: blockCount, previous: activeMarker } - activeMarker = frame.marker - return - } - if (name === "s" || name === "strike" || name === "del") { - inline("~~", true) + const marker = inlineMarker(name) + if (marker) { + inline(marker, true) frame.marker = { index: output.length - 1, block: blockCount, previous: activeMarker } activeMarker = frame.marker return @@ -509,16 +504,8 @@ export function convertHTMLToMarkdown(html: string) { return } if (name === "dd") return block() - if ( - name === "strong" || - name === "b" || - name === "em" || - name === "i" || - name === "s" || - name === "strike" || - name === "del" - ) { - const value = name === "strong" || name === "b" ? "**" : name === "em" || name === "i" ? "*" : "~~" + const marker = inlineMarker(name) + if (marker) { const trailingSpace = pendingSpace pendingSpace = false if (frame.marker) activeMarker = frame.marker.previous @@ -527,15 +514,15 @@ export function convertHTMLToMarkdown(html: string) { pendingSpace = trailingSpace || frame.marker.leadingSpace === true return } - inline(value) + inline(marker) pendingSpace = trailingSpace || frame.marker?.leadingSpace === true return } if (name === "a") { if (frame.link && (activeLink === frame.link || !activeLink)) { activeLink = frame.link - if (linkOpen) append(`](${destination(frame.link?.href ?? "")}${title(frame.link?.title)})`) - else if (last && last !== "\n") append(`](${destination(frame.link?.href ?? "")}${title(frame.link?.title)})`) + if (linkOpen || (last && last !== "\n")) + append(`](${destination(frame.link.href)}${title(frame.link.title)})`) linkOpen = false activeLink = undefined }