refactor: replace cli-highlight with shiki
This commit is contained in:
parent
9cd8ccd4e2
commit
d822ba2141
4 changed files with 229 additions and 51 deletions
|
|
@ -1,13 +1,16 @@
|
|||
import chalk from "chalk";
|
||||
import { highlight } from "cli-highlight";
|
||||
import CliTable3 from "cli-table3";
|
||||
import {
|
||||
type BundledLanguage,
|
||||
type BundledTheme,
|
||||
createHighlighter,
|
||||
type HighlighterGeneric,
|
||||
} from "shiki";
|
||||
import terminalLink from "terminal-link";
|
||||
import { codeToANSI } from "./codeToANSI";
|
||||
|
||||
const originalErr = console.error;
|
||||
console.error = (...data: unknown[]) => {
|
||||
if (data.join("").includes("Could not find the language")) return;
|
||||
originalErr(...data);
|
||||
};
|
||||
let highlighter: HighlighterGeneric<BundledLanguage, BundledTheme> | undefined;
|
||||
let highligherLangs = "";
|
||||
|
||||
export async function renderMarkdown(
|
||||
text: PromiseLike<string> | string,
|
||||
|
|
@ -15,6 +18,20 @@ export async function renderMarkdown(
|
|||
): Promise<string> {
|
||||
const md = await text;
|
||||
|
||||
const langs = md
|
||||
.match(/```[a-z]+\n/g)
|
||||
?.map((lang) => lang.replace("```", "").trimEnd());
|
||||
|
||||
if (langs && langs?.join() !== highligherLangs) {
|
||||
try {
|
||||
highligherLangs = langs.join();
|
||||
highlighter = await createHighlighter({
|
||||
langs,
|
||||
themes: ["kanagawa-wave", "kanagawa-lotus"],
|
||||
});
|
||||
} catch {}
|
||||
}
|
||||
|
||||
let currentTable = new CliTable3();
|
||||
|
||||
return Bun.markdown.render(md, {
|
||||
|
|
@ -41,20 +58,21 @@ export async function renderMarkdown(
|
|||
code: (children, meta) => {
|
||||
let toRender = children;
|
||||
|
||||
if (meta?.language) {
|
||||
if (highlighter && meta?.language) {
|
||||
try {
|
||||
toRender = highlight(children, {
|
||||
language: meta.language,
|
||||
ignoreIllegals: true,
|
||||
theme: { string: chalk.greenBright },
|
||||
});
|
||||
toRender = codeToANSI(
|
||||
highlighter,
|
||||
children,
|
||||
meta.language as BundledLanguage,
|
||||
"kanagawa-wave",
|
||||
);
|
||||
} catch {}
|
||||
}
|
||||
|
||||
return `\`\`\`${chalk.green(meta?.language)}\n${toRender}\n\`\`\`\n`;
|
||||
return `\`\`\`${chalk.blue(meta?.language)}\n${toRender}\n\`\`\`\n`;
|
||||
},
|
||||
list: (children, { depth }) =>
|
||||
`${depth > 0 ? "\n" : ""}${children}${depth === 0 ? "\n" : ""}`,
|
||||
`${depth > 0 ? "\n" : ""}${children.trimEnd()}${depth === 0 ? "\n\n" : ""}`,
|
||||
listItem: (children, { index, depth, ordered, start, checked }) => {
|
||||
const indent = " ".repeat(depth);
|
||||
let marker = "";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue