修复代码组件渲染注释、xml异常的bug

master
yutent 2023-05-04 16:13:48 +08:00
parent 4f62a0adf3
commit c86a1d9dc6
1 changed files with 12 additions and 1 deletions

View File

@ -170,6 +170,7 @@ class Code extends Component {
setCode(txt, a) {
let lang = this.lang
let skip = false
switch (lang) {
case 'js':
@ -180,12 +181,14 @@ class Code extends Component {
break
case 'html':
case 'xml':
txt = colorHtml(txt)
break
case 'css':
case 'scss':
case 'less':
skip = true
txt = colorCss(txt)
break
@ -196,7 +199,15 @@ class Code extends Component {
}
// 除代码高亮用的特殊标签 <c></c> 外, 其他的全部转义
// 避免渲染时混乱
txt = txt.replace(/<(\/?)(?!c)(\w+)([^>]*?)>/g, '&lt;$1$2$3>')
if (skip === false) {
txt = txt
.replace(/<(\/?)(?!c)(\w+)([^>]*?)>/g, '&lt;$1$2$3>')
.replace(/<!(doctype)([^>]*?)>/gi, '&lt;$1$2>')
.replace(/<\?([^\?>]*?)\?>/g, '&lt;?$1?>')
.replace(/<\!\-\-([^>]*?)\-\->/g, '&lt;!--$1--&gt;')
}
this.#code = txt.split('\n')
}