From 80c50e31e25e92d2999e69f027f4b5080a8f7b75 Mon Sep 17 00:00:00 2001 From: Tibor Peluch Date: Fri, 11 Jul 2025 13:25:03 +0200 Subject: [PATCH] Attempt to fix tracking of tree node positions (line, column) inside GridTable --- .../Extensions/Tables/GridTableParser.cs | 34 +++++++++++++++---- src/Markdig/Parsers/BlockProcessor.cs | 10 ++++-- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/Markdig/Extensions/Tables/GridTableParser.cs b/src/Markdig/Extensions/Tables/GridTableParser.cs index 847bd94c..aff92bd5 100644 --- a/src/Markdig/Extensions/Tables/GridTableParser.cs +++ b/src/Markdig/Extensions/Tables/GridTableParser.cs @@ -1,10 +1,11 @@ // Copyright (c) Alexandre Mutel. All rights reserved. -// This file is licensed under the BSD-Clause 2 license. +// This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. using Markdig.Helpers; using Markdig.Parsers; using Markdig.Syntax; +using System.Linq; namespace Markdig.Extensions.Tables; @@ -60,7 +61,12 @@ public class GridTableParser : BlockParser } // Store the line (if we need later to build a ParagraphBlock because the GridTable was in fact invalid) tableState.AddLine(ref processor.Line); - var table = new Table(this); + var table = new Table(this) + { + Line = processor.LineIndex, + Column = processor.Column, + Span = { Start = lineStart } + }; table.SetData(typeof(GridTableState), tableState); // Calculate the total width of all columns @@ -94,10 +100,12 @@ public class GridTableParser : BlockParser tableState.AddLine(ref processor.Line); if (processor.CurrentChar == '+') { + gridTable.UpdateSpanEnd(processor.Line.End); return HandleNewRow(processor, tableState, gridTable); } if (processor.CurrentChar == '|') { + gridTable.UpdateSpanEnd(processor.Line.End); return HandleContents(processor, tableState, gridTable); } TerminateCurrentRow(processor, tableState, gridTable, true); @@ -182,8 +190,18 @@ public class GridTableParser : BlockParser var columnSlice = columns[i]; if (columnSlice.CurrentCell != null) { - currentRow ??= new TableRow(); - + if (currentRow == null) + { + TableCell firstCell = columns.First(c => c.CurrentCell != null).CurrentCell!; + TableCell lastCell = columns.Last(c => c.CurrentCell != null).CurrentCell!; + + currentRow ??= new TableRow() + { + Span = new SourceSpan(firstCell.Span.Start, lastCell.Span.End), + Line = firstCell.Line + }; + } + // If this cell does not already belong to a row if (columnSlice.CurrentCell.Parent is null) { @@ -271,7 +289,10 @@ public class GridTableParser : BlockParser columnSlice.CurrentCell = new TableCell(this) { ColumnSpan = columnSlice.CurrentColumnSpan, - ColumnIndex = i + ColumnIndex = i, + Column = columnSlice.Start, + Line = processor.LineIndex, + Span = new SourceSpan(line.Start + columnSlice.Start, line.Start + columnSlice.End) }; columnSlice.BlockProcessor ??= processor.CreateChild(); @@ -281,7 +302,8 @@ public class GridTableParser : BlockParser } // Process the content of the cell columnSlice.BlockProcessor!.LineIndex = processor.LineIndex; - columnSlice.BlockProcessor.ProcessLine(sliceForCell); + + columnSlice.BlockProcessor.ProcessLine(sliceForCell, sliceForCell.Start - line.Start); } // Go to next column diff --git a/src/Markdig/Parsers/BlockProcessor.cs b/src/Markdig/Parsers/BlockProcessor.cs index 225ed971..1019bbc2 100644 --- a/src/Markdig/Parsers/BlockProcessor.cs +++ b/src/Markdig/Parsers/BlockProcessor.cs @@ -485,15 +485,21 @@ public class BlockProcessor /// Processes a new line. /// /// The new line. - public void ProcessLine(StringSlice newLine) + /// The offset. + public void ProcessLine(StringSlice newLine, int column = 0) { CurrentLineStartPosition = newLine.Start; - Document.LineStartIndexes?.Add(CurrentLineStartPosition); + if (column == 0) + { + Document.LineStartIndexes?.Add(CurrentLineStartPosition); + } ContinueProcessingLine = true; ResetLine(newLine); + Column = column; + originalLineStart -= column; TryContinueBlocks();