Skip to content

Commit

Permalink
feat: use shikiji for more accurate highlight (vuejs#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Dec 21, 2023
1 parent 2f3709f commit e79aa1a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 77 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,9 @@
"hash-sum": "^2.0.0",
"lint-staged": "^15.1.0",
"monaco-editor-core": "^0.44.0",
"monaco-editor-textmate": "^4.0.0",
"monaco-textmate": "^3.0.1",
"monaco-volar": "^0.4.0",
"onigasm": "^2.2.5",
"prettier": "^3.1.0",
"shikiji": "^0.9.6",
"shikiji-monaco": "^0.9.6",
"simple-git-hooks": "^2.9.0",
"sucrase": "^3.34.0",
"typescript": "^5.3.2",
Expand Down
83 changes: 22 additions & 61 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions src/monaco/Monaco.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
import * as monaco from 'monaco-editor-core'
import { initMonaco } from './env'
import { getOrCreateModel } from './utils'
import { loadGrammars, loadTheme } from 'monaco-volar'
import { Store } from '../store'
import type { PreviewMode } from '../editor/types'
Expand Down Expand Up @@ -44,7 +43,7 @@ const lang = computed(() => (props.mode === 'css' ? 'css' : 'javascript'))
const replTheme = inject<Ref<'dark' | 'light'>>('theme')!
onMounted(async () => {
const theme = await loadTheme(monaco.editor)
const theme = await import('./highlight').then(r => r.registerHighlighter())
ready.value = true
await nextTick()
Expand All @@ -68,7 +67,6 @@ onMounted(async () => {
inlineSuggest: {
enabled: false,
},
'semanticHighlighting.enabled': true,
fixedOverflowWidgets: true,
})
editor.value = editorInstance
Expand Down Expand Up @@ -137,8 +135,6 @@ onMounted(async () => {
)
}
await loadGrammars(monaco, editorInstance)
editorInstance.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => {
// ignore save event
})
Expand Down
7 changes: 0 additions & 7 deletions src/monaco/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { jsDelivrUriBase } from '@volar/cdn'
import * as volar from '@volar/monaco'
import { editor, languages, Uri } from 'monaco-editor-core'
import editorWorker from 'monaco-editor-core/esm/vs/editor/editor.worker?worker'
import * as onigasm from 'onigasm'
import onigasmWasm from 'onigasm/lib/onigasm.wasm?url'
import { watchEffect } from 'vue'
import { Store } from '../store'
import { getOrCreateModel } from './utils'
Expand All @@ -14,7 +12,6 @@ let initted = false
export function initMonaco(store: Store) {
if (initted) return
loadMonacoEnv(store)
loadWasm()

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

export function loadWasm() {
return onigasm.loadWASM(onigasmWasm)
}

export class WorkerHost {
onFetchCdnFile(uri: string, text: string) {
getOrCreateModel(Uri.parse(uri), undefined, text)
Expand Down
23 changes: 23 additions & 0 deletions src/monaco/highlight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as monaco from 'monaco-editor-core'
import { getHighlighterCore } from 'shikiji/core'
import { getWasmInlined } from 'shikiji/wasm'
import { shikijiToMonaco } from 'shikiji-monaco'

import langVue from 'shikiji/langs/vue.mjs'
import themeDark from 'shikiji/themes/dark-plus.mjs'
import themeLight from 'shikiji/themes/light-plus.mjs'


export async function registerHighlighter() {
const highlighter = await getHighlighterCore({
themes: [themeDark, themeLight],
langs: [langVue],
loadWasm: getWasmInlined
})
monaco.languages.register({ id: 'vue' })
shikijiToMonaco(highlighter, monaco)
return {
light: themeLight.name!,
dark: themeDark.name!
}
}

0 comments on commit e79aa1a

Please sign in to comment.