mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 12:14:41 +01:00
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Farnabaz <farnabaz@gmail.com>
35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
import { createHighlighterCore } from 'shiki/core'
|
|
import type { BuiltinLanguage, HighlighterCore } from 'shiki'
|
|
import MaterialThemeLighter from 'shiki/themes/material-theme-lighter.mjs'
|
|
import MaterialThemePalenight from 'shiki/themes/material-theme-palenight.mjs'
|
|
import VueLang from 'shiki/langs/vue.mjs'
|
|
import MarkdownLang from 'shiki/langs/markdown.mjs'
|
|
import { createOnigurumaEngine } from 'shiki/engine/oniguruma'
|
|
|
|
export const highlighter = shallowRef<HighlighterCore>()
|
|
|
|
// A custom composable for syntax highlighting with Shiki since `@nuxt/mdc` relies on
|
|
// a server endpoint to highlight code.
|
|
export function useShiki() {
|
|
async function codeToHtml(code: string, lang: BuiltinLanguage | 'text' = 'text') {
|
|
if (!highlighter.value) {
|
|
highlighter.value = await createHighlighterCore({
|
|
themes: [MaterialThemeLighter, MaterialThemePalenight],
|
|
langs: [VueLang, MarkdownLang],
|
|
engine: createOnigurumaEngine(import('shiki/wasm'))
|
|
})
|
|
}
|
|
|
|
return highlighter.value.codeToHtml(code, {
|
|
lang,
|
|
themes: {
|
|
dark: 'material-theme-palenight',
|
|
default: 'material-theme-lighter',
|
|
light: 'material-theme-lighter'
|
|
}
|
|
})
|
|
}
|
|
|
|
return { codeToHtml }
|
|
}
|