2024-02-15 07:43:09 +00:00
|
|
|
import type { Highlighter, BuiltinLanguage as Lang } from 'shiki'
|
2022-11-24 03:42:03 +00:00
|
|
|
|
2023-12-20 18:54:40 +00:00
|
|
|
const highlighter = ref<Highlighter>()
|
2022-11-24 03:42:03 +00:00
|
|
|
|
|
|
|
const registeredLang = ref(new Map<string, boolean>())
|
2024-02-15 07:43:09 +00:00
|
|
|
let shikiImport: Promise<void> | undefined
|
2022-11-24 03:42:03 +00:00
|
|
|
|
2024-03-05 14:48:58 +00:00
|
|
|
export function useHighlighter(lang: Lang): {
|
|
|
|
promise?: Promise<void>
|
|
|
|
highlighter?: Highlighter
|
|
|
|
} {
|
2024-02-15 07:43:09 +00:00
|
|
|
if (!shikiImport) {
|
|
|
|
shikiImport = import('shiki')
|
2023-12-20 18:54:40 +00:00
|
|
|
.then(async ({ getHighlighter }) => {
|
|
|
|
highlighter.value = await getHighlighter({
|
2022-11-24 03:42:03 +00:00
|
|
|
themes: [
|
|
|
|
'vitesse-dark',
|
|
|
|
'vitesse-light',
|
|
|
|
],
|
2022-11-30 07:15:26 +00:00
|
|
|
langs: [
|
2022-12-02 17:40:02 +00:00
|
|
|
'js',
|
|
|
|
'css',
|
2022-11-30 07:15:26 +00:00
|
|
|
'html',
|
|
|
|
],
|
2022-11-24 03:42:03 +00:00
|
|
|
})
|
|
|
|
})
|
2024-01-01 16:46:37 +00:00
|
|
|
|
2024-02-15 07:43:09 +00:00
|
|
|
return { promise: shikiImport }
|
2022-11-24 03:42:03 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 18:54:40 +00:00
|
|
|
if (!highlighter.value)
|
2024-02-15 07:43:09 +00:00
|
|
|
return { promise: shikiImport }
|
2022-11-24 03:42:03 +00:00
|
|
|
|
|
|
|
if (!registeredLang.value.get(lang)) {
|
2024-01-01 16:46:37 +00:00
|
|
|
const promise = highlighter.value.loadLanguage(lang)
|
2022-11-24 03:42:03 +00:00
|
|
|
.then(() => {
|
|
|
|
registeredLang.value.set(lang, true)
|
|
|
|
})
|
2023-01-13 15:08:08 +00:00
|
|
|
.catch(() => {
|
|
|
|
const fallbackLang = 'md'
|
2023-12-20 18:54:40 +00:00
|
|
|
highlighter.value?.loadLanguage(fallbackLang).then(() => {
|
2023-01-13 15:08:08 +00:00
|
|
|
registeredLang.value.set(fallbackLang, true)
|
|
|
|
})
|
2022-11-24 08:57:24 +00:00
|
|
|
})
|
2024-01-01 16:46:37 +00:00
|
|
|
return { promise }
|
2022-11-24 03:42:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 16:46:37 +00:00
|
|
|
return { highlighter: highlighter.value }
|
2022-12-13 13:02:43 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 07:43:09 +00:00
|
|
|
function useShikiTheme() {
|
2022-12-29 12:26:08 +00:00
|
|
|
return useColorMode().value === 'dark' ? 'vitesse-dark' : 'vitesse-light'
|
2022-12-13 13:02:43 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 10:48:22 +00:00
|
|
|
const HTML_ENTITIES = {
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
'&': '&',
|
|
|
|
'\'': ''',
|
|
|
|
'"': '"',
|
|
|
|
} as Record<string, string>
|
|
|
|
|
|
|
|
function escapeHtml(text: string) {
|
|
|
|
return text.replace(/[<>&'"]/g, ch => HTML_ENTITIES[ch])
|
|
|
|
}
|
|
|
|
|
2022-12-13 13:02:43 +00:00
|
|
|
export function highlightCode(code: string, lang: Lang) {
|
2024-01-01 16:46:37 +00:00
|
|
|
const { highlighter } = useHighlighter(lang)
|
2023-12-20 18:54:40 +00:00
|
|
|
if (!highlighter)
|
2023-01-15 10:48:22 +00:00
|
|
|
return escapeHtml(code)
|
2022-12-13 13:02:43 +00:00
|
|
|
|
2023-12-20 18:54:40 +00:00
|
|
|
return highlighter.codeToHtml(code, {
|
2022-11-24 03:42:03 +00:00
|
|
|
lang,
|
2024-02-15 07:43:09 +00:00
|
|
|
theme: useShikiTheme(),
|
2022-11-24 03:42:03 +00:00
|
|
|
})
|
|
|
|
}
|