Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix emitting :: for centered column in table. #76

Merged
merged 1 commit into from
Jun 16, 2024
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
25 changes: 16 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,13 +530,9 @@ where
Ok(())
}
TagEnd::TableCell => {
state.table_headers.push(
state
.text_for_header
.take()
.filter(|s| !s.is_empty())
.unwrap_or_else(|| " ".into()),
);
state
.table_headers
.push(state.text_for_header.take().unwrap_or_default());
Ok(())
}
ref t @ TagEnd::TableRow | ref t @ TagEnd::TableHead => {
Expand All @@ -551,8 +547,19 @@ where
formatter.write_char('|')?;
// NOTE: For perfect counting, count grapheme clusters.
// The reason this is not done is to avoid the dependency.
let last_minus_one = name.chars().count().saturating_sub(1);
for c in 0..name.len() {

// The minimum width of the column so that we can represent its alignment.
let min_width = match alignment {
// Must at least represent `-`.
Alignment::None => 1,
// Must at least represent `:-` or `-:`
Alignment::Left | Alignment::Right => 2,
// Must at least represent `:-:`
Alignment::Center => 3,
};
let length = name.chars().count().max(min_width);
let last_minus_one = length.saturating_sub(1);
for c in 0..length {
formatter.write_char(
if (c == 0 && (alignment == &Alignment::Center || alignment == &Alignment::Left))
|| (c == last_minus_one
Expand Down
37 changes: 36 additions & 1 deletion tests/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,42 @@ mod table {
indoc!(
"
|Tables|Are|Cool|yo||
|------|:-:|---:|:-|--|
|------|:-:|---:|:-|-|
|col 3 is|right-aligned|$1600|x|01|
|col 2 is|centered|$12|y|02|
|zebra stripes|are neat|$1|z|03|"
)
);

let p = Parser::new_ext(&generated_markdown, Options::all());
let generated_events: Vec<_> = p.into_iter().collect();

assert_eq!(original_events, generated_events);
}

#[test]
fn it_generates_equivalent_table_markdown_with_empty_headers() {
use pulldown_cmark::{Options, Parser};

let original_table_markdown = indoc!(
"
||||||
|:-------------:|:--------------|------:|:--:|:-:|
| col 3 is | right-aligned | $1600 | x |01|
| col 2 is | centered | $12 | y |02|
| zebra stripes | are neat | $1 | z |03|"
);
let p = Parser::new_ext(original_table_markdown, Options::all());
let original_events: Vec<_> = p.into_iter().collect();

let (generated_markdown, _) = fmte(&original_events);

assert_eq!(
generated_markdown,
indoc!(
"
||||||
|:-:|:-|-:|:-:|:-:|
|col 3 is|right-aligned|$1600|x|01|
|col 2 is|centered|$12|y|02|
|zebra stripes|are neat|$1|z|03|"
Expand Down
Loading