Something went wrong. Try again.
🌿 Collaborative wiki on ATProto forked from juprodh.me/lichen.wiki
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import type { MarkdownIt } from "markdown-it";import { createHighlighter, type Highlighter } from "shiki";
const BUNDLED_LANGS = [ "javascript", "typescript", "python", "rust", "go", "bash", "shell", "json", "yaml", "toml", "html", "css", "sql", "markdown", "c", "cpp", "java", "ruby", "php", "swift", "kotlin", "dockerfile", "graphql", "diff", "lua", "elixir", "haskell", "jsx", "tsx",];
let highlighter: Highlighter | null = null;
export async function initHighlighter(): Promise<void> { highlighter = await createHighlighter({ themes: ["github-light", "github-dark"], langs: BUNDLED_LANGS, });}
function highlightCode(code: string, lang: string): string | null { if (!highlighter) return null; try { return highlighter.codeToHtml(code, { lang, themes: { light: "github-light", dark: "github-dark" }, // Emit both palettes as vars so CSS light-dark() follows Lichen's theme, not the OS. defaultColor: false, }); } catch { return null; }}
export function highlightPlugin(mdi: MarkdownIt): void { const defaultFence = mdi.renderer.rules["fence"] ?? ((tokens, idx, options, _env, slf) => slf.renderToken(tokens, idx, options));
mdi.renderer.rules["fence"] = (tokens, idx, options, env, slf) => { const token = tokens[idx]; if (!token) return defaultFence(tokens, idx, options, env, slf); const lang = token.info.trim().split(/\s+/)[0] ?? "";
if (lang.startsWith("viz-")) { return defaultFence(tokens, idx, options, env, slf); }
const highlighted = lang ? highlightCode(token.content, lang) : null; if (highlighted) return highlighted;
return defaultFence(tokens, idx, options, env, slf); };}