import { createHighlighter, HighlighterGeneric, BundledLanguage, BundledTheme } from 'shiki' import type { Element } from 'hast' let highlighter: HighlighterGeneric | null = null export async function getHighlighterInstance() { if (!highlighter) { highlighter = await createHighlighter({ themes: ['catppuccin-latte', 'catppuccin-mocha'], langs: [ 'javascript', 'typescript', 'python', 'java', 'rust', 'go', 'c', 'cpp', 'csharp', 'php', 'ruby', 'cobol', 'plaintext', 'txt', 'bash', 'nix' ] }) } return highlighter } export async function highlightCode(code: string, lang: string) { const highlighter = await getHighlighterInstance() try { return highlighter.codeToHtml(code, { lang: lang, themes: { light: 'catppuccin-latte', dark: 'catppuccin-mocha' }, transformers: [ { line(node: Element, line: number) { node.properties['code-line'] = line } } ] }) } catch (error) { console.warn(`Language "${lang}" not supported by Shiki, falling back to plaintext`) return highlighter.codeToHtml(code, { lang: 'txt', themes: { light: 'catppuccin-latte', dark: 'catppuccin-mocha' } }) } }