一,使用
1.下载
npm install highlight.js --save
2.封装成全局自定义指令 utils/highlight.js
import hljs from "highlight.js";
import 'highlight.js/styles/stackoverflow-light.css'
//sql语言提供的关键字不全可以自定义补充
var sql = hljs.getLanguage('sql')
var key = ['use','go','if','while','type','proc']
sql.keywords['keyword'] = sql.keywords['keyword'].concat(key)
hljs.registerLanguage('sql',function(hljs){return {...sql}
})
//多行注释有问题处理多行注释
function replaceHTML(str){const regexs = /(<([^>]+)>)/gi;const tempStr = str.replace(regexs, ''); // 删除所有HTML标签return `<div class="hljs-comment">${tempStr}</div>`}
let Highlight = {}
Highlight.install = function (Vue) {Vue