feat: 完善内容管理通用富文本编辑器
This commit is contained in:
24
app/utils/rich-text.ts
Normal file
24
app/utils/rich-text.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
const RICH_TEXT_TAG = /<(?:p|h[1-6]|ul|ol|li|blockquote|strong|em|u|s|a|br|hr|span)\b/i
|
||||
|
||||
export function escapeRichText(value: string) {
|
||||
return value
|
||||
.replaceAll('&', '&')
|
||||
.replaceAll('<', '<')
|
||||
.replaceAll('>', '>')
|
||||
.replaceAll('"', '"')
|
||||
.replaceAll("'", ''')
|
||||
}
|
||||
|
||||
export function toEditorHtml(value: string) {
|
||||
if (!value.trim()) return '<p></p>'
|
||||
if (RICH_TEXT_TAG.test(value)) return value
|
||||
|
||||
return value
|
||||
.split(/\n{2,}/)
|
||||
.map(paragraph => `<p>${escapeRichText(paragraph).replaceAll('\n', '<br>')}</p>`)
|
||||
.join('')
|
||||
}
|
||||
|
||||
export function toDisplayHtml(value: string) {
|
||||
return toEditorHtml(value || '')
|
||||
}
|
||||
Reference in New Issue
Block a user