html-to-markdown

RustPythonNode.jsWASMJavaGoC#PHPRubyElixirRLicense
html-to-markdown
Discord

Elixir bindings for the Rust html-to-markdown engine. The package exposes a fast HTML to Markdown converter implemented with Rustler. Ship identical Markdown across every runtime while enjoying native performance with Rustler NIF bindings.

Installation

Add {:html_to_markdown, "~> 2.24.1"} to mix.exs deps

Requires Elixir 1.19+ and OTP 28. Add to your mix.exs:

def deps do
  [
    {:html_to_markdown, "~> 2.27.1"}
  ]
end

Performance Snapshot

Apple M4 • Real Wikipedia documents • convert() (Elixir)

Document Size Ops/sec Throughput
Lists (Timeline) 129KB 2,547 321.7 MB/s
Tables (Countries) 360KB 835 293.8 MB/s
Medium (Python) 656KB 439 281.5 MB/s
Large (Rust) 567KB 485 268.7 MB/s
Small (Intro) 463KB 581 262.9 MB/s
HOCR German PDF 44KB 7,106 303.1 MB/s
HOCR Embedded Tables 37KB 6,231 226.1 MB/s
HOCR Invoice 4KB 62,657 256.4 MB/s

See Performance Guide for detailed benchmarks.

Quick Start

Basic conversion:

{:ok, markdown} = HtmlToMarkdown.convert("<h1>Hello</h1><p>This is <strong>fast</strong>!</p>")
IO.puts(markdown)

With conversion options:

handle = HtmlToMarkdown.options(%HtmlToMarkdown.Options{wrap: true, wrap_width: 40})
{:ok, markdown} = HtmlToMarkdown.convert_with_options("<h1>Hello</h1><p>World</p>", handle)
IO.puts(markdown)

API Reference

Core Functions

HtmlToMarkdown.convert(html, options \\ nil) :: String.t()

Basic HTML-to-Markdown conversion. Fast and simple.

HtmlToMarkdown.convert_with_metadata(html, options \\ nil, config \\ nil) :: {String.t(), map()}

Extract Markdown plus metadata in a single pass. See Metadata Extraction Guide.

HtmlToMarkdown.convert_with_inline_images(html, config \\ nil) :: {String.t(), list(map()), list(String.t())}

Extract base64-encoded inline images with metadata.

Options

ConversionOptions – Key configuration fields:

MetadataConfig – Selective metadata extraction:

Djot Output Format

The library supports converting HTML to Djot, a lightweight markup language similar to Markdown but with a different syntax for some elements. Set output_format to "djot" to use this format.

Syntax Differences

Element Markdown Djot
Strong **text***text*
Emphasis *text*_text_
Strikethrough ~~text~~{-text-}
Inserted/Added N/A {+text+}
Highlighted N/A {=text=}
Subscript N/A ~text~
Superscript N/A ^text^

Example Usage

html = "<p>This is <strong>bold</strong> and <em>italic</em> text.</p>"

# Default Markdown output
{:ok, markdown} = HtmlToMarkdown.convert(html)
# Result: "This is **bold** and *italic* text."

# Djot output
{:ok, djot} = HtmlToMarkdown.convert(html, %{output_format: "djot"})
# Result: "This is *bold* and _italic_ text."

Djot's extended syntax allows you to express more semantic meaning in lightweight text, making it useful for documents that require strikethrough, insertion tracking, or mathematical notation.

Plain Text Output

Set output_format to "plain" to strip all markup and return only visible text. This bypasses the Markdown conversion pipeline entirely for maximum speed.

html = "<h1>Title</h1><p>This is <strong>bold</strong> and <em>italic</em> text.</p>"

{:ok, plain} = HtmlToMarkdown.convert(html, %{output_format: "plain"})
# Result: "Title\n\nThis is bold and italic text."

Plain text mode is useful for search indexing, text extraction, and feeding content to LLMs.

Metadata Extraction

The metadata extraction feature enables comprehensive document analysis during conversion. Extract document properties, headers, links, images, and structured data in a single pass.

Use Cases:

Zero Overhead When Disabled: Metadata extraction adds negligible overhead and happens during the HTML parsing pass. Disable unused metadata types in MetadataConfig to optimize further.

Example: Quick Start

alias HtmlToMarkdown

html = "<h1>Article</h1><img src=\"test.jpg\" alt=\"test\">"
{markdown, metadata} = HtmlToMarkdown.convert_with_metadata(html)

IO.inspect(metadata.document.title)        # Document title
IO.inspect(metadata.headers)               # All h1-h6 elements
IO.inspect(metadata.links)                 # All hyperlinks
IO.inspect(metadata.images)                # All images with alt text
IO.inspect(metadata.structured_data)       # JSON-LD, Microdata, RDFa

For detailed examples including SEO extraction, table-of-contents generation, link validation, and accessibility audits, see the Metadata Extraction Guide.

Visitor Pattern

The visitor pattern enables custom HTML→Markdown conversion logic by providing callbacks for specific HTML elements during traversal. Use visitors to transform content, filter elements, validate structure, or collect analytics.

Use Cases:

Supported Visitor Methods: 40+ callbacks for text, inline elements, links, images, headings, lists, blocks, and tables.

Example: Quick Start

defmodule MyVisitor do
  def visit_link(ctx, href, text, title) do
    # Rewrite CDN URLs
    href = if String.starts_with?(href, "https://old-cdn.com") do
      String.replace(href, "https://old-cdn.com", "https://new-cdn.com")
    else
      href
    end
    {:custom, "[#{text}](#{href})"}
  end

  def visit_image(ctx, src, alt, title) do
    # Skip tracking pixels
    if String.contains?(src, "tracking") do
      :skip
    else
      :continue
    end
  end
end

html = "<a href=\"https://old-cdn.com/file.pdf\">Download</a>"
markdown = HtmlToMarkdown.convert_with_visitor(html, visitor: MyVisitor)

For comprehensive examples including content filtering, link footnotes, accessibility validation, and asynchronous URL validation, see the Visitor Pattern Guide.

Examples

Links

Contributing

We welcome contributions! Please see our Contributing Guide for details on:

All contributions must follow our code quality standards (enforced via pre-commit hooks):

License

MIT License – see LICENSE.

Support

If you find this library useful, consider sponsoring the project.

Have questions or run into issues? We're here to help: