Skip to content

WIP: update monaco #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: javascript engine of shiki
  • Loading branch information
sxzz authored and oumoussa98 committed Apr 18, 2025
commit e3eac1720766a512d294955d9badc642cebd3d1b
9 changes: 3 additions & 6 deletions src/monaco/Monaco.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
onBeforeUnmount,
ref,
shallowRef,
nextTick,
inject,
watch,
computed,
Expand All @@ -19,6 +18,7 @@ import parserBabel from 'prettier/parser-babel'
import parserHtml from 'prettier/parser-html'
import parserPostcss from 'prettier/parser-postcss'
import prettier from 'prettier/standalone'
import { registerHighlighter } from './highlight'

const props = withDefaults(
defineProps<{
Expand All @@ -37,7 +37,6 @@ const emit = defineEmits<{
}>()

const containerRef = ref<HTMLDivElement>()
const ready = ref(false)
const editor = shallowRef<monaco.editor.IStandaloneCodeEditor>()
const store = inject<Store>('store')!

Expand All @@ -47,10 +46,8 @@ const lang = computed(() => (props.mode === 'css' ? 'css' : 'javascript'))
const extension = computed(() => props.filename.split('.').at(-1))

const replTheme = inject<Ref<'dark' | 'light'>>('theme')!
onMounted(async () => {
const theme = await import('./highlight').then(r => r.registerHighlighter())
ready.value = true
await nextTick()
onMounted(() => {
const theme = registerHighlighter()

if (!containerRef.value) {
throw new Error('Cannot find containerRef')
Expand Down
26 changes: 17 additions & 9 deletions src/monaco/highlight.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import * as monaco from 'monaco-editor-core'
import { createHighlighterCore } from 'shiki/core'
import { createHighlighterCoreSync } from 'shiki/core'
import { createJavaScriptRegexEngine } from 'shiki/engine-javascript.mjs'
import { shikiToMonaco } from '@shikijs/monaco'

import langVue from 'shiki/langs/vue.mjs'
import langTsx from 'shiki/langs/tsx.mjs'
import langJsx from 'shiki/langs/jsx.mjs'
import themeDark from 'shiki/themes/dark-plus.mjs'
import themeLight from 'shiki/themes/light-plus.mjs'

export async function registerHighlighter() {
const highlighter = await createHighlighterCore({
themes: [themeDark, themeLight],
langs: [langVue],
loadWasm: import('shiki/wasm'),
})
monaco.languages.register({ id: 'vue' })
shikiToMonaco(highlighter, monaco)
let registered = false
export function registerHighlighter() {
if (!registered) {
const highlighter = createHighlighterCoreSync({
themes: [themeDark, themeLight],
langs: [langVue, langTsx, langJsx],
engine: createJavaScriptRegexEngine(),
})
monaco.languages.register({ id: 'vue' })
shikiToMonaco(highlighter, monaco)
registered = true
}

return {
light: themeLight.name!,
dark: themeDark.name!,
Expand Down