fix: blockquote and list whitespace handling in markdown rendering

This commit is contained in:
mitchell 2026-05-12 16:34:13 -04:00
parent af52e89928
commit 7367cfdfe3

View file

@ -64,13 +64,14 @@ export async function renderMarkdown(
const lines = Bun.wrapAnsi(`\n${children.trim()}`, columns - 2, { const lines = Bun.wrapAnsi(`\n${children.trim()}`, columns - 2, {
trim: false, trim: false,
}) })
.replace(/\n(\s?[a-zA-Z_-]+\s?)\n/g, "\n$1")
.replace(/\n+/g, "\n") .replace(/\n+/g, "\n")
.concat("\n") .concat("\n")
.split("\n"); .split("\n");
const toRender = lines const toRender = lines
.map((line) => `${chalk.dim("> ")}${line}`) .map((line) => `> ${line}`)
.join("\n") .join("\n")
.replace(/(\n> +\S+) *\n> */g, "$1 ")
.replaceAll(">", chalk.dim(">"))
.concat("\n\n"); .concat("\n\n");
return toRender; return toRender;
}, },
@ -91,9 +92,7 @@ export async function renderMarkdown(
return `\`\`\`${chalk.blue(meta?.language ?? "")}\n${toRender}\`\`\`\n`; return `\`\`\`${chalk.blue(meta?.language ?? "")}\n${toRender}\`\`\`\n`;
}, },
list: (children, { depth }) => { list: (children, { depth }) => {
const trimmed = children const trimmed = children.replace(/\n+/g, "\n");
.replace(/\n+/g, "\n")
.replace(/\n(\s?[a-zA-Z_-]+\s?)\n/g, "\n$1");
return depth === 0 ? `${trimmed}\n` : `\n${trimmed}`; return depth === 0 ? `${trimmed}\n` : `\n${trimmed}`;
}, },
listItem: (children, { index, depth, ordered, start, checked }) => { listItem: (children, { index, depth, ordered, start, checked }) => {
@ -112,6 +111,7 @@ export async function renderMarkdown(
.split("\n") .split("\n")
.map((line) => (line.length > 0 ? ` ${line}` : "")) .map((line) => (line.length > 0 ? ` ${line}` : ""))
.join("\n") .join("\n")
.replace(/(\n +\S+) *\n */g, "$1 ")
.concat("\n"); .concat("\n");
}, },
hr: () => `${chalk.dim("─".repeat(columns))}\n\n`, hr: () => `${chalk.dim("─".repeat(columns))}\n\n`,