From 3e96007f1ffacc41ad9548a97977428ae130c9a7 Mon Sep 17 00:00:00 2001 From: mitchell Date: Wed, 3 Jun 2026 15:52:49 -0400 Subject: [PATCH] refactor: migrate shiki to core API with dynamic language/theme loading - Update code block regex to support symbols/numbers in language identifiers - Adjust codespan background color to a softer shade --- index.ts | 6 ++++++ src/render/codeToANSI.ts | 4 ++-- src/render/markdown.ts | 28 ++++++++++++++++------------ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/index.ts b/index.ts index 14530ef..dd17b66 100644 --- a/index.ts +++ b/index.ts @@ -15,6 +15,7 @@ const { values: args, positionals } = parseArgs({ n: { type: "boolean" }, config: { type: "boolean" }, c: { type: "boolean" }, + v: { type: "boolean" }, plain: { type: "boolean" }, plainOut: { type: "boolean" }, plainErr: { type: "boolean" }, @@ -30,6 +31,11 @@ async function main() { const modelFlag = args.model ?? args.m; const configFlag = args.config ?? args.c; + if (args.v) { + console.log("0.0.0"); + return; + } + const config = await loadConfig(configFlag); if (configFlag || !config) { diff --git a/src/render/codeToANSI.ts b/src/render/codeToANSI.ts index d5a081e..fc54b8e 100644 --- a/src/render/codeToANSI.ts +++ b/src/render/codeToANSI.ts @@ -24,7 +24,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ import c from "chalk"; -import type { BundledLanguage, BundledTheme, HighlighterGeneric } from "shiki"; +import type { BundledLanguage, BundledTheme, HighlighterCore } from "shiki"; // From @shikijs/vscode-textmate enum FontStyle { @@ -37,7 +37,7 @@ enum FontStyle { } export function codeToANSI( - highlighter: HighlighterGeneric, + highlighter: HighlighterCore, code: string, lang: BundledLanguage, theme: BundledTheme, diff --git a/src/render/markdown.ts b/src/render/markdown.ts index 9c6d4f8..905c003 100644 --- a/src/render/markdown.ts +++ b/src/render/markdown.ts @@ -1,18 +1,15 @@ import { Cell, Table } from "@cliffy/table"; import chalk from "chalk"; import { marked, Renderer, type Tokens } from "marked"; -import { - type BundledLanguage, - type BundledTheme, - createHighlighter, - type HighlighterGeneric, -} from "shiki"; +import type { BundledLanguage, HighlighterCore } from "shiki"; +import { createHighlighterCore } from "shiki/core"; +import { createOnigurumaEngine } from "shiki/engine/oniguruma"; import stringWidth from "string-width"; import terminalLink from "terminal-link"; import wrapAnsi from "wrap-ansi"; import { codeToANSI } from "./codeToANSI"; -let highlighter: HighlighterGeneric | undefined; +let highlighter: HighlighterCore | undefined; let highligherLangs = ""; class SWCell extends Cell { @@ -147,7 +144,7 @@ class LmcRenderer extends Renderer { } override codespan({ text }: Tokens.Codespan) { - return chalk.bgYellow.black.bold(text); + return chalk.bgRgb(165, 165, 190).black.bold(text); } override link({ href, tokens }: Tokens.Link) { @@ -194,15 +191,22 @@ export async function renderMarkdown( const md = await text; const langs = md - .match(/```[a-z]+\n/g) + .match(/```\S+\n/g) ?.map((lang) => lang.replace("```", "").trimEnd()); if (langs && langs?.join() !== highligherLangs) { + const { bundledThemes, bundledLanguages, bundledLanguagesAlias } = + await import("shiki/bundle/full"); try { highligherLangs = langs.join(); - highlighter = await createHighlighter({ - langs, - themes: ["kanagawa-wave", "kanagawa-lotus"], + highlighter = await createHighlighterCore({ + langs: langs.map( + (lang) => + bundledLanguages[lang as BundledLanguage] || + bundledLanguagesAlias[lang], + ), + themes: [bundledThemes["kanagawa-wave"]], + engine: createOnigurumaEngine(import("shiki/wasm")), }); } catch {} }