Skip to content

Include opening and closing pipes in the table span #835

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

Merged
merged 2 commits into from
Dec 18, 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
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
Loading