-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-common.ts
More file actions
31 lines (28 loc) · 871 Bytes
/
github-common.ts
File metadata and controls
31 lines (28 loc) · 871 Bytes
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
import hljs from 'highlight.js'
import type { Options } from 'overtype'
import OverType from 'overtype'
import { oncePerRefresh } from '@/lib/once-per-refresh'
export const commonGitHubOptions: Options = {
autoResize: true,
lineHeight: 'var(--text-body-lineHeight-medium, 1.4285)',
padding: 'var(--base-size-16)',
}
export function prepareGitHubHighlighter() {
oncePerRefresh('github-highlighter', () => {
OverType.setCodeHighlighter(githubHighlighter)
})
}
function githubHighlighter(code: string, language?: string) {
try {
if (language && hljs.getLanguage(language)) {
const result = hljs.highlight(code, { language })
return result.value
} else {
const result = hljs.highlightAuto(code)
return result.value
}
} catch (error) {
console.warn('highlight.js highlighting failed:', error)
return code
}
}