Skip to content

Commit

Permalink
Fix tables with leading/trailing header pipes and trailing spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
bplump committed Sep 5, 2021
1 parent 726d369 commit d1ac104
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,21 @@ private TableCell parseCell(SourceLine cell, int column, InlineParser inlinePars
private static List<SourceLine> split(SourceLine line) {
CharSequence row = line.getContent();
int nonSpace = Parsing.skipSpaceTab(row, 0, row.length());
int cellStart = row.charAt(nonSpace) == '|' ? nonSpace + 1 : nonSpace;
int cellStart = nonSpace;
int cellEnd = row.length();
if (row.charAt(nonSpace) == '|') {
// This row has leading/trailing pipes - adjust cellStart/End to ignore them
cellStart = nonSpace + 1;
int nonSpaceEnd = Parsing.skipSpaceTabBackwards(row, row.length() - 1, cellStart + 1);
cellEnd = row.charAt(nonSpaceEnd) == '|' ? nonSpaceEnd : nonSpaceEnd + 1;
}
List<SourceLine> cells = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = cellStart; i < row.length(); i++) {
for (int i = cellStart; i < cellEnd; i++) {
char c = row.charAt(i);
switch (c) {
case '\\':
if (i + 1 < row.length() && row.charAt(i + 1) == '|') {
if (i + 1 < cellEnd && row.charAt(i + 1) == '|') {
// Pipe is special for table parsing. An escaped pipe doesn't result in a new cell, but is
// passed down to inline parsing as an unescaped pipe. Note that that applies even for the `\|`
// in an input like `\\|` - in other words, table parsing doesn't support escaping backslashes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,24 @@ public void pipesOnOutside() {
"</table>\n");
}

@Test
public void pipesOnOutsideWhitespaceAfterHeader() {
assertRendering("|Abc|Def| \n|---|---|\n|1|2|", "<table>\n" +
"<thead>\n" +
"<tr>\n" +
"<th>Abc</th>\n" +
"<th>Def</th>\n" +
"</tr>\n" +
"</thead>\n" +
"<tbody>\n" +
"<tr>\n" +
"<td>1</td>\n" +
"<td>2</td>\n" +
"</tr>\n" +
"</tbody>\n" +
"</table>\n");
}

@Test
public void inlineElements() {
assertRendering("*Abc*|Def\n---|---\n1|2", "<table>\n" +
Expand Down

0 comments on commit d1ac104

Please sign in to comment.