From 3681244d1526378765e0fb8dac9d173b490ade09 Mon Sep 17 00:00:00 2001 From: youdie006 Date: Thu, 29 Oct 2026 12:42:29 +0900 Subject: [PATCH] fix: keep inline code that holds a backtick intact in answers The answer reader wrapped inline code in one backtick on each side, so the code a`b came back as `a`b`, which markdown reads as a one-letter span and a stray backtick (measured 2026-10-29: the page showed the codes ``a`b`` and ``echo `date` ``). Inline code now goes in a backtick run longer than any inside it, padded with a space when it starts or ends with a backtick or has a space at both ends that markdown would strip, as CommonMark specifies. Verified: failing tests first, then tsc --noEmit and npm test (147 files, 1829 passed, 3 skipped; one earlier full run failed browser-slow-vs-dead's tiny-timeout case under load, which passed three times alone and in the rerun). Live, Instant, branch build: the answer came back as "Run ``echo `date` `` to print the date, or ``a`b`` for short.", matching what the page showed. Claude-Session: https://claude.ai/code/session_01EMqNwnnhHXznd2P2PDsaDV --- CHANGELOG.md | 1 + src/chatgpt-browser.ts | 13 ++++++++++++- tests/answer-markdown.test.ts | 22 ++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1456938a..70016826 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Answers lost their horizontal rules. ChatGPT's newer answer renderer draws a `---` rule as an empty `div[data-d-component=divider]`, and prodex read only `
`, so an answer showing a rule between two sections came back with the sections run together. The rule now comes back as `---`. Found 2026-10-29 comparing a live answer with the page. +- Inline code that held a backtick came back as broken markdown. An answer showing the code ``a`b`` or ``echo `date` `` returned `` `a`b` `` and `` `echo `date` ` ``, which markdown reads as a different span followed by stray backticks. Inline code is now wrapped in a backtick run longer than any inside it, padded with a space where markdown needs one. Found 2026-10-29 comparing a live answer with the page. ## 0.40.47 diff --git a/src/chatgpt-browser.ts b/src/chatgpt-browser.ts index c0046acb..713d9f4f 100644 --- a/src/chatgpt-browser.ts +++ b/src/chatgpt-browser.ts @@ -552,6 +552,17 @@ export const CHATGPT_RENDERED_MESSAGE_TEXT_JS = ` const link = findAll(node, (n) => tagOf(n) === "a" && (attr(n, "href") || "").startsWith("http"))[0]; return link ? "[" + label + "](" + untracked(attr(link, "href")) + ")" : "[" + label + "]"; }; + // Inline code goes in a backtick run longer than any inside it, padded + // with a space when it starts or ends with a backtick, or has a space at + // both ends that markdown would strip. One backtick each side turned the + // code a\`b into a one-letter span and a stray backtick (measured + // 2026-10-29). + const inlineCode = (value) => { + const longestRun = (value.match(new RegExp(BT + "+", "g")) || []).reduce((most, run) => Math.max(most, run.length), 0); + const ticks = BT.repeat(longestRun + 1); + const pad = value.startsWith(BT) || value.endsWith(BT) || (value.startsWith(" ") && value.endsWith(" ") && value.trim() !== "") ? " " : ""; + return ticks + pad + value + pad + ticks; + }; const inline = (node, keep) => { const keepLines = keepsLineBreaks(node, keep === true); let out = ""; @@ -572,7 +583,7 @@ export const CHATGPT_RENDERED_MESSAGE_TEXT_JS = ` if (isCheckbox(node)) return (attr(node, "aria-checked") === "true" || attr(node, "checked") !== null ? "[x]" : "[ ]") + " "; if (isMath(node)) return isDisplayMath(node) ? "$$" + mathSource(node) + "$$" : "$" + mathSource(node) + "$"; if (attr(node, "data-chatgpt-copy-reference") !== null || isSourceBadge(node)) return pill(node); - if (attr(node, "data-markdown-copy") === "inline-code" || tag === "code") return BT + (node.textContent || "") + BT; + if (attr(node, "data-markdown-copy") === "inline-code" || tag === "code") return inlineCode(node.textContent || ""); if (tag === "br") return NL; const inner = inline(node, keep); // The newer renderer marks bold as span[data-d-default-strong], italic diff --git a/tests/answer-markdown.test.ts b/tests/answer-markdown.test.ts index f10987ee..ab82d28c 100644 --- a/tests/answer-markdown.test.ts +++ b/tests/answer-markdown.test.ts @@ -226,6 +226,28 @@ describe("line breaks the page shows", () => { // span[data-d-text-decoration=line-through], as it marks bold with // data-d-default-strong. Only bold was read, so "*better*" and "~~old~~" came // back as plain words and "***brilliant***" as "**brilliant**". +describe("inline code holding backticks", () => { + // Measured 2026-10-29: the page showed the code ``a`b`` and ``echo `date` ``, + // and the answer came back as `a`b` and `echo `date` `, which markdown reads + // as different spans with stray backticks between them. + const code = (value: string) => el("code", { "data-d-component": "code" }, [value]); + + it("wraps it in a longer backtick run", () => { + const root = el("div", {}, [el("p", {}, ["Run ", code("echo `date` "), " or ", code("a`b"), "."])]); + expect(render(root)).toBe("Run ``echo `date` `` or ``a`b``."); + }); + + it("pads code that starts or ends with a backtick, or with spaces on both sides", () => { + const root = el("div", {}, [el("p", {}, ["A lone ", code("`"), ", ", code("``x"), " and ", code(" x "), "."])]); + expect(render(root)).toBe("A lone `` ` ``, ``` ``x ``` and ` x `."); + }); + + it("leaves plain inline code as it was", () => { + const root = el("div", {}, [el("p", {}, ["Call ", code("add(2, 3)"), "."])]); + expect(render(root)).toBe("Call `add(2, 3)`."); + }); +}); + describe("emphasis in the newer renderer", () => { const text = (attrs: Record, value: string) => el("span", { "data-d-component": "text", ...attrs }, [value]);