我试图在将 html 字符串发送到数据库之前对其进行清理,我正在使用 sanitize-html npm 包,但它不起作用
if (noteContent) {
const resultContent = sanitize(noteContent);
console.log(resultContent);
} else {
setErrorMessages((prevState) => {
return ["note content cannot be empty"];
});
}
当我进入时
它返回:
根据 Github 上提供的文档,您正在寻找的函数应该称为
sanitizeHtml()
而不是 sanitize()
但是,如果您仔细阅读,默认情况下它确实允许此处列出的相当多的 html 标签和属性:https://github.com/apostropecms/sanitize-html?tab=readme-ov-file#default-options
为了更加严格,请遵循其文档中提供的示例。
// Allow only a super restricted set of tags and attributes
const clean = sanitizeHtml(dirty, {
allowedTags: [ 'b', 'i', 'em', 'strong', 'a' ],
allowedAttributes: {
'a': [ 'href' ]
},
allowedIframeHostnames: ['www.youtube.com']
});