diff --git a/src/lib.rs b/src/lib.rs index 0acee547..d8200344 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,13 @@ //! Library for fixing markdown tables. //! //! Functions here reflow tables that were broken during formatting. +//! The [`convert_html_tables`] helper is re-exported at the crate root so +//! callers can convert simple HTML tables before reflowing. mod html; +pub use html::convert_html_tables; + use regex::Regex; use std::fs; use std::path::Path; diff --git a/tests/integration.rs b/tests/integration.rs index cacc2efc..5d3f471d 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1,5 +1,5 @@ use assert_cmd::Command; -use mdtablefix::{process_stream, reflow_table}; +use mdtablefix::{convert_html_tables, process_stream, reflow_table}; use rstest::{fixture, rstest}; use std::fs::File; use std::io::Write; @@ -83,6 +83,36 @@ fn html_table_with_attrs() -> Vec { ] } +#[fixture] +fn html_table_with_colspan() -> Vec { + vec![ + "".to_string(), + "".to_string(), + "".to_string(), + "
A
12
".to_string(), + ] +} + +#[fixture] +fn html_table_no_header() -> Vec { + vec![ + "".to_string(), + "".to_string(), + "".to_string(), + "
AB
12
".to_string(), + ] +} + +#[fixture] +fn html_table_empty() -> Vec { + vec!["
".to_string()] +} + +#[fixture] +fn html_table_unclosed() -> Vec { + vec!["".to_string(), "".to_string()] +} + #[fixture] fn html_table_uppercase() -> Vec { vec![ @@ -344,3 +374,77 @@ fn test_non_table_lines_unchanged() { ]; assert_eq!(output, expected); } + +#[test] +fn test_convert_html_table_basic() { + let html_table = vec![ + "
1
".to_string(), + "".to_string(), + "".to_string(), + "
AB
12
".to_string(), + ]; + let expected = vec![ + "| A | B |".to_string(), + "| --- | --- |".to_string(), + "| 1 | 2 |".to_string(), + ]; + assert_eq!(convert_html_tables(&html_table), expected); +} + +#[rstest] +#[case("```")] +#[case("~~~")] +#[case("```rust")] +fn test_convert_html_table_in_text_and_code(#[case] fence: &str) { + let lines = vec![ + "Intro".to_string(), + "".to_string(), + "".to_string(), + "".to_string(), + "
AB
12
".to_string(), + fence.to_string(), + "
x
".to_string(), + fence.to_string(), + "Outro".to_string(), + ]; + let expected = vec![ + "Intro".to_string(), + "| A | B |".to_string(), + "| --- | --- |".to_string(), + "| 1 | 2 |".to_string(), + fence.to_string(), + "
x
".to_string(), + fence.to_string(), + "Outro".to_string(), + ]; + assert_eq!(convert_html_tables(&lines), expected); +} + +#[test] +fn test_convert_html_table_with_attrs_basic() { + let expected = vec!["| A | B |", "| --- | --- |", "| 1 | 2 |"]; + assert_eq!(convert_html_tables(&html_table_with_attrs()), expected); +} + +#[test] +fn test_convert_html_table_with_colspan() { + let expected = vec!["| A |", "| --- |", "| 1 | 2 |"]; + assert_eq!(convert_html_tables(&html_table_with_colspan()), expected); +} + +#[test] +fn test_convert_html_table_no_header() { + let expected = vec!["| A | B |", "| 1 | 2 |"]; + assert_eq!(convert_html_tables(&html_table_no_header()), expected); +} + +#[test] +fn test_convert_html_table_empty() { + assert!(convert_html_tables(&html_table_empty()).is_empty()); +} + +#[test] +fn test_convert_html_table_unclosed_returns_original() { + let html = html_table_unclosed(); + assert_eq!(convert_html_tables(&html), html); +}