Skip to content

Commit

Permalink
Table columns have min width of 3
Browse files Browse the repository at this point in the history
  • Loading branch information
driver-deploy-2 committed May 19, 2024
1 parent c1efd8f commit e7ccf4f
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 52 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ impl MarkdownDocument {
(is_header.clone(), row_content.clone())
})
.collect();
let column_lengths = max_lengths_per_column(&table_with_simple_cells);
let column_lengths = max_lengths_per_column(&table_with_simple_cells, 3);
let divider = &table_row_to_markdown(
&column_lengths,
&column_lengths.iter().map(|i| "-".repeat(*i)).collect(),
Expand Down Expand Up @@ -800,4 +800,30 @@ mod tests {
let markdown = markdown_doc.to_markdown(false);
assert_eq!(markdown_pandoc, markdown);
}

#[test]
fn test_one_row_table() {
let markdown_pandoc = fs::read_to_string("./test/table_one_row.md").unwrap();
let markdown_doc = MarkdownDocument::from_file("./test/table_one_row.docx");
let markdown = markdown_doc.to_markdown(false);
assert_eq!(markdown_pandoc, markdown);
}

#[test]
fn test_table_with_list_cell() {
let markdown_pandoc = fs::read_to_string("./test/table_with_list_cell.md").unwrap();
let markdown_doc = MarkdownDocument::from_file("./test/table_with_list_cell.docx");
let markdown = markdown_doc.to_markdown(false);
assert_eq!(markdown_pandoc, markdown);
}

#[test]
fn test_tables_separated_with_rawblock() {
let markdown_pandoc =
fs::read_to_string("./test/tables_separated_with_rawblock.md").unwrap();
let markdown_doc =
MarkdownDocument::from_file("./test/tables_separated_with_rawblock.docx");
let markdown = markdown_doc.to_markdown(false);
assert_eq!(markdown_pandoc, markdown);
}
}
7 changes: 5 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub fn max_lengths_per_column(table_with_simple_cells: &Vec<(bool, Vec<String>)>) -> Vec<usize> {
pub fn max_lengths_per_column(
table_with_simple_cells: &Vec<(bool, Vec<String>)>,
min_width: usize,
) -> Vec<usize> {
// Check if the table is empty
if table_with_simple_cells.is_empty() {
return vec![];
Expand All @@ -8,7 +11,7 @@ pub fn max_lengths_per_column(table_with_simple_cells: &Vec<(bool, Vec<String>)>
let num_columns = table_with_simple_cells[0].1.len();

// Initialize a vector to store the maximum length of each column
let mut max_lengths = vec![0; num_columns];
let mut max_lengths = vec![min_width; num_columns];

// Iterate over each row
for (_, row) in table_with_simple_cells {
Expand Down
6 changes: 3 additions & 3 deletions test/table_one_row.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
----- ----- -------
One Row Table
----- ----- -------
| | | |
| --- | --- | ----- |
| One | Row | Table |
12 changes: 3 additions & 9 deletions test/table_with_list_cell.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
+-----------------+--------------------+
| Cell with text | Cell with text |
+=================+====================+
| - Cell with | 1. Cell with |
| | |
| - A | 2. A |
| | |
| - Bullet list | 3. Numbered list. |
+-----------------+--------------------+
| Cell with text | Cell with text |
| ------------------------------------- | ------------------------------------------- |
| - Cell with<br/>- A<br/>- Bullet list | 1. Cell with<br/>2. A<br/>3. Numbered list. |
Binary file removed test/tables-default-widths.docx
Binary file not shown.
22 changes: 0 additions & 22 deletions test/tables-default-widths.md

This file was deleted.

12 changes: 6 additions & 6 deletions test/tables_separated_with_rawblock.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- ---
a b
--- ---
| | |
| --- | --- |
| a | b |

--- ---
c d
--- ---
| | |
| --- | --- |
| c | d |

0 comments on commit e7ccf4f

Please sign in to comment.