diff --git a/packages/shared/src/markdownPipeline.test.ts b/packages/shared/src/markdownPipeline.test.ts index 9671225731e22..dfe2471bf206c 100644 --- a/packages/shared/src/markdownPipeline.test.ts +++ b/packages/shared/src/markdownPipeline.test.ts @@ -10,6 +10,7 @@ import { CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS, remarkAssistantInsights, remarkEnableOrderedListNumbers, + shouldPreserveAssistantLineBreaks, } from "./markdownPipeline.ts"; it.each([ @@ -712,3 +713,33 @@ it("links complete absolute paths in prose and skips shortened ones", () => { expect(links(`[notes](${path}) and \`${path}\``)).toEqual([path]); expect(links(`\`\`\`\n${path}\n\`\`\``)).toEqual([]); }); + +it.each([ + [ + "lettered lines", + "Three calls:\nA. Merge the gateway PR.\nB. Hold the window.\nC. Retire Go.", + true, + ], + ["parenthesized letters", "a) one\nb) two", true], + ["bullet characters", "• one\n• two", true], + ["insight block", "★ Insight ─────\nfirst\nsecond", true], + ["one lettered line", "A. Only one item here.\nThen prose.", false], + ["lettered paragraphs", "A. one\n\nB. two", false], + ["real markdown list", "- one\n- two\n1. three", false], + ["plain paragraph", "A normal\nmarkdown paragraph", false], + ["inside a code fence", "```\nA. one\nB. two\n```", false], +] as const)("preserves assistant line breaks for %s: %s", (_, text, expected) => { + expect(shouldPreserveAssistantLineBreaks(text)).toBe(expected); +}); + +it("keeps lettered lines apart once breaks are on", () => { + const text = "A. Merge the gateway PR.\nB. Hold the window."; + const tree = unified() + .use(remarkParse) + .use(CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS) + .runSync(unified().use(remarkParse).parse(text)) as Root; + const paragraph = tree.children[0]; + expect(paragraph?.type).toBe("paragraph"); + const kinds = paragraph && "children" in paragraph ? paragraph.children.map((c) => c.type) : []; + expect(kinds).toContain("break"); +}); diff --git a/packages/shared/src/markdownPipeline.ts b/packages/shared/src/markdownPipeline.ts index ad77b6d928aee..66225aabdbdee 100644 --- a/packages/shared/src/markdownPipeline.ts +++ b/packages/shared/src/markdownPipeline.ts @@ -744,8 +744,29 @@ function remarkNormalizeLinksAndTagInlineCode() { }; } +// Markdown has no lettered or "•" lists, so lines such as "A. merge it" and +// "B. hold it" are one paragraph to CommonMark and render as a solid block. +const PSEUDO_LIST_LINE = /^ {0,3}(?:\(?[A-Za-z]\)|[A-Za-z]\.|[•·▪◦‣])[ \t]+\S/u; +const FENCE_LINE = /^ {0,3}(?:`{3,}|~{3,})/u; + +function hasPseudoListLines(text: string): boolean { + let inFence = false; + let previousMarked = false; + for (const line of text.split(/\r?\n/u)) { + if (FENCE_LINE.test(line)) { + inFence = !inFence; + previousMarked = false; + continue; + } + const marked = !inFence && PSEUDO_LIST_LINE.test(line); + if (marked && previousMarked) return true; + previousMarked = marked; + } + return false; +} + export function shouldPreserveAssistantLineBreaks(text: string): boolean { - return /^★ Insight(?:\s|─)/mu.test(text); + return /^★ Insight(?:\s|─)/mu.test(text) || hasPseudoListLines(text); } export function remarkAssistantInsights(this: Processor) {