Skip to content

Commit ccbd4d6

Browse files
committed
fix(site): use xml of highlight to format the source code
1 parent e38be69 commit ccbd4d6

File tree

3 files changed

+8
-15
lines changed

3 files changed

+8
-15
lines changed

examples/sites/src/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import hljs from 'highlight.js/lib/core'
2727
import javascript from 'highlight.js/lib/languages/javascript'
2828
import css from 'highlight.js/lib/languages/css'
2929
import html from 'highlight.js/lib/languages/xml'
30+
import tsPath from 'highlight.js/lib/languages/typescript'
3031
import docsearch from '@docsearch/js'
3132
import '@docsearch/css'
3233
import { doSearchEverySite } from './tools/docsearch'
@@ -37,6 +38,7 @@ const envTarget = import.meta.env.VITE_BUILD_TARGET || 'open'
3738
hljs.registerLanguage('javascript', javascript)
3839
hljs.registerLanguage('css', css)
3940
hljs.registerLanguage('html', html)
41+
hljs.registerLanguage('ts', tsPath)
4042

4143
if (!location.href.includes('tiny-vue-plus')) {
4244
docsearch({

examples/sites/src/views/components-doc/components/async-highlight.vue

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import { defineComponent, ref, watch } from 'vue'
1010
import hljs from 'highlight.js/lib/core'
1111
import 'highlight.js/styles/github.css'
12-
import tsPath from 'highlight.js/lib/languages/typescript'
1312
1413
export default defineComponent({
1514
name: 'AsyncHighlight',
@@ -18,29 +17,21 @@ export default defineComponent({
1817
type: String,
1918
default: ''
2019
},
21-
types: {
20+
filename: {
2221
type: String
2322
}
2423
},
2524
setup(props) {
2625
const highlightFinish = ref(false)
2726
const highlightCode = ref('')
2827
29-
const getFormatCodes = (types) => {
30-
hljs.registerLanguage('ts', tsPath)
31-
const textHtml = hljs.highlight(props.code, { language: types }).value
32-
33-
return textHtml
34-
}
3528
watch(
3629
props,
3730
() => {
3831
setTimeout(() => {
3932
// highlight和其他同步任务叠加容易形成长任务,改成异步消除长任务。
40-
if (props.types && props.types === 'html') {
41-
highlightCode.value = props.code
42-
} else if (props.types && props.types === 'ts') {
43-
highlightCode.value = getFormatCodes(props.types)
33+
if (props.filename && props.filename.endsWith('.vue')) {
34+
highlightCode.value = hljs.highlight(props.code, { language: 'html' }).value
4435
} else {
4536
highlightCode.value = hljs.highlightAuto(props.code).value
4637
}

examples/sites/src/views/components-doc/components/demo.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
/>
3030

3131
<i
32-
v-if="!isPlus"
32+
v-if="!isPlus"
3333
v-auto-tip="{ content: i18nByKey('playground'), effect: 'light', always: true }"
3434
class="i-ti-playground ml8 ti-w16 ti-h16 ti-cur-hand"
3535
@click="openPlayground(demo)"
@@ -57,12 +57,12 @@
5757
<template v-if="files?.length">
5858
<tiny-tabs v-model="state.tabValue" class="code-tabs">
5959
<tiny-tab-item v-for="(file, idx) in files" :key="file.fileName" :name="'tab' + idx" :title="file.fileName">
60-
<async-highlight :code="file.code"></async-highlight>
60+
<async-highlight :code="file.code" :filename="file.fileName"></async-highlight>
6161
</tiny-tab-item>
6262
</tiny-tabs>
6363
</template>
6464
<div v-else-if="files[0]">
65-
<async-highlight :code="files[0].code"></async-highlight>
65+
<async-highlight :code="files[0].code" :filename="files[0].fileName"></async-highlight>
6666
</div>
6767
</div>
6868
</div>

0 commit comments

Comments
 (0)