tpnote_lib/
html2md.rs

1//! This module abstracts the HTML to Markdown filter.
2use crate::error::NoteError;
3use html2md::parse_html;
4
5/*
6// Alternative implementation:
7/// Abstracts the HTML to Markdown conversion.
8/// This implementation uses the `htmd` crate.
9#[inline]
10pub(crate) fn convert_html_to_md(html: &str) -> Result<String, NoteError> {
11    use htmd;
12    let converter = htmd::HtmlToMarkdown::builder()
13        .skip_tags(vec!["script", "style"])
14        .build();
15    converter.convert(&s).map_err(|e| NoteError::InvalidHtml {
16        source_str: e.to_string(),
17    })
18}
19*/
20
21/// Abstracts the HTML to Markdown conversion.
22/// This implementation uses the `html2md` crate.
23#[inline]
24pub(crate) fn convert_html_to_md(html: &str) -> Result<String, NoteError> {
25    Ok(parse_html(html))
26}
27
28#[cfg(test)]
29mod tests {
30
31    use crate::html2md::convert_html_to_md;
32
33    #[test]
34    fn test_convert_html_to_md() {
35        let input: &str =
36            "<div id=\"videopodcast\">outside <span id=\"pills\">inside</span>\n</div>";
37        let expected: &str = "outside inside";
38
39        let result = convert_html_to_md(input);
40        assert_eq!(result.unwrap(), expected);
41
42        //
43        let input: &str = r#"<p><a href="/my_uri">link</a></p>"#;
44        let expected: &str = "[link](/my_uri)";
45
46        let result = convert_html_to_md(input);
47        assert_eq!(result.unwrap(), expected);
48
49        //
50        // [Commonmark: Example 489](https://spec.commonmark.org/0.31.2/#example-489)
51        let input: &str = r#"<p><a href="/my uri">link</a></p>"#;
52        let expected: &str = "[link](</my uri>)";
53
54        let result = convert_html_to_md(input);
55        assert_eq!(result.unwrap(), expected);
56
57        //
58        // [Commonmark: Example 489](https://spec.commonmark.org/0.31.2/#example-489)
59        let input: &str = r#"<p><a href="/my%20uri">link</a></p>"#;
60        let expected: &str = "[link](</my uri>)";
61
62        let result = convert_html_to_md(input);
63        assert_eq!(result.unwrap(), expected);
64
65        //
66        // We want ATX style headers.
67        let input: &str = r#"<p><h1>Title</h1></p>"#;
68        let expected: &str = "# Title";
69
70        let result = convert_html_to_md(input);
71        assert_eq!(result.unwrap(), expected);
72    }
73}