mdka/lib.rs
1//! HTML to Markdown converter - Lightweight library written in Rust.
2
3mod elements;
4mod nodes;
5mod utils;
6
7use nodes::{node::root_node_md, utils::parse_html};
8use utils::file::{read_from_filepath, write_to_filepath};
9
10#[cfg(feature = "pyo3")]
11pub mod python_bindings;
12
13#[cfg(feature = "napi")]
14pub mod nodejs_bindings;
15
16/// Convert HTML to Markdown
17///
18/// ```
19/// use mdka::from_html;
20///
21/// let html_text = r#"
22/// <h1>heading 1</h1>
23/// <p>Hello, world.</p>"#;
24/// let expect = "# heading 1\n\nHello, world.\n\n";
25///
26/// let ret = from_html(html_text);
27/// assert_eq!(ret, expect);
28/// ```
29///
30pub fn from_html(html_text: &str) -> String {
31 let dom = parse_html(html_text);
32 root_node_md(&dom.document, None::<usize>)
33}
34
35/// Convert HTML to Markdown
36///
37/// ```
38/// use mdka::from_file;
39///
40/// let html_filepath = "tests/fixtures/simple-01.html";
41/// let expect = "# heading 1\n\nHello, world.\n\n";
42///
43/// let ret = from_file(html_filepath).expect("Failed to read");
44/// assert_eq!(ret.as_str(), expect);
45/// ```
46///
47pub fn from_file(html_filepath: &str) -> Result<String, String> {
48 let html_text = read_from_filepath(html_filepath)
49 .expect(format!("Failed to read: {}", html_filepath).as_str());
50 Ok(from_html(html_text.as_str()))
51}
52
53/// Convert HTML to Markdown
54///
55/// ```
56/// use mdka::from_html_to_file;
57///
58/// let html_text = r#"
59/// <h1>heading 1</h1>
60/// <p>Hello, world.</p>"#;
61/// let markdown_filepath = "tests/tmp/from_html_file_doc_test_result.html";
62/// let expect = "# heading 1\n\nHello, world.\n\n";
63///
64/// let ret = from_html_to_file(html_text, markdown_filepath, true).expect("Failed to write");
65/// let markdown_file_content = std::fs::read_to_string(markdown_filepath).expect("Failed to read from markdown filepath");
66/// assert_eq!(expect, markdown_file_content);
67/// ```
68///
69pub fn from_html_to_file(
70 html_text: &str,
71 markdown_filepath: &str,
72 overwrites: bool,
73) -> Result<(), String> {
74 let md = from_html(html_text);
75 write_to_filepath(md.as_str(), markdown_filepath, overwrites)
76}
77
78/// Convert HTML to Markdown
79///
80/// ```
81/// use mdka::from_file_to_file;
82///
83/// let html_filepath = "tests/fixtures/simple-01.html";
84/// let markdown_filepath = "tests/tmp/from_html_file_doc_test_result.html";
85/// let expect = "# heading 1\n\nHello, world.\n\n";
86///
87/// let ret = from_file_to_file(html_filepath, markdown_filepath, true).expect("Failed to write");
88/// let markdown_file_content = std::fs::read_to_string(markdown_filepath).expect("Failed to read from markdown filepath");
89/// assert_eq!(expect, markdown_file_content);
90/// ```
91///
92pub fn from_file_to_file(
93 html_filepath: &str,
94 markdown_filepath: &str,
95 overwrites: bool,
96) -> Result<(), String> {
97 let html = read_from_filepath(html_filepath)
98 .expect(format!("Failed to read: {}", html_filepath).as_str());
99 from_html_to_file(html.as_str(), markdown_filepath, overwrites)
100}