parent
2c509c329f
commit
21fb4f817c
@ -0,0 +1,23 @@
|
|||||||
|
import type { App, Directive } from 'vue'
|
||||||
|
import hljs from 'highlight.js'
|
||||||
|
import { includeCode } from '@/utils/format'
|
||||||
|
|
||||||
|
hljs.configure({ ignoreUnescapedHTML: true })
|
||||||
|
|
||||||
|
function highlightCode(el: HTMLElement) {
|
||||||
|
if (includeCode(el.textContent))
|
||||||
|
hljs.highlightBlock(el)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function setupHighlightDirective(app: App) {
|
||||||
|
const highLightDirective: Directive<HTMLElement> = {
|
||||||
|
mounted(el: HTMLElement) {
|
||||||
|
highlightCode(el)
|
||||||
|
},
|
||||||
|
updated(el: HTMLElement) {
|
||||||
|
highlightCode(el)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
app.directive('highlight', highLightDirective)
|
||||||
|
}
|
@ -1 +1,6 @@
|
|||||||
export function setupDirectives() {}
|
import type { App } from 'vue'
|
||||||
|
import setupHighlightDirective from './highlight'
|
||||||
|
|
||||||
|
export function setupDirectives(app: App) {
|
||||||
|
setupHighlightDirective(app)
|
||||||
|
}
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
// 转义 HTML 字符
|
||||||
|
export function encodeHTML(source: string) {
|
||||||
|
return source
|
||||||
|
.replace(/&/g, '&')
|
||||||
|
.replace(/</g, '<')
|
||||||
|
.replace(/>/g, '>')
|
||||||
|
.replace(/"/g, '"')
|
||||||
|
.replace(/'/g, ''')
|
||||||
|
}
|
||||||
|
|
||||||
|
// 判断是否为代码块
|
||||||
|
export function includeCode(text: string | null | undefined) {
|
||||||
|
const regexp = /^(?:\s{4}|\t).+/gm
|
||||||
|
return !!(text?.includes(' = ') || text?.match(regexp))
|
||||||
|
}
|
@ -1,6 +0,0 @@
|
|||||||
function includeCode(text: string | null | undefined) {
|
|
||||||
const regexp = /^(?:\s{4}|\t).+/gm
|
|
||||||
return !!(text?.includes(' = ') || text?.match(regexp))
|
|
||||||
}
|
|
||||||
|
|
||||||
export default includeCode
|
|
Loading…
Reference in New Issue