A markdown-it plugin built on DOMPurify.
npm install @markdown-design/markdown-it-sanitize一个基于 DOMPurify 的 markdown-it 插件。当开启 markdown-it 的 html 配置项时,会过滤输出的 HTML 标签,以预防 XSS 攻击。
``sh`
npm i @markdown-design/markdown-it-sanitize
`js
// demo.js
import markdownit from 'markdown-it'
import { sanitize } from '@markdown-design/markdown-it-sanitize'
const md = markdownit({ html: true })
md.use(sanitize)
const html = md.render('foo')
console.log(html)
// 输出:
foo
$3
你也可以传入 DOMPurify 的配置项,如:
`js
// demo.js
import markdownit from 'markdown-it'
import { sanitize } from '@markdown-design/markdown-it-sanitize'const md = markdownit({ html: true })
md.use(sanitize, { ALLOWED_TAGS: ['p', '#text'] })
const html = md.render(
'
goodbye
not me!
'
)
console.log(html)
// 输出:<iframe//src=JavScript:alert(((1)>goodbye
not me!
``本项目基于 MIT 协议。