Crates.io | htmd |
lib.rs | htmd |
version | 0.1.6 |
source | src |
created_at | 2024-06-16 12:56:21.1698 |
updated_at | 2024-07-11 07:45:17.646164 |
description | A turndown.js inspired HTML to Markdown converter. |
homepage | https://github.com/letmutex/htmd |
repository | https://github.com/letmutex/htmd |
max_upload_size | |
id | 1273506 |
size | 1,634,699 |
An HTML to Markdown converter for Rust, inspired by turndown.js.
Looking for the cli tool? Try htmd-cli now!
Add the dependency
htmd = "0.1"
fn main() {
assert_eq!("# Heading", htmd::convert("<h1>Heading</h1>").unwrap());
}
use htmd::HtmlToMarkdown;
let converter = HtmlToMarkdown::builder()
.skip_tags(vec!["script", "style"])
.build();
assert_eq!("", converter.convert("<script>let x = 0;</script>").unwrap());
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());
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());
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.
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.