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
|
|
|
|
2023-07-01 03:03:49 +08:00
|
|
|
import Worker from './shiki.worker.js?worker'
|
2022-06-08 20:10:25 +08:00
|
|
|
|
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>
|