style: improve markdown rendering output formatting
- Use terminal-link for clickable hyperlinks - Reduce heading color brightness - Remove extra trailing newlines from code blocks - Fix horizontal rule spacing - Wrap codespan content in backticks - Suppress language error warnings from highlighter
This commit is contained in:
parent
c0ab652135
commit
0727c5ebec
4 changed files with 32 additions and 14 deletions
|
|
@ -81,7 +81,7 @@ export default class Renderer {
|
|||
|
||||
const [columns] = pipe.getWindowSize();
|
||||
const rendered = await renderMarkdown(str, columns);
|
||||
pipe.write(rendered);
|
||||
pipe.write(rendered.replace(/\n\n$/, "\n"));
|
||||
}
|
||||
|
||||
async renderStream(stream: AsyncIterableStream<TextStreamPart<ToolSet>>) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,13 @@
|
|||
import chalk from "chalk";
|
||||
import { highlight } from "cli-highlight";
|
||||
import CliTable3 from "cli-table3";
|
||||
import terminalLink from "terminal-link";
|
||||
|
||||
const originalErr = console.error;
|
||||
console.error = (...data: unknown[]) => {
|
||||
if (data.join("").includes("Could not find the language")) return;
|
||||
originalErr(...data);
|
||||
};
|
||||
|
||||
export async function renderMarkdown(
|
||||
text: PromiseLike<string> | string,
|
||||
|
|
@ -13,12 +20,12 @@ export async function renderMarkdown(
|
|||
return Bun.markdown.render(md, {
|
||||
heading: (children, { level }) => {
|
||||
const colors = [
|
||||
chalk.cyanBright.underline,
|
||||
chalk.blueBright,
|
||||
chalk.greenBright,
|
||||
chalk.cyan,
|
||||
chalk.cyan.underline,
|
||||
chalk.blue,
|
||||
chalk.green,
|
||||
chalk.cyanBright,
|
||||
chalk.blueBright,
|
||||
chalk.greenBright,
|
||||
];
|
||||
const color = colors[level - 1] ?? chalk.white;
|
||||
return color.bold(`${"#".repeat(level)} ${children}\n\n`);
|
||||
|
|
@ -44,7 +51,7 @@ export async function renderMarkdown(
|
|||
} catch {}
|
||||
}
|
||||
|
||||
return `\`\`\`${chalk.green(meta?.language)}\n${toRender}\n\`\`\`\n`;
|
||||
return `\`\`\`${chalk.green(meta?.language)}\n${toRender}\n\`\`\``;
|
||||
},
|
||||
list: (children, { depth }) =>
|
||||
`${depth > 0 ? "\n" : ""}${children}${depth === 0 ? "\n" : ""}`,
|
||||
|
|
@ -61,15 +68,12 @@ export async function renderMarkdown(
|
|||
}
|
||||
return `${indent}${marker} ${children}\n`;
|
||||
},
|
||||
hr: () => `\n${chalk.dim("─".repeat(columns))}\n`,
|
||||
hr: () => `${chalk.dim("─".repeat(columns))}\n\n`,
|
||||
table: (_children) => {
|
||||
const toRender = currentTable.toString();
|
||||
currentTable = new CliTable3();
|
||||
return `${toRender}\n\n`;
|
||||
},
|
||||
thead: () => {
|
||||
return "";
|
||||
},
|
||||
tr: (children) => {
|
||||
if (children) currentTable.push(children.split("|").slice(1));
|
||||
return "";
|
||||
|
|
@ -82,9 +86,9 @@ export async function renderMarkdown(
|
|||
strong: (children) => chalk.bold(children),
|
||||
emphasis: (children) => chalk.italic(children),
|
||||
strikethrough: (children) => chalk.grey.strikethrough(children),
|
||||
codespan: (children) => chalk.bgWhite.black(children),
|
||||
codespan: (children) => chalk.bgWhite.black(`\`${children}\``),
|
||||
link: (children, { href }) =>
|
||||
chalk.blueBright(`${children}: ${chalk.underline(href)}`),
|
||||
chalk.blueBright.underline(terminalLink(children, href)),
|
||||
image: () => "",
|
||||
text: (children) => children,
|
||||
html: () => "",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue