A markdown-it plugin that sanitizes html_block and html_inline tokens using DOMPurify.
This helps prevent XSS and ensures only safe HTML is rendered from Markdown.
- Sanitizes embedded HTML inside Markdown
- Supports
html_blockandhtml_inlinetokens - Passes options directly to DOMPurify (
ADD_TAGS,ALLOWED_TAGS, etc.) - Easy to use and minimal
npm install markdown-it-purifier dompurifyimport MarkdownIt from 'markdown-it'
import markdownItPurifier from 'markdown-it-purifier'
const md = new MarkdownIt({ html: true })
md.use(markdownItPurifier, {
// These options are passed directly to DOMPurify
ADD_TAGS: ['iframe'],
ADD_ATTR: ['src', 'width', 'height', 'allow', 'allowfullscreen']
})
const result = md.render(`
# Hello
<iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" allowfullscreen></iframe>
`)
console.log(result)You can pass any DOMPurify options directly into this plugin.
- To add tags or attributes, use
ADD_TAGS/ADD_ATTR. - To fully override the whitelist, use
ALLOWED_TAGS/ALLOWED_ATTR.
This plugin uses DOMPurify internally and does not maintain its own allowlist. Make sure to review the DOMPurify security docs if you're processing untrusted user input.
MIT