/
highlight.ts
35 lines (30 loc) · 913 Bytes
/
highlight.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { getHighlighter } from 'shiki'
const htmlEscapes: Record<string, string> = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
'\'': ''',
}
function escapeHtml(html: string) {
return html.replace(/[&<>"']/g, chr => htmlEscapes[chr])
}
export default async () => {
const highlighter = await getHighlighter({
themes: [
'vitesse-dark',
'vitesse-light',
],
})
return (code: string, lang: string) => {
if (!lang || lang === 'text')
return `<pre v-pre><code>${escapeHtml(code)}</code></pre>`
const dark = highlighter
.codeToHtml(code, { lang, theme: 'vitesse-dark' })
.replace('<pre class="shiki"', '<pre v-pre class="shiki shiki-dark"')
const light = highlighter
.codeToHtml(code, { lang, theme: 'vitesse-light' })
.replace('<pre class="shiki"', '<pre v-pre class="shiki shiki-light"')
return dark + light
}
}