Skip to content

Commit 375757e

Browse files
authored
fix(site): rewrite the theme of code highlight (#3397)
* fix(site): rewrite the theme of code highlight * fix(site): use xml of highlight to format the source code
1 parent 8a72dda commit 375757e

File tree

4 files changed

+116
-18
lines changed

4 files changed

+116
-18
lines changed

examples/sites/src/assets/custom-markdown.css

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ body .markdown-body {
9797
}
9898
.markdown-body code {
9999
text-shadow: none !important;
100+
font-size: 120%;
101+
font-family: Consolas, 'Courier New', monospace !important;
100102
}
101103

102104
html.dark.dark .markdown-body {
@@ -115,6 +117,63 @@ html.dark.dark .markdown-body code .token.operator {
115117
background-color: transparent;
116118
}
117119

118-
html.dark.dark .markdown-body code .hljs-string {
119-
color: #6f42c1;
120+
/* 指南中的代码块高亮 */
121+
html.dark.dark .markdown-body code {
122+
&.language-bash {
123+
color: #9ecbff;
124+
.function {
125+
color: #b392f0;
126+
}
127+
}
128+
129+
&.language-js,
130+
&.language-javascript {
131+
color: #e1e4e8;
132+
.punctuation {
133+
color: #e1e4e8;
134+
}
135+
.keyword {
136+
color: #f97583;
137+
}
138+
.string {
139+
color: #9ecbff;
140+
}
141+
.function {
142+
color: #b392f0;
143+
}
144+
.literal-property,
145+
.property {
146+
color: #b392f0;
147+
}
148+
.operator {
149+
color: #e1e4e8;
150+
}
151+
.constant {
152+
color: #e1e4e8;
153+
}
154+
}
155+
156+
&.language-html {
157+
color: #e1e4e8;
158+
.tag {
159+
color: #85e89d;
160+
}
161+
.punctuation {
162+
color: #e1e4e8;
163+
}
164+
.attr-name {
165+
color: #f97583;
166+
}
167+
.attr-value {
168+
color: #9ecbff;
169+
}
170+
.literal-property,
171+
.string-property,
172+
.property {
173+
color: #b392f0;
174+
}
175+
.string {
176+
color: #9ecbff;
177+
}
178+
}
120179
}

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: 50 additions & 13 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
}
@@ -58,13 +49,59 @@ export default defineComponent({
5849
</script>
5950

6051
<style lang="less">
52+
/* 代码预览中的代码块高亮 */
53+
.dark {
54+
.code-preview-box {
55+
.hljs-tag {
56+
color: #e1e4e8;
57+
.hljs-name {
58+
color: #85e89d;
59+
}
60+
.hljs-attr {
61+
color: #b392f0;
62+
}
63+
.hljs-string {
64+
color: #9ecbff;
65+
}
66+
}
67+
68+
.language-javascript {
69+
color: #79b8ff;
70+
71+
.hljs-keyword,
72+
.hljs-title {
73+
color: #f97583;
74+
}
75+
.hljs-string {
76+
color: #9ecbff;
77+
}
78+
.hljs-attr {
79+
color: #e1e4e8;
80+
}
81+
.hljs-variable {
82+
color: #b392f0;
83+
}
84+
}
85+
.language-css {
86+
.hljs-selector-class {
87+
color: #b392f0;
88+
}
89+
.hljs-attribute {
90+
color: #9ecbff;
91+
}
92+
.hljs-number {
93+
color: #e1e4e8;
94+
}
95+
}
96+
}
97+
}
6198
.code-preview-box {
6299
max-height: 400px;
63100
overflow-y: auto;
64101
65102
pre {
66103
line-height: 22px;
67-
font-family: monospace;
104+
font-family: Consolas, 'Courier New', monospace !important;
68105
font-size: 14px;
69106
font-weight: 400;
70107
padding: 0px 12px;

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)