Skip to content

Commit e79aa1a

Browse files
authored
feat: use shikiji for more accurate highlight (#190)
1 parent 2f3709f commit e79aa1a

File tree

5 files changed

+48
-77
lines changed

5 files changed

+48
-77
lines changed

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@
8181
"hash-sum": "^2.0.0",
8282
"lint-staged": "^15.1.0",
8383
"monaco-editor-core": "^0.44.0",
84-
"monaco-editor-textmate": "^4.0.0",
85-
"monaco-textmate": "^3.0.1",
86-
"monaco-volar": "^0.4.0",
87-
"onigasm": "^2.2.5",
8884
"prettier": "^3.1.0",
85+
"shikiji": "^0.9.6",
86+
"shikiji-monaco": "^0.9.6",
8987
"simple-git-hooks": "^2.9.0",
9088
"sucrase": "^3.34.0",
9189
"typescript": "^5.3.2",

pnpm-lock.yaml

Lines changed: 22 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/monaco/Monaco.vue

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
import * as monaco from 'monaco-editor-core'
1414
import { initMonaco } from './env'
1515
import { getOrCreateModel } from './utils'
16-
import { loadGrammars, loadTheme } from 'monaco-volar'
1716
import { Store } from '../store'
1817
import type { PreviewMode } from '../editor/types'
1918
@@ -44,7 +43,7 @@ const lang = computed(() => (props.mode === 'css' ? 'css' : 'javascript'))
4443
4544
const replTheme = inject<Ref<'dark' | 'light'>>('theme')!
4645
onMounted(async () => {
47-
const theme = await loadTheme(monaco.editor)
46+
const theme = await import('./highlight').then(r => r.registerHighlighter())
4847
ready.value = true
4948
await nextTick()
5049
@@ -68,7 +67,6 @@ onMounted(async () => {
6867
inlineSuggest: {
6968
enabled: false,
7069
},
71-
'semanticHighlighting.enabled': true,
7270
fixedOverflowWidgets: true,
7371
})
7472
editor.value = editorInstance
@@ -137,8 +135,6 @@ onMounted(async () => {
137135
)
138136
}
139137
140-
await loadGrammars(monaco, editorInstance)
141-
142138
editorInstance.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => {
143139
// ignore save event
144140
})

src/monaco/env.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { jsDelivrUriBase } from '@volar/cdn'
22
import * as volar from '@volar/monaco'
33
import { editor, languages, Uri } from 'monaco-editor-core'
44
import editorWorker from 'monaco-editor-core/esm/vs/editor/editor.worker?worker'
5-
import * as onigasm from 'onigasm'
6-
import onigasmWasm from 'onigasm/lib/onigasm.wasm?url'
75
import { watchEffect } from 'vue'
86
import { Store } from '../store'
97
import { getOrCreateModel } from './utils'
@@ -14,7 +12,6 @@ let initted = false
1412
export function initMonaco(store: Store) {
1513
if (initted) return
1614
loadMonacoEnv(store)
17-
loadWasm()
1815

1916
watchEffect(() => {
2017
// create a model for each file in the store
@@ -62,10 +59,6 @@ export function initMonaco(store: Store) {
6259
initted = true
6360
}
6461

65-
export function loadWasm() {
66-
return onigasm.loadWASM(onigasmWasm)
67-
}
68-
6962
export class WorkerHost {
7063
onFetchCdnFile(uri: string, text: string) {
7164
getOrCreateModel(Uri.parse(uri), undefined, text)

src/monaco/highlight.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as monaco from 'monaco-editor-core'
2+
import { getHighlighterCore } from 'shikiji/core'
3+
import { getWasmInlined } from 'shikiji/wasm'
4+
import { shikijiToMonaco } from 'shikiji-monaco'
5+
6+
import langVue from 'shikiji/langs/vue.mjs'
7+
import themeDark from 'shikiji/themes/dark-plus.mjs'
8+
import themeLight from 'shikiji/themes/light-plus.mjs'
9+
10+
11+
export async function registerHighlighter() {
12+
const highlighter = await getHighlighterCore({
13+
themes: [themeDark, themeLight],
14+
langs: [langVue],
15+
loadWasm: getWasmInlined
16+
})
17+
monaco.languages.register({ id: 'vue' })
18+
shikijiToMonaco(highlighter, monaco)
19+
return {
20+
light: themeLight.name!,
21+
dark: themeDark.name!
22+
}
23+
}

0 commit comments

Comments
 (0)