# html-to-markdown
A robust html-to-markdown converter that transforms HTML (even entire websites) into clean, readable Markdown. It supports complex formatting, customizable options, and plugins for full control over the conversion process.
Use the fully extendable [Golang library](#golang-library) or a quick [CLI command](#cli---using-it-on-the-command-line). Alternatively, try the [Online Demo](https://html-to-markdown.com) or [REST API](https://html-to-markdown.com/api) to see it in action!
Here are some _cool features_:
- **Bold & Italic:** Supports bold and italicβeven within single words.

- **List:** Handles ordered and unordered lists with full nesting support.

- **Blockquote:** Blockquotes can include other elements, with seamless support for nested quotes.

- **Inline Code & Code Block:** Correctly handles backticks and multi-line code blocks, preserving code structure.

- **Link & Image:** Properly formats multi-line links, adding escapes for blank lines where needed.

- **Smart Escaping:** Escapes special characters only when necessary, to avoid accidental Markdown rendering.
ποΈ [ESCAPING.md](/ESCAPING.md)

- **Remove/Keep HTML:** Choose to strip or retain specific HTML tags for ultimate control over output.

- **Plugins:** Easily extend with plugins. Or create custom ones to enhance functionality.

- **Table Plugin:** Converts tables with support for alignment, rowspan and colspan.

---
## Usage
[π» Golang library](#golang-library) | [π¦ CLI](#cli---using-it-on-the-command-line) | [βΆοΈ Hosted Demo](https://html-to-markdown.com) | [π Hosted REST API](https://html-to-markdown.com/api)
---
## Golang Library
### Installation
```bash
go get -u github.com/JohannesKaufmann/html-to-markdown/v2
```
_Or if you want a specific commit add the suffix `/v2@commithash`_
> [!NOTE]
> This is the documentation for the v2 library. For the old version switch to the ["v1" branch](https://github.com/JohannesKaufmann/html-to-markdown/tree/v1).
### Usage
[](https://pkg.go.dev/github.com/JohannesKaufmann/html-to-markdown/v2)
```go
package main
import (
"fmt"
"log"
htmltomarkdown "github.com/JohannesKaufmann/html-to-markdown/v2"
)
func main() {
input := `Bold Text`
markdown, err := htmltomarkdown.ConvertString(input)
if err != nil {
log.Fatal(err)
}
fmt.Println(markdown)
// Output: **Bold Text**
}
```
- π§βπ» [Example code, basics](/examples/basics/main.go)
Use `WithDomain` to convert _relative_ links to _absolute_ links:
```go
package main
import (
"fmt"
"log"
htmltomarkdown "github.com/JohannesKaufmann/html-to-markdown/v2"
"github.com/JohannesKaufmann/html-to-markdown/v2/converter"
)
func main() {
input := `
`
markdown, err := htmltomarkdown.ConvertString(
input,
converter.WithDomain("https://example.com"),
)
if err != nil {
log.Fatal(err)
}
fmt.Println(markdown)
// Output: 
}
```
The function `htmltomarkdown.ConvertString()` is a _small wrapper_ around `converter.NewConverter()` and the _base_ and _commonmark_ plugins. If you want more control, use the following:
```go
package main
import (
"fmt"
"log"
"github.com/JohannesKaufmann/html-to-markdown/v2/converter"
"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/base"
"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/commonmark"
)
func main() {
input := `Bold Text`
conv := converter.NewConverter(
converter.WithPlugins(
base.NewBasePlugin(),
commonmark.NewCommonmarkPlugin(
commonmark.WithStrongDelimiter("__"),
// ...additional configurations for the plugin
),
// ...additional plugins (e.g. table)
),
)
markdown, err := conv.ConvertString(input)
if err != nil {
log.Fatal(err)
}
fmt.Println(markdown)
// Output: __Bold Text__
}
```
- π§βπ» [Example code, options](/examples/options/main.go)
> [!NOTE]
> If you use `NewConverter` directly make sure to also **register the commonmark and base plugin**.
---
### Collapse & Tag Type

You can specify how different HTML tags should be handled during conversion.
- **Tag Types:** When _collapsing_ whitespace it is useful to know if a node is _block_ or _inline_.
- So if you have Web Components/Custom Elements remember to register the type using `TagType` or `RendererFor`.
- Additionally, you can _remove_ tags completely from the output.
- **Pre-built Renderers:** There are several pre-built renderers available. For example:
- `RenderAsHTML` will render the node (including children) as HTML.
- `RenderAsHTMLWrapper` will render the node as HTML and render the children as markdown.
> [!NOTE]
> By default, some tags are automatically removed (e.g. `