Skip to content

Commit

Permalink
Fix emitting :: for centered column in table.
Browse files Browse the repository at this point in the history
Emitting `::` in a table to indicate alignment is `Center` seems to
break some parsers. Emitting `:-:` appears to work fine everywhere.

This commit also removes an allocation when encountering an empty header
cell. Rather than allocating a non-empty string, let the string be empty
and add a minimum width for emitting alignments.
  • Loading branch information
Ethiraric committed Jun 16, 2024
1 parent e6c8c3e commit 33811dc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
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

0 comments on commit 33811dc

Please sign in to comment.