Skip to main content

html2md/
lib.rs

1use extended::sifter::{WhitespaceSifter, WhitespaceSifterBytes};
2
3// we want to just use the rewriter instead for v0.1.
4pub mod extended;
5
6#[cfg(feature = "scraper")]
7pub use markup5ever_rcdom::{Handle, NodeData, RcDom};
8
9#[cfg(feature = "rewriter")]
10pub mod rewriter;
11#[cfg(feature = "scraper")]
12pub mod scraper;
13#[cfg(feature = "scraper")]
14pub use scraper::{
15    ignore, parse_html, parse_html_custom, parse_html_custom_base, parse_html_custom_with_url,
16    parse_html_extended,
17};
18
19// Regex patterns only needed for the scraper feature
20#[cfg(feature = "scraper")]
21lazy_static::lazy_static! {
22    pub(crate) static ref MARKDOWN_MIDDLE_KEYCHARS: regex::Regex =
23        regex::Regex::new(r"[<>*\\_~]").expect("valid regex pattern");
24}
25
26/// Main function of this library to come. Rewrites incoming HTML, converts it into Markdown
27/// and returns converted string. Incomplete work in progress for major performance increases.
28/// # Arguments
29/// `html` is source HTML as `String`
30#[cfg(feature = "rewriter")]
31pub fn rewrite_html(html: &str, commonmark: bool) -> String {
32    rewriter::writer::convert_html_to_markdown(html, &None, commonmark, &None).unwrap_or_default()
33}
34
35/// Main function of this library async streaming. Rewrites incoming HTML, converts it into Markdown
36/// and returns converted string. Incomplete work in progress for major performance increases.
37/// # Arguments
38/// `html` is source HTML as `String`
39#[cfg(all(feature = "stream", feature = "rewriter"))]
40pub async fn rewrite_html_streaming(html: &str, commonmark: bool) -> String {
41    rewriter::writer::convert_html_to_markdown_send(html, &None, commonmark, &None)
42        .await
43        .unwrap_or_default()
44}
45
46/// Custom variant of rewrite function.
47///
48/// You can also override standard tag handlers this way
49/// # Arguments
50/// `html` is source HTML as `String`
51/// `custom` is custom tag hadler producers for tags you want, can be empty
52/// `commonmark` is for adjusting markdown output to commonmark
53/// `url` is used to provide absolute url handling
54#[cfg(all(feature = "stream", feature = "rewriter"))]
55pub fn rewrite_html_custom_with_url(
56    html: &str,
57    custom: &Option<std::collections::HashSet<String>>,
58    commonmark: bool,
59    url: &Option<url::Url>,
60) -> String {
61    rewriter::writer::convert_html_to_markdown(html, &custom, commonmark, url).unwrap_or_default()
62}
63
64/// Custom variant of rewrite function.
65///
66/// You can also override standard tag handlers this way
67/// # Arguments
68/// `html` is source HTML as `String`
69/// `custom` is custom tag hadler producers for tags you want, can be empty
70/// `commonmark` is for adjusting markdown output to commonmark
71/// `url` is used to provide absolute url handling
72/// `chunk_size` the chunk size to use.
73#[cfg(all(feature = "stream", feature = "rewriter"))]
74pub async fn rewrite_html_custom_with_url_and_chunk(
75    html: &str,
76    custom: &Option<std::collections::HashSet<String>>,
77    commonmark: bool,
78    url: &Option<url::Url>,
79    chunk_size: usize,
80) -> String {
81    rewriter::writer::convert_html_to_markdown_send_with_size(
82        html, &custom, commonmark, url, chunk_size,
83    )
84    .await
85    .unwrap_or_default()
86}
87
88/// Custom variant of rewrite function streaming async.
89///
90/// You can also override standard tag handlers this way
91/// # Arguments
92/// `html` is source HTML as `String`
93/// `custom` is custom tag hadler producers for tags you want, can be empty
94/// `commonmark` is for adjusting markdown output to commonmark
95/// `url` is used to provide absolute url handling
96#[cfg(all(feature = "stream", feature = "rewriter"))]
97pub async fn rewrite_html_custom_with_url_streaming(
98    html: &str,
99    custom: &Option<std::collections::HashSet<String>>,
100    commonmark: bool,
101    url: &Option<url::Url>,
102) -> String {
103    rewriter::writer::convert_html_to_markdown_send(html, &custom, commonmark, url)
104        .await
105        .unwrap_or_default()
106}
107
108/// Re-export the stream error type.
109#[cfg(all(feature = "stream", feature = "rewriter"))]
110pub use rewriter::writer::StreamConvertError;
111
112/// Convert an async stream of HTML byte chunks into markdown.
113///
114/// Genuinely async — yields to the executor while waiting for each input chunk.
115/// Accepts any `Stream<Item = Result<B, E>>` where `B: AsRef<[u8]>`, so it works
116/// with `Vec<u8>`, `bytes::Bytes`, `&[u8]`, etc.
117///
118/// # Arguments
119/// * `stream` - an async stream of byte chunks (e.g. from an HTTP response body)
120/// * `commonmark` - adjust output to CommonMark spec
121#[cfg(all(feature = "stream", feature = "rewriter"))]
122pub async fn rewrite_html_stream<S, B, E>(
123    stream: S,
124    commonmark: bool,
125) -> Result<String, StreamConvertError<E>>
126where
127    S: futures_util::Stream<Item = Result<B, E>> + Unpin,
128    B: AsRef<[u8]>,
129{
130    rewriter::writer::convert_html_stream_to_markdown(stream, &None, commonmark, &None).await
131}
132
133/// Convert an async stream of HTML byte chunks into markdown with custom options.
134///
135/// # Arguments
136/// * `stream` - an async stream of byte chunks
137/// * `custom` - custom tag handler producers for tags to ignore
138/// * `commonmark` - adjust output to CommonMark spec
139/// * `url` - base URL for resolving relative links
140#[cfg(all(feature = "stream", feature = "rewriter"))]
141pub async fn rewrite_html_stream_custom_with_url<S, B, E>(
142    stream: S,
143    custom: &Option<std::collections::HashSet<String>>,
144    commonmark: bool,
145    url: &Option<url::Url>,
146) -> Result<String, StreamConvertError<E>>
147where
148    S: futures_util::Stream<Item = Result<B, E>> + Unpin,
149    B: AsRef<[u8]>,
150{
151    rewriter::writer::convert_html_stream_to_markdown(stream, custom, commonmark, url).await
152}
153
154/// Called after all processing has been finished
155///
156/// Clears excessive punctuation that would be trimmed by renderer anyway
157pub fn clean_markdown(input: &str) -> String {
158    input.sift_preserve_newlines()
159}
160
161/// Called after all processing has been finished
162///
163/// Clears excessive punctuation that would be trimmed by renderer anyway
164pub fn clean_markdown_bytes(input: &Vec<u8>) -> String {
165    input.sift_bytes_preserve_newlines()
166}
167
168/// Check if a byte needs markdown escaping.
169#[inline]
170const fn needs_escape(b: u8) -> bool {
171    matches!(b, b'<' | b'>' | b'*' | b'\\' | b'_' | b'~')
172}
173
174/// Check if byte could start a special sequence (escape char or &nbsp;).
175#[inline]
176const fn is_special_byte(b: u8) -> bool {
177    needs_escape(b) || b == b'&'
178}
179
180/// Check if a string contains any characters that need markdown escaping.
181/// Use this to avoid allocation when no escaping is needed.
182#[inline]
183pub fn contains_markdown_chars(input: &str) -> bool {
184    input.as_bytes().iter().any(|&b| is_special_byte(b))
185}
186
187/// Decode a single HTML entity at the start of a byte slice.
188/// Returns the decoded string and the number of bytes consumed, or None if not a recognized entity.
189/// Handles named entities (&amp; &lt; &gt; &quot; &nbsp; &apos;) and numeric (&#N; &#xH;).
190#[inline]
191fn decode_html_entity(bytes: &[u8]) -> Option<(&'static str, usize)> {
192    debug_assert_eq!(bytes[0], b'&');
193
194    // Find the semicolon (cap search at 10 bytes for perf — longest named entity we care about is &nbsp; = 6)
195    let limit = bytes.len().min(12);
196    let semi = bytes[1..limit].iter().position(|&b| b == b';')?;
197    let entity = &bytes[1..semi + 1]; // between & and ;
198    let consumed = semi + 2; // includes & and ;
199
200    match entity {
201        b"amp" => Some(("&", consumed)),
202        b"lt" => Some(("\\<", consumed)),
203        b"gt" => Some(("\\>", consumed)),
204        b"quot" => Some(("\"", consumed)),
205        b"apos" => Some(("'", consumed)),
206        b"nbsp" => Some(("", consumed)), // strip non-breaking spaces like before
207        _ if entity.first() == Some(&b'#') => decode_numeric_entity(entity, consumed),
208        _ => None,
209    }
210}
211
212/// Decode numeric HTML entities: &#39; &#x27; etc.
213#[inline]
214fn decode_numeric_entity(entity: &[u8], consumed: usize) -> Option<(&'static str, usize)> {
215    let (digits, radix) = if entity.get(1) == Some(&b'x') || entity.get(1) == Some(&b'X') {
216        (&entity[2..], 16)
217    } else {
218        (&entity[1..], 10)
219    };
220
221    if digits.is_empty() {
222        return None;
223    }
224
225    // Parse into a u32 without allocation
226    let mut val: u32 = 0;
227    for &b in digits {
228        let d = match b {
229            b'0'..=b'9' => (b - b'0') as u32,
230            b'a'..=b'f' if radix == 16 => (b - b'a' + 10) as u32,
231            b'A'..=b'F' if radix == 16 => (b - b'A' + 10) as u32,
232            _ => return None,
233        };
234        val = val.checked_mul(radix)?.checked_add(d)?;
235    }
236
237    // Map common code points to static strings to avoid allocation
238    match val {
239        0x26 => Some(("&", consumed)),          // &
240        0x3C => Some(("\\<", consumed)),         // <
241        0x3E => Some(("\\>", consumed)),         // >
242        0x22 => Some(("\"", consumed)),          // "
243        0x27 => Some(("'", consumed)),           // '
244        0xA0 => Some(("", consumed)),            // nbsp
245        0x2014 => Some(("\u{2014}", consumed)),  // em dash
246        0x2013 => Some(("\u{2013}", consumed)),  // en dash
247        0x2018 => Some(("\u{2018}", consumed)),  // left single quote
248        0x2019 => Some(("\u{2019}", consumed)),  // right single quote
249        0x201C => Some(("\u{201c}", consumed)),  // left double quote
250        0x201D => Some(("\u{201d}", consumed)),  // right double quote
251        _ => None, // unrecognized numeric entity — pass through as-is
252    }
253}
254
255/// Replace the markdown chars cleanly.
256/// Optimized to scan bytes and process in bulk segments.
257/// Returns None if no changes needed (avoids allocation).
258#[inline]
259pub fn replace_markdown_chars_opt(input: &str) -> Option<String> {
260    let bytes = input.as_bytes();
261
262    // Fast path: scan for any special character
263    let first_special = bytes.iter().position(|&b| is_special_byte(b));
264
265    match first_special {
266        None => None,
267        Some(first_pos) => {
268            // Pre-allocate with some headroom for escapes
269            let mut output = String::with_capacity(input.len() + input.len() / 8);
270
271            // Copy everything before first special char
272            output.push_str(&input[..first_pos]);
273
274            // Process the rest byte-by-byte from first_pos
275            let mut i = first_pos;
276            while i < bytes.len() {
277                let b = bytes[i];
278
279                if needs_escape(b) {
280                    output.push('\\');
281                    output.push(b as char);
282                    i += 1;
283                } else if b == b'&' {
284                    // Decode HTML entities in a single pass
285                    if let Some((decoded, len)) = decode_html_entity(&bytes[i..]) {
286                        output.push_str(decoded);
287                        i += len;
288                    } else {
289                        output.push('&');
290                        i += 1;
291                    }
292                } else {
293                    // Find the next special character and copy the segment
294                    let segment_start = i;
295                    i += 1;
296                    while i < bytes.len() && !is_special_byte(bytes[i]) {
297                        i += 1;
298                    }
299                    output.push_str(&input[segment_start..i]);
300                }
301            }
302
303            Some(output)
304        }
305    }
306}
307
308/// Replace the markdown chars cleanly.
309/// Optimized to scan bytes and process in bulk segments.
310#[inline]
311pub fn replace_markdown_chars(input: &str) -> String {
312    replace_markdown_chars_opt(input).unwrap_or_else(|| input.to_string())
313}