2018-07-16 22:22:55 +08:00
|
|
|
<template>
|
|
|
|
|
<pre class="d2-highlight" v-html="highlightHTML"></pre>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
// https://highlightjs.org/usage/
|
|
|
|
|
// http://highlightjs.readthedocs.io/en/latest/api.html#configure-options
|
2018-07-18 00:11:13 +08:00
|
|
|
import htmlFormat from './libs/htmlFormat'
|
2018-07-16 22:22:55 +08:00
|
|
|
import highlight from 'highlight.js'
|
|
|
|
|
export default {
|
|
|
|
|
name: 'd2-highlight',
|
|
|
|
|
props: {
|
|
|
|
|
code: {
|
|
|
|
|
type: String,
|
|
|
|
|
required: false,
|
|
|
|
|
default: `console.log('you lost code prop')`
|
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-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-07-18 00:11:13 +08:00
|
|
|
this.highlightHTML = highlight.highlightAuto(code).value
|
2018-07-16 22:22:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
.d2-highlight {
|
|
|
|
|
margin: 0px;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|