2018-07-16 22:22:55 +08:00
|
|
|
<template>
|
2018-08-29 16:23:07 +08:00
|
|
|
<pre class="d2-highlight hljs" v-html="highlightHTML"></pre>
|
2018-07-16 22:22:55 +08:00
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
2019-03-20 10:11:47 +08:00
|
|
|
// 相关文档
|
2018-07-16 22:22:55 +08:00
|
|
|
// https://highlightjs.org/usage/
|
|
|
|
|
// http://highlightjs.readthedocs.io/en/latest/api.html#configure-options
|
|
|
|
|
import highlight from 'highlight.js'
|
2019-03-20 10:11:47 +08:00
|
|
|
import '../highlight-styles/github-gist.css'
|
|
|
|
|
import htmlFormat from './libs/htmlFormat'
|
2018-07-16 22:22:55 +08:00
|
|
|
export default {
|
|
|
|
|
name: 'd2-highlight',
|
|
|
|
|
props: {
|
|
|
|
|
code: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: false,
|
2018-08-29 16:23:07 +08:00
|
|
|
default: ''
|
2018-07-18 00:11:13 +08:00
|
|
|
},
|
2018-07-18 00:14:31 +08:00
|
|
|
formatHtml: {
|
2018-07-18 00:11:13 +08:00
|
|
|
type: Boolean,
|
|
|
|
|
required: false,
|
|
|
|
|
default: false
|
2018-08-29 16:23:07 +08:00
|
|
|
},
|
|
|
|
|
lang: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: false,
|
|
|
|
|
default: ''
|
2018-07-16 22:22:55 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
data () {
|
|
|
|
|
return {
|
|
|
|
|
highlightHTML: ''
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
mounted () {
|
|
|
|
|
this.highlight()
|
|
|
|
|
},
|
|
|
|
|
watch: {
|
|
|
|
|
code () {
|
|
|
|
|
this.highlight()
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
highlight () {
|
2018-07-18 00:14:31 +08:00
|
|
|
const code = this.formatHtml ? htmlFormat(this.code) : this.code
|
2018-08-29 16:23:07 +08:00
|
|
|
this.highlightHTML = highlight.highlightAuto(code, [
|
|
|
|
|
this.lang,
|
|
|
|
|
'html',
|
|
|
|
|
'javascript',
|
|
|
|
|
'json',
|
|
|
|
|
'css',
|
|
|
|
|
'scss',
|
|
|
|
|
'less'
|
|
|
|
|
]).value
|
2018-07-16 22:22:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
.d2-highlight {
|
|
|
|
|
margin: 0px;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|