Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
106 changes: 105 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -83,6 +83,36 @@ fn html_table_with_attrs() -> Vec<String> {
]
}

#[fixture]
fn html_table_with_colspan() -> Vec<String> {
vec![
"<table>".to_string(),
"<tr><th colspan=\"2\">A</th></tr>".to_string(),
"<tr><td>1</td><td>2</td></tr>".to_string(),
"</table>".to_string(),
]
}

#[fixture]
fn html_table_no_header() -> Vec<String> {
vec![
"<table>".to_string(),
"<tr><td>A</td><td>B</td></tr>".to_string(),
"<tr><td>1</td><td>2</td></tr>".to_string(),
"</table>".to_string(),
]
}

#[fixture]
fn html_table_empty() -> Vec<String> {
vec!["<table></table>".to_string()]
}

#[fixture]
fn html_table_unclosed() -> Vec<String> {
vec!["<table>".to_string(), "<tr><td>1</td></tr>".to_string()]
}

#[fixture]
fn html_table_uppercase() -> Vec<String> {
vec![
Expand Down Expand Up @@ -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![
"<table>".to_string(),
"<tr><th>A</th><th>B</th></tr>".to_string(),
"<tr><td>1</td><td>2</td></tr>".to_string(),
"</table>".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(),
"<table>".to_string(),
"<tr><th>A</th><th>B</th></tr>".to_string(),
"<tr><td>1</td><td>2</td></tr>".to_string(),
"</table>".to_string(),
fence.to_string(),
"<table><tr><td>x</td></tr></table>".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(),
"<table><tr><td>x</td></tr></table>".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);
}