feat: 完善内容管理通用富文本编辑器
This commit is contained in:
36
server/utils/rich-text.ts
Normal file
36
server/utils/rich-text.ts
Normal 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' } : {}),
|
||||
},
|
||||
}),
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user