20 lines
678 B
JavaScript
20 lines
678 B
JavaScript
// Escape HTML special characters
|
|
// 转义 HTML 特殊字符
|
|
export function escapeHTML(str) {
|
|
if (typeof str !== 'string') return '';
|
|
return str.replace(/[&<>"']/g, function(c) {
|
|
return {
|
|
'&': '&', // & to & // & 转为 &
|
|
'<': '<', // < to < // < 转为 <
|
|
'>': '>', // > to > // > 转为 >
|
|
'"': '"', // " to " // " 转为 "
|
|
'\'': ''' // ' to ' // ' 转为 '
|
|
} [c]
|
|
})
|
|
}
|
|
// Convert text to HTML, preserving line breaks
|
|
// 将文本转换为 HTML,保留换行符
|
|
export function textToHTML(text) {
|
|
if (typeof text !== 'string') return '';
|
|
return escapeHTML(text).replace(/\n/g, '<br>')
|
|
} |