feat: 完善内容管理通用富文本编辑器

This commit is contained in:
2026-08-27 17:01:48 +08:00
parent ed81f24892
commit d93486cd9a
21 changed files with 1245 additions and 30 deletions

24
app/utils/rich-text.ts Normal file
View 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('&', '&amp;')
.replaceAll('<', '&lt;')
.replaceAll('>', '&gt;')
.replaceAll('"', '&quot;')
.replaceAll("'", '&#039;')
}
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 || '')
}