htmd
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
- HTML table to Markdown table conversion
- Minimum dependencies, it uses only html5ever
- Fast, it takes ~16ms to convert a 1.37MB Wikipedia page on Apple M4 (See Bench README)
- Faithful mode, which can preserve HTML output for tags not supported by Markdown. (See #54)
Looking for the cli tool? Try htmd-cli now!
Usages
Add the dependency
= "0.5"
Basic
Skip tags
use HtmlToMarkdown;
let converter = builder
.skip_tags
.build;
assert_eq!;
Options
use ;
let converter = builder
.options
.build;
assert_eq!;
Custom tag handlers
use ;
let converter = builder
.add_handler
.build;
assert_eq!;
Tables
use convert;
let html = r#"
<table>
<thead>
<tr>
<th>Language</th>
<th>Type</th>
<th>Year</th>
</tr>
</thead>
<tbody>
<tr>
<td>Rust</td>
<td>Systems</td>
<td>2010</td>
</tr>
<tr>
<td>Python</td>
<td>Interpreted</td>
<td>1991</td>
</tr>
</tbody>
</table>
"#;
println!;
// Output:
// | Language | Type | Year |
// | -------- | ----------- | ---- |
// | Rust | Systems | 2010 |
// | Python | Interpreted | 1991 |
Multithreading
You can safely share HtmlToMarkdown between multiple threads when only using built-in tag handlers.
let converter = new;
for _ in 0..10
If you have custom tag handlers that are not stateless, you likely need a thread-safe mechanism. See AnchorElementHandler for example.
Bindings
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.