Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions packages/shared/src/markdownPipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS,
remarkAssistantInsights,
remarkEnableOrderedListNumbers,
shouldPreserveAssistantLineBreaks,
} from "./markdownPipeline.ts";

it.each([
Expand Down Expand Up @@ -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");
});
23 changes: 22 additions & 1 deletion packages/shared/src/markdownPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading