Convert HTML to Markdown (CommonMark), a MoonBit port of JohannesKaufmann/html-to-markdown v2.
Dependencies
moon add hustcer/html2mdimport { "hustcer/html2md" }///|
test "basic" {
let html =
#|<h1>Hello</h1>
#|<p>Some <b>bold</b> and <i>italic</i> text with a
#|<a href="https://example.com">link</a>.</p>
inspect(
@html2md.convert(html),
content=(
#|# Hello
#|
#|Some **bold** and *italic* text with a [link](https://example.com).
),
)
}///|
test "blocks" {
let html =
#|<ul><li>first</li><li>second</li></ul>
#|<pre><code class="language-go">x := 1</code></pre>
#|<blockquote>a quote</blockquote>
inspect(
@html2md.convert(html),
content=(
#|- first
#|- second
#|
#|```go
#|x := 1
#|```
#|
#|> a quote
),
)
}///|
test "gfm" {
let html =
#|<p><del>old</del> value</p>
#|<table>
#| <tr><th>Name</th><th>Value</th></tr>
#| <tr><td>A</td><td>1 | 2</td></tr>
#|</table>
inspect(
@html2md.convert(html),
content=(
#|~~old~~ value
#|
#|| Name | Value |
#|| --- | --- |
#|| A | 1 \| 2 |
),
)
}///|
test "escaping" {
inspect(
@html2md.convert("<p>fake **bold** and real <strong>bold</strong></p>"),
content=(
#|fake \*\*bold\** and real **bold**
),
)
}///|
test "escaping disabled" {
inspect(
@html2md.convert("<p>1. not a list</p>", escape_mode=Disabled),
content="1. not a list",
)
}///|
test "domain" {
inspect(
@html2md.convert("<a href=\"/page\">link</a>", domain="https://example.com"),
content="[link](https://example.com/page)",
)
inspect(
@html2md.convert(
"<a href=\"?q=hello world&next=/a?b=1&plus=a+b\">search</a>",
domain="https://example.com/path?old=1",
),
content="[search](https://example.com/path?q=hello%20world&next=/a?b=1&plus=a+b)",
)
}| Option | Type | Default | Notes |
|---|---|---|---|
| domain | String | "" | Base domain for relative URLs |
| heading_style | HeadingStyle | Atx | Atx (# H) or Setext (underlined h1/h2) |
| em_delimiter | String | "*" | "*" or "_" |
| strong_delimiter | String | "**" | "**" or "__" |
| horizontal_rule | String | "* * *" | any thematic break (≥3 of */_/-) |
| bullet_list_marker | String | "-" | "-", "+" or "*" |
| code_block_fence | String | "```" | "```" or "~~~" |
| escape_mode | EscapeMode | Smart | Smart or Disabled |
| link_empty_href_behavior | LinkBehavior | Render | Render keeps [text](), Skip drops the link |
| link_empty_content_behavior | LinkBehavior | Render | Render keeps [](href), Skip drops the link |
| list_end_comment | Bool | true | insert <!--THE END--> between adjacent lists |
moon fmt
moon check --target all
moon test --target all
moon infofn convert(html : String, domain? : String, heading_style? : HeadingStyle, em_delimiter? : String, strong_delimiter? : String, horizontal_rule? : String, bullet_list_marker? : String, code_block_fence? : String, escape_mode? : EscapeMode, link_empty_href_behavior? : LinkBehavior, link_empty_content_behavior? : LinkBehavior, list_end_comment? : Bool) -> String raise ConvertErrortest {
inspect(
@html2md.convert("<h1>Title</h1><p>Some <b>bold</b> text.</p>"),
content=(
#|# Title
#|
#|Some **bold** text.
),
)
}fn convert_dom(doc : Node, domain? : String, heading_style? : HeadingStyle, em_delimiter? : String, strong_delimiter? : String, horizontal_rule? : String, bullet_list_marker? : String, code_block_fence? : String, escape_mode? : EscapeMode, link_empty_href_behavior? : LinkBehavior, link_empty_content_behavior? : LinkBehavior, list_end_comment? : Bool) -> String raise ConvertErrorConvert HTML to Markdown (CommonMark), a MoonBit port of JohannesKaufmann/html-to-markdown v2.
Dependencies