refactor: move wrapAnsi from blockquote/listitem to text tokens

This commit is contained in:
mitchell 2026-05-27 18:33:14 -04:00
parent c277418349
commit fc4934af4a

View file

@ -40,24 +40,19 @@ class LmcRenderer extends Renderer {
];
const color = colors[depth - 1] ?? chalk.white;
const text = this.parser.parseInline(tokens);
return wrapAnsi(
color.bold(`${"#".repeat(depth)} ${text}\n`),
this.columns,
{ trim: false },
);
return color.bold(`${"#".repeat(depth)} ${text}\n`);
}
override paragraph({ tokens }: Tokens.Paragraph) {
const text = this.parser.parseInline(tokens);
return wrapAnsi(`\n${text}\n`, this.columns - 2, { trim: false });
const text = wrapAnsi(this.parser.parseInline(tokens), this.columns - 4);
return `\n${text}\n`;
}
override blockquote({ tokens }: Tokens.Blockquote) {
let text = this.parser.parse(tokens).trim();
if (!text.includes("\n")) text = wrapAnsi(text, this.columns - 2);
const text = this.parser.parse(tokens).trim();
const lines = `\n${text}\n`.split("\n");
const toRender = lines.map((line) => `> ${line}`).join("\n");
return `${toRender.replaceAll(">", chalk.dim(">"))}\n`;
return `\n${toRender.replaceAll(">", chalk.dim(">"))}\n`;
}
override code({ text, lang }: Tokens.Code) {
@ -96,8 +91,7 @@ class LmcRenderer extends Renderer {
marker = "•";
}
let item = `${marker} ${text}`;
if (!item.includes("\n")) item = wrapAnsi(item, this.columns - 2);
const item = `${marker} ${text}`;
return item
.split("\n")
.map((line) => (line.length > 0 ? ` ${line}` : ""))
@ -167,7 +161,8 @@ class LmcRenderer extends Renderer {
override text(token: Tokens.Text | Tokens.Escape) {
if (token.type === "text" && token.tokens) {
return this.parser.parseInline(token.tokens);
const parsed = this.parser.parseInline(token.tokens);
return wrapAnsi(parsed, this.columns - 12);
}
return token.text
.replaceAll("$\\rightarrow$", "-->")