prevent double blank lines after markdown code blocks (#173)

This commit is contained in:
Markus Ylisiurunen 2025-12-13 03:04:49 +02:00 committed by GitHub
parent 36dbb2410e
commit db886746b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 1 deletions

View file

@ -267,7 +267,9 @@ export class Markdown implements Component {
lines.push(" " + this.theme.codeBlock(codeLine));
}
lines.push(this.theme.codeBlockBorder("```"));
lines.push(""); // Add spacing after code blocks
if (nextTokenType !== "space") {
lines.push(""); // Add spacing after code blocks (unless space token follows)
}
break;
}

View file

@ -253,6 +253,38 @@ describe("Markdown component", () => {
});
});
describe("Spacing after code blocks", () => {
it("should have only one blank line between code block and following paragraph", () => {
const markdown = new Markdown(
`hello world
\`\`\`js
const hello = "world";
\`\`\`
again, hello world`,
0,
0,
defaultMarkdownTheme,
);
const lines = markdown.render(80);
const plainLines = lines.map((line) => line.replace(/\x1b\[[0-9;]*m/g, "").trimEnd());
const closingBackticksIndex = plainLines.indexOf("```");
assert.ok(closingBackticksIndex !== -1, "Should have closing backticks");
const afterBackticks = plainLines.slice(closingBackticksIndex + 1);
const emptyLineCount = afterBackticks.findIndex((line) => line !== "");
assert.strictEqual(
emptyLineCount,
1,
`Expected 1 empty line after code block, but found ${emptyLineCount}. Lines after backticks: ${JSON.stringify(afterBackticks.slice(0, 5))}`,
);
});
});
describe("HTML-like tags in text", () => {
it("should render content with HTML-like tags as text", () => {
// When the model emits something like <thinking>content</thinking> in regular text,