Jeditor的显示源代码更新为格式化显示

This commit is contained in:
jingrow 2025-10-11 14:39:40 +08:00
parent e69bfb96f2
commit 79ce15e445

View File

@ -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() {