7 releases

0.1.6 Jul 11, 2024
0.1.5 Jun 27, 2024

#571 in Text processing

Download history 330/week @ 2024-07-14 319/week @ 2024-07-21 265/week @ 2024-07-28 249/week @ 2024-08-04 369/week @ 2024-08-11 193/week @ 2024-08-18 285/week @ 2024-08-25 341/week @ 2024-09-01 504/week @ 2024-09-08 867/week @ 2024-09-15 1116/week @ 2024-09-22 985/week @ 2024-09-29 617/week @ 2024-10-06 334/week @ 2024-10-13 536/week @ 2024-10-20 658/week @ 2024-10-27

2,280 downloads per month
Used in 4 crates (3 directly)

Apache-2.0

285KB
1K SLoC

htmd

crates.io version

An HTML to Markdown converter for Rust, inspired by turndown.js.

Features

  • Rich options, same as turndown.js
  • Reliable, it passes all test cases of turndown.js
  • Minimum dependencies, it uses only html5ever
  • Fast, it takes ~70ms to convert a 1.37MB Wikipedia page on an i5 7th gen CPU (See Bench README)

Looking for the cli tool? Try htmd-cli now!

Usages

Add the dependency

htmd = "0.1"

Basic

fn main() {
    assert_eq!("# Heading", htmd::convert("<h1>Heading</h1>").unwrap());
}

Skip tags

use htmd::HtmlToMarkdown;

let converter = HtmlToMarkdown::builder()
    .skip_tags(vec!["script", "style"])
    .build();
assert_eq!("", converter.convert("<script>let x = 0;</script>").unwrap());

Options

use htmd::{options::Options, HtmlToMarkdown};

let converter = HtmlToMarkdown::builder()
    .options(Options {
        heading_style: htmd::options::HeadingStyle::Setex,
        ..Default::default()
    })
    .build();
assert_eq!("Heading\n=======", converter.convert("<h1>Heading</h1>").unwrap());

Custom tag handlers

use htmd::HtmlToMarkdown;

let converter = HtmlToMarkdown::builder()
    .add_handler(vec!["svg"], |_: Element| Some("[Svg Image]".to_string()))
    .build();
assert_eq!("[Svg Image]", converter.convert("<svg></svg>").unwrap());

Multithreading

You can safely share HtmlToMarkdown between multiple threads when only using built-in tag handlers.

let converter = Arc::new(HtmlToMarkdown::new());

for _ in 0..10 {
    let converter_clone = converter.clone();
    let handle = std::thread::spawn(move || {
        let md = converter_clone.convert("<h1>Hello</h1>").unwrap();
    });
}

If you have custom tag handlers that are not stateless, you likely need a thread-safe mechanism. See AnchorElementHandler for example.

Credits

License

Copyright 2024 letmutex

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Dependencies

~1.5–6.5MB
~35K SLoC