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
This commit is contained in:
parent
6372dfc41f
commit
3e96007f1f
3 changed files with 24 additions and 14 deletions
|
|
@ -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<BundledLanguage, BundledTheme>,
|
||||
highlighter: HighlighterCore,
|
||||
code: string,
|
||||
lang: BundledLanguage,
|
||||
theme: BundledTheme,
|
||||
|
|
|
|||
|
|
@ -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<BundledLanguage, BundledTheme> | 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 {}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue