From 2b9fbfb252d7fed0c4862385821baf408ecc39a1 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Sat, 3 Oct 2026 02:46:01 +0200 Subject: [PATCH] Fix grid table open last cell taking a `|` from the next line (#494) To find a closing `|` at the end of the last column, GridTableParser used PeekCharExtra, which ignores the end of the slice. When the line was shorter than the table, it could read the next line instead, and a `|` there made the cell run past the end of the line, so the next line's leading `|` ended up in the cell. Use PeekChar, which stays within the current line. --- src/Markdig.Tests/MiscTests.cs | 25 +++++++++++++++++++ .../Extensions/Tables/GridTableParser.cs | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Markdig.Tests/MiscTests.cs b/src/Markdig.Tests/MiscTests.cs index ca5ad4c..1dcc9f0 100644 --- a/src/Markdig.Tests/MiscTests.cs +++ b/src/Markdig.Tests/MiscTests.cs @@ -346,6 +346,31 @@ $$ TestParser.TestSpec(input, expected, new MarkdownPipelineBuilder().UseGridTables().Build()); } + [Test] + public void TestGridTableLastCellWithoutClosingPipe() + { + // The last cell is open and shorter than the column, and the next line has + // a `|` where this one would end: https://github.com/xoofx/markdig/issues/494 + var input = @" ++-----+----------+ +| 6 | 1 +| | ++-----+----------+"; + + var expected = @" +++ + + + + + +
61
+"; + TestParser.TestSpec(input, expected, new MarkdownPipelineBuilder().UseGridTables().Build()); + } + [Test] public void TestDefinitionListInListItemWithBlankLine() { diff --git a/src/Markdig/Extensions/Tables/GridTableParser.cs b/src/Markdig/Extensions/Tables/GridTableParser.cs index 35de6f1..20aec86 100644 --- a/src/Markdig/Extensions/Tables/GridTableParser.cs +++ b/src/Markdig/Extensions/Tables/GridTableParser.cs @@ -279,7 +279,7 @@ public class GridTableParser : BlockParser else { var columnEnd = columns[columns.Count - 1].End; - var columnEndChar = line.PeekCharExtra(columnEnd); + var columnEndChar = line.PeekChar(columnEnd); // If there is a `|` (or a `+` in the case that we are dealing with a row line // with spanned contents) exactly at the expected end of the table row, we cut the line // otherwise we allow to have the last cell of a row to be open for longer cell content