Skip to content

Table column alignments are not properly applied to headings #145

@smaeul

Description

@smaeul

When rendering to HTML, the align attribute is put on the wrong th tag in some tables.

Minimal reproducer:

|  Title A  |  Title B  |  Title C  |
| --------- | --------- | --------- |
| Content   | Content   | Content   |

|  Title A  |  Title B  |  Title C  |  Title D  |
| --------- | --------- | --------- | ---------:|
| Content   | Content   | Conent    | Content   |

|  Title A  |  Title B  |
| --------- | --------- |
| Content   | Content   |

|  Title A  |  Title B  |  Title C  |  Title D  |
| --------- | --------- | --------- | ---------:|
| Content   | Content   | Conent    | Content   |

Produces the output:

<table><thead><tr><th>  Title A  </th><th>  Title B  </th><th>  Title C  </th></tr></thead><tbody>
<tr><td> Content   </td><td> Content   </td><td> Content   </td></tr>
</tbody></table>
<table><thead><tr><th align="right">  Title A  </th><th>  Title B  </th><th>  Title C  </th><th>  Title D  </th></tr></thead><tbody>
<tr><td> Content   </td><td> Content   </td><td> Conent    </td><td align="right"> Content   </td></tr>
</tbody></table>
<table><thead><tr><th>  Title A  </th><th>  Title B  </th></tr></thead><tbody>
<tr><td> Content   </td><td> Content   </td></tr>
</tbody></table>
<table><thead><tr><th>  Title A  </th><th align="right">  Title B  </th><th>  Title C  </th><th>  Title D  </th></tr></thead><tbody>
<tr><td> Content   </td><td> Content   </td><td> Conent    </td><td align="right"> Content   </td></tr>
</tbody></table>

Note that the first or second column is right-aligned, when the fourth column should be.

Proposed patch that fixes the immediate issue:

@@ -94,6 +94,7 @@ impl<'a, 'b, I: Iterator<Item=Event<'a>>> Ctx<'b, I> {
                 self.buf.push_str("<table>");
             }
             Tag::TableHead => {
+                self.table_cell_index = 0;
                 self.table_state = TableState::Head;
                 self.buf.push_str("<thead><tr>");
             }

If headings should not follow the column alignment (to match GitHub-flavored-markdown behavior):

@@ -104,13 +105,15 @@ impl<'a, 'b, I: Iterator<Item=Event<'a>>> Ctx<'b, I> {
             Tag::TableCell => {
                 match self.table_state {
                     TableState::Head => self.buf.push_str("<th"),
-                    TableState::Body => self.buf.push_str("<td"),
-                }
-                match self.table_alignments.get(self.table_cell_index) {
-                    Some(&Alignment::Left) => self.buf.push_str(" align=\"left\""),
-                    Some(&Alignment::Center) => self.buf.push_str(" align=\"center\""),
-                    Some(&Alignment::Right) => self.buf.push_str(" align=\"right\""),
-                    _ => (),
+                    TableState::Body => {
+                        self.buf.push_str("<td");
+                        match self.table_alignments.get(self.table_cell_index) {
+                            Some(&Alignment::Left) => self.buf.push_str(" align=\"left\""),
+                            Some(&Alignment::Center) => self.buf.push_str(" align=\"center\""),
+                            Some(&Alignment::Right) => self.buf.push_str(" align=\"right\""),
+                            _ => (),
+                        }
+                    }
                 }
                 self.buf.push_str(">");
             }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions