37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
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' } : {}),
|
|
},
|
|
}),
|
|
},
|
|
})
|
|
}
|