Skip to content

Commit

Permalink
Merge pull request #835 from snnz/fix-pipetable-span
Browse files Browse the repository at this point in the history
Include opening and closing pipes in the table span
  • Loading branch information
xoofx authored Dec 18, 2024
2 parents b8a3c27 + aff8a68 commit a8de208
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Markdig.Tests/TestSourcePosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,29 @@ public void TestPipeTable2()
", "pipetables");
}

[Test]
public void TestPipeTable3()
{
// 01234 5678 9ABCD
Check("|a|b\n-|-\n0|1|\n", @"
table ( 0, 0) 0-12
tablerow ( 0, 1) 1-3
tablecell ( 0, 1) 1-1
paragraph ( 0, 1) 1-1
literal ( 0, 1) 1-1
tablecell ( 0, 3) 3-3
paragraph ( 0, 3) 3-3
literal ( 0, 3) 3-3
tablerow ( 2, 0) 9-11
tablecell ( 2, 0) 9-9
paragraph ( 2, 0) 9-9
literal ( 2, 0) 9-9
tablecell ( 2, 2) 11-11
paragraph ( 2, 2) 11-11
literal ( 2, 2) 11-11
", "pipetables");
}

[Test]
public void TestIndentedCode()
{
Expand Down
14 changes: 14 additions & 0 deletions src/Markdig/Extensions/Tables/PipeTableParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ public bool PostProcess(InlineProcessor state, Inline? root, Inline? lastChild,
tableState.EndOfLines.Add(endOfTable);
}

int lastPipePos = 0;

// Cell loop
// Reconstruct the table from the delimiters
TableRow? row = null;
Expand All @@ -302,6 +304,12 @@ public bool PostProcess(InlineProcessor state, Inline? root, Inline? lastChild,
if (pipeSeparator != null && (delimiter.PreviousSibling is null || delimiter.PreviousSibling is LineBreakInline))
{
delimiter.Remove();
if (table.Span.IsEmpty)
{
table.Span = delimiter.Span;
table.Line = delimiter.Line;
table.Column = delimiter.Column;
}
continue;
}
}
Expand Down Expand Up @@ -354,6 +362,7 @@ public bool PostProcess(InlineProcessor state, Inline? root, Inline? lastChild,
// If the delimiter is a pipe, we need to remove it from the tree
// so that previous loop looking for a parent will not go further on subsequent cells
delimiter.Remove();
lastPipePos = delimiter.Span.End;
}

// We trim whitespace at the beginning and ending of the cell
Expand Down Expand Up @@ -421,6 +430,11 @@ public bool PostProcess(InlineProcessor state, Inline? root, Inline? lastChild,
}
}

if (lastPipePos > table.Span.End)
{
table.UpdateSpanEnd(lastPipePos);
}

// Once we are done with the cells, we can remove all end of lines in the table tree
foreach (var endOfLine in tableState.EndOfLines)
{
Expand Down

0 comments on commit a8de208

Please sign in to comment.