将 highlight 代码高亮的全局引入,改成局部引入

(cherry picked from commit 30685bee75)
This commit is contained in:
YunaiV 2023-04-15 21:36:16 +08:00 committed by shizhong
parent f9c2850d6b
commit d73118f386
2 changed files with 31 additions and 12 deletions

View File

@ -40,8 +40,7 @@ import App from './App.vue'
import './permission'
import hljs from 'highlight.js' //导入代码高亮文件
import 'highlight.js/styles/github.css' //导入代码高亮样式 新版
import '@/plugins/tongji' // 百度统计
import Logger from '@/utils/Logger'
@ -49,14 +48,6 @@ import Logger from '@/utils/Logger'
const setupAll = async () => {
const app = createApp(App)
//自定义一个代码高亮指令
app.directive('highlight', function (el) {
const blocks = el.querySelectorAll('code')
blocks.forEach((block: any) => {
hljs.highlightElement(block)
})
})
await setupI18n(app)
setupStore(app)

View File

@ -45,8 +45,8 @@
<el-button text type="primary" class="float-right" @click="copy(item.code)">
{{ t('common.copy') }}
</el-button>
<div v-highlight>
<code>{{ item.code }}</code>
<div>
<pre><code class="hljs" v-html="highlightedCode(item)"></code></pre>
</div>
</el-tab-pane>
</el-tabs>
@ -179,6 +179,34 @@ const copy = async (text: string) => {
message.success(t('common.copySuccess'))
}
}
/**
* 代码高亮
*/
import hljs from 'highlight.js' //
import 'highlight.js/styles/github.css' //
import java from 'highlight.js/lib/languages/java'
import xml from 'highlight.js/lib/languages/java'
import javascript from 'highlight.js/lib/languages/javascript'
import sql from 'highlight.js/lib/languages/sql'
import typescript from 'highlight.js/lib/languages/typescript'
const highlightedCode = (item) => {
const language = item.filePath.substring(item.filePath.lastIndexOf('.') + 1)
const result = hljs.highlight(language, item.code || '', true)
return result.value || '&nbsp;'
}
/** 初始化 **/
onMounted(async () => {
//
hljs.registerLanguage('java', java)
hljs.registerLanguage('xml', xml)
hljs.registerLanguage('html', xml)
hljs.registerLanguage('vue', xml)
hljs.registerLanguage('javascript', javascript)
hljs.registerLanguage('sql', sql)
hljs.registerLanguage('typescript', typescript)
})
</script>
<style lang="scss">
.app-infra-codegen-preview-container {