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

36
server/utils/rich-text.ts Normal file
View File

@@ -0,0 +1,36 @@
import sanitizeHtml from 'sanitize-html'
const allowedFontFamilies = /^(?:Noto Serif SC|Source Han Serif SC|Songti SC|SimSun|Noto Sans SC|Source Han Sans SC|PingFang SC|Microsoft YaHei|Arial|sans-serif|serif)(?:,\s*(?:serif|sans-serif))?$/
export function sanitizeRichText(value: string) {
if (!value) return ''
return sanitizeHtml(value, {
allowedTags: ['p', 'h2', 'h3', 'ul', 'ol', 'li', 'blockquote', 'strong', 'em', 'u', 's', 'a', 'br', 'hr', 'span'],
allowedAttributes: {
a: ['href', 'target', 'rel'],
'*': ['style'],
},
allowedSchemes: ['http', 'https', 'mailto', 'tel'],
allowProtocolRelative: false,
allowedStyles: {
'*': {
color: [/^#[0-9a-f]{6}$/i],
'background-color': [/^#[0-9a-f]{6}$/i],
'font-family': [allowedFontFamilies],
'font-size': [/^(?:14|16|18|20|24|28|32)px$/],
'line-height': [/^(?:1\.5|1\.75|2|2\.25)$/],
'text-align': [/^(?:left|center|right|justify)$/],
},
},
transformTags: {
a: (tagName, attributes) => ({
tagName,
attribs: {
...attributes,
...(attributes.target === '_blank' ? { rel: 'noopener noreferrer' } : {}),
},
}),
},
})
}