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