Jeditor的显示源代码更新为格式化显示
This commit is contained in:
parent
e69bfb96f2
commit
79ce15e445
@ -516,9 +516,40 @@ function toggleSourceMode() {
|
||||
}
|
||||
}
|
||||
|
||||
// 显示原始HTML数据,不进行格式化
|
||||
// 格式化HTML,提高可读性
|
||||
function formatHTML(html: string): string {
|
||||
return html || ''
|
||||
if (!html) return ''
|
||||
|
||||
// 简单的HTML格式化函数
|
||||
let formatted = html
|
||||
.replace(/></g, '>\n<') // 在标签之间添加换行
|
||||
.replace(/^\s+|\s+$/g, '') // 去除首尾空白
|
||||
|
||||
// 添加缩进
|
||||
const lines = formatted.split('\n')
|
||||
let indentLevel = 0
|
||||
const indentSize = 2
|
||||
|
||||
const formattedLines = lines.map(line => {
|
||||
const trimmed = line.trim()
|
||||
if (!trimmed) return ''
|
||||
|
||||
// 减少缩进的情况
|
||||
if (trimmed.match(/^<\/[^>]+>$/)) {
|
||||
indentLevel = Math.max(0, indentLevel - 1)
|
||||
}
|
||||
|
||||
const indented = ' '.repeat(indentLevel * indentSize) + trimmed
|
||||
|
||||
// 增加缩进的情况(排除自闭合标签)
|
||||
if (trimmed.match(/^<[^/][^>]*[^/]>$/) && !trimmed.match(/^<[^>]*\/>$/)) {
|
||||
indentLevel++
|
||||
}
|
||||
|
||||
return indented
|
||||
})
|
||||
|
||||
return formattedLines.join('\n')
|
||||
}
|
||||
|
||||
function mountControl() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user