From 68659f4037d1b2942818f7a9261cf02ecc38bfd3 Mon Sep 17 00:00:00 2001 From: Sergey Nozhenko Date: Wed, 18 Dec 2024 07:43:22 +0300 Subject: [PATCH 1/2] Include opening and closing pipes in the table span --- src/Markdig/Extensions/Tables/PipeTableParser.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Markdig/Extensions/Tables/PipeTableParser.cs b/src/Markdig/Extensions/Tables/PipeTableParser.cs index 87bd4249..71c14538 100644 --- a/src/Markdig/Extensions/Tables/PipeTableParser.cs +++ b/src/Markdig/Extensions/Tables/PipeTableParser.cs @@ -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; @@ -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; } } @@ -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 @@ -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) { From aff8a6823aa64af88620194e3ece2354349789c1 Mon Sep 17 00:00:00 2001 From: Sergey Nozhenko Date: Wed, 18 Dec 2024 10:21:29 +0300 Subject: [PATCH 2/2] A test has been added. --- src/Markdig.Tests/TestSourcePosition.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Markdig.Tests/TestSourcePosition.cs b/src/Markdig.Tests/TestSourcePosition.cs index 746694eb..2cfa61e1 100644 --- a/src/Markdig.Tests/TestSourcePosition.cs +++ b/src/Markdig.Tests/TestSourcePosition.cs @@ -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() {