# html/to-markdown
Convert HTML to markdown in the deft markdown-viewer dialect — the
pipeline for rendering HTML mail and documents with the markdown
viewer. WHATWG parsing (tag soup recovers like a browser), then a DOM
walk emitting exactly what the viewer renders.
## Syntax
[html/to-markdown $src|$doc ?opts]
## Parameters
- `$src` — HTML source text (string) or a `%HtmlDocument` handle from
`html/open`
- `$opts` — optional map:
- `:encoding` — charset label or `:auto` (string inputs; same as
`html/parse`)
- `:fragment true` — body-context fragment parse (string inputs)
- `:selector "css"` — convert only the matching subtrees
## Examples
[html/to-markdown "<h2>T</h2><p>a <b>b</b></p><ul><li>one</ul>"] # => "## T\n\na **b**\n\n- one" [html/to-markdown "see <a href='https://deft-lang.org'>docs</a>"] # => "see [docs](https://deft-lang.org)" # Only the main article of a page [html/to-markdown $body %{ :selector "article" }]
## Returns
A markdown string. Mapping: `h1`–`h6` → ATX headings clamped to `###`
(the viewer renders h1–h3 only); `b`/`strong`/`em`/`i` → `**` (italic
degrades to bold — the viewer has one emphasis style); `code` →
backticks; `pre` → fenced block, language from `class="language-x"`;
`a` → `[text](href)`; `img` → `` (the viewer degrades
unresolvable sources to an alt label itself); lists flatten to `- ` /
`N. ` (the viewer has no nesting); `blockquote` → `> `; tables → pipe
tables (cell pipes neutralised); `br` → newline; `hr` → paragraph gap;
script/style/head/template and form controls are dropped; whitespace
collapses like `html/text`, `<pre>` stays verbatim, entities are
decoded.
Failures return `%Err{ :message "..." }`.
## See Also
`html/from-markdown`, `html/text`, `html/open`, `html/parse`