atproto pastebin service: https://plonk.li
at main 48 lines 1.3 kB view raw
1import { createHighlighter, HighlighterGeneric, BundledLanguage, BundledTheme } from 'shiki' 2import type { Element } from 'hast' 3 4let highlighter: HighlighterGeneric<BundledLanguage, BundledTheme> | null = null 5 6export async function getHighlighterInstance() { 7 if (!highlighter) { 8 highlighter = await createHighlighter({ 9 themes: ['catppuccin-latte', 'catppuccin-mocha'], 10 langs: [ 11 'javascript', 'typescript', 'python', 'java', 'rust', 12 'go', 'c', 'cpp', 'csharp', 'php', 'ruby', 'cobol', 13 'plaintext', 'txt', 'bash', 'nix' 14 ] 15 }) 16 } 17 return highlighter 18} 19 20export async function highlightCode(code: string, lang: string) { 21 const highlighter = await getHighlighterInstance() 22 23 try { 24 return highlighter.codeToHtml(code, { 25 lang: lang, 26 themes: { 27 light: 'catppuccin-latte', 28 dark: 'catppuccin-mocha' 29 }, 30 transformers: [ 31 { 32 line(node: Element, line: number) { 33 node.properties['code-line'] = line 34 } 35 } 36 ] 37 }) 38 } catch (error) { 39 console.warn(`Language "${lang}" not supported by Shiki, falling back to plaintext`) 40 return highlighter.codeToHtml(code, { 41 lang: 'txt', 42 themes: { 43 light: 'catppuccin-latte', 44 dark: 'catppuccin-mocha' 45 } 46 }) 47 } 48}