html-safe-md
Convert untrusted HTML into safe, readable markdown. No remote fetches, no scripts, no tracking pixels.
Built for email. Useful anywhere you need to render HTML from an untrusted source without a webview or JavaScript execution.
Quick Start
// Sanitize HTML to markdown with default email-tuned config
let md = sanitize_html;
assert!;
// Render an email body (prefers plain text, falls back to sanitized HTML)
let md = render_email;
assert!;
The Problem
HTML in email (and app stores, RSS feeds, CMS content) is a privacy and security minefield:
- Tracking pixels —
<img>tags that phone home when you open a message - Remote image loads — leak your IP, client, and read-time to senders
- JavaScript — rare in email, devastating when it lands
- CSS exfiltration — fingerprinting and data leaks via style rules
- Layout tables — produce garbage when naively converted to markdown
The standard approaches are both bad:
- Full webview rendering — you're running the sender's code
- Plain text only — loses all structure, links, formatting
The Solution
A middle path: sanitize HTML down to semantic content, then convert to markdown.
untrusted HTML
→ ammonia (allowlist-based sanitizer, strips everything dangerous)
→ html2md (structural conversion to markdown)
→ safe markdown string
The output is a plain string. What you render it with — iced, ratatui, egui, a terminal pager — is your business.
What Survives
Only semantic tags that produce meaningful markdown:
- Block:
p,br,hr,blockquote,pre - Headings:
h1–h6 - Inline:
b,strong,i,em,code,s,del,u,small,sub,sup - Lists:
ul,ol,li - Links:
a(ammonia sanitizeshref— nojavascript:URIs)
What Gets Killed
Everything else. Text content inside stripped tags is preserved — only the tags are removed.
<img>— no remote fetches, no tracking pixels, no beacon GIFs<table>,<tr>,<td>— layout tables that produce markdown soup<style>— CSS exfiltration and fingerprinting<script>— obvious<iframe>,<object>,<embed>— embedded content<form>,<input>— phishing vectors- MSO conditionals — Office junk
- All inline
style=""attributes
API
// Core: sanitize any untrusted HTML to markdown
sanitize_html // With custom config (extra tags, size limits)
sanitize_html_with // Email convenience: prefers plain text, falls back to sanitized HTML
render_email // Email plain text output (no markdown, uses html2text for fallback)
render_email_plain // Detect junk/stub plain text parts
is_junk_plain
Configuration
use Config;
let config = Config ;
let md = sanitize_html_with;
Safety Limits
- Input truncation: HTML larger than 512 KB is truncated before processing
- Output cap: Markdown output capped at 200K characters
Email Junk Detection
Many emails include both text/plain and text/html. The plain version is preferred (already safe), but some senders use it as a stub:
View this email in your browser
render_email detects these stubs (empty, under 40 chars, or ≤2 lines) and falls through to the HTML sanitization pipeline.
Design Principles
- Allowlist, not denylist. New HTML features are blocked by default.
- Text content is sacred. Stripped tags lose their markup, not their content.
- No remote fetches. Zero network activity. If it's not inline, it doesn't exist.
- No framework coupling. Output is a string. Rendering is your problem.
- Sane defaults, configurable when needed. Tuned for email. Override for other contexts.
Use Cases
- Email clients — nevermail (the origin of this crate)
- App stores — safely render app descriptions from upstream sources
- RSS/feed readers — untrusted HTML from feeds, same threat model
- CMS/comment systems — anywhere user-submitted HTML needs safe display
Dependencies
ammonia— HTML sanitizationhtml2md— HTML to markdown conversionhtml2text— HTML to plain text fallback
No UI framework dependencies. Stable Rust. No unsafe.
License
Dual-licensed under MIT or Apache-2.0, at your option.
"Display the message. Not the sender's agenda."