tiptap/demos/preview/Shiki.vue

74 lines
1.1 KiB
Vue
Raw Normal View History

2021-08-25 17:52:20 +08:00
<template>
<div v-if="html" v-html="html" />
<pre v-else><code>{{ code }}</code></pre>
</template>
<script>
2021-09-15 05:02:58 +08:00
// this import is a bugfix
2021-12-03 04:06:13 +08:00
// otherwise the `onig.wasm` file is missing in the dist folder
import 'shiki/dist/onig.wasm?url'
2021-08-25 17:52:20 +08:00
import Worker from './shiki.worker.js?worker'
2021-08-25 17:52:20 +08:00
export default {
props: {
code: {
default: '',
type: String,
},
language: {
default: 'js',
type: String,
},
},
data() {
return {
2021-09-15 04:28:49 +08:00
worker: new Worker(),
2021-08-25 17:52:20 +08:00
html: null,
highlighter: window?.highlighter,
}
},
watch: {
code: {
immediate: true,
handler() {
this.render()
},
},
highlighter: {
immediate: true,
handler() {
this.render()
},
},
},
methods: {
render() {
2021-09-15 04:28:49 +08:00
this.worker.postMessage({
code: this.code,
language: this.language,
2021-08-25 17:52:20 +08:00
})
},
},
created() {
2021-09-15 04:28:49 +08:00
this.worker.addEventListener('message', event => {
const { html } = event.data
this.html = html
})
2021-08-25 17:52:20 +08:00
},
}
</script>
<style>
.shiki {
background-color: transparent !important;
}
</style>