DOM Tree Transformation Invariants & Markdown Equations
Converting HTML documents into CommonMark Markdown maps semantic DOM elements into lightweight plain text tokens:
1. Semantic Tag Mapping Function
M(T) = (<h1>C</h1> ⇒ "# " + M(C)) ∧ (<strong>C</strong> ⇒ "**" + M(C) + "**") ∧ (<a href=U>C</a> ⇒ "[" + M(C) + "](" + U + ")")
2. Markdown Compression Ratio
Compression Ratio =
Length(Markdown Chars)Length(Raw HTML Chars)
× 100%Step-by-Step HTML to Markdown Conversion Breakdown
Step 1: Comment Removal & Entity Decoding
Strip
<!-- comments --> and decode &, <, >.Step 2: Semantic Block & Inline Transformation
Map headings to
#, links to [text](url), and pre/code to triple backticks.Step 3: Wrapper Stripping & Whitespace Normalization
Result=Clean CommonMark Document (.md)
HTML vs Markdown Syntax Mapping Reference
| HTML Element | Markdown Equivalent | Rendered Output |
|---|---|---|
| <h1>Heading 1</h1> | # Heading 1 | Heading 1 |
| <strong>Bold</strong> | **Bold** | Bold |
| <a href="url">Link</a> | [Link](url) | Link |
| <pre><code>code</code></pre> | ```code``` | code block |
| <blockquote>quote</blockquote> | > quote | “quote” |