11// Copyright (c) Alexandre Mutel. All rights reserved.
2- // This file is licensed under the BSD-Clause 2 license.
2+ // This file is licensed under the BSD-Clause 2 license.
33// See the license.txt file in the project root for more information.
44
55using Markdig . Helpers ;
66using Markdig . Parsers ;
77using Markdig . Syntax ;
8+ using System . Linq ;
89
910namespace Markdig . Extensions . Tables ;
1011
@@ -60,7 +61,12 @@ public override BlockState TryOpen(BlockProcessor processor)
6061 }
6162 // Store the line (if we need later to build a ParagraphBlock because the GridTable was in fact invalid)
6263 tableState . AddLine ( ref processor . Line ) ;
63- var table = new Table ( this ) ;
64+ var table = new Table ( this )
65+ {
66+ Line = processor . LineIndex ,
67+ Column = processor . Column ,
68+ Span = { Start = lineStart }
69+ } ;
6470 table . SetData ( typeof ( GridTableState ) , tableState ) ;
6571
6672 // Calculate the total width of all columns
@@ -94,10 +100,12 @@ public override BlockState TryContinue(BlockProcessor processor, Block block)
94100 tableState . AddLine ( ref processor . Line ) ;
95101 if ( processor . CurrentChar == '+' )
96102 {
103+ gridTable . UpdateSpanEnd ( processor . Line . End ) ;
97104 return HandleNewRow ( processor , tableState , gridTable ) ;
98105 }
99106 if ( processor . CurrentChar == '|' )
100107 {
108+ gridTable . UpdateSpanEnd ( processor . Line . End ) ;
101109 return HandleContents ( processor , tableState , gridTable ) ;
102110 }
103111 TerminateCurrentRow ( processor , tableState , gridTable , true ) ;
@@ -182,8 +190,18 @@ private static void TerminateCurrentRow(BlockProcessor processor, GridTableState
182190 var columnSlice = columns [ i ] ;
183191 if ( columnSlice . CurrentCell != null )
184192 {
185- currentRow ??= new TableRow ( ) ;
186-
193+ if ( currentRow == null )
194+ {
195+ TableCell firstCell = columns . First ( c => c . CurrentCell != null ) . CurrentCell ! ;
196+ TableCell lastCell = columns . Last ( c => c . CurrentCell != null ) . CurrentCell ! ;
197+
198+ currentRow ??= new TableRow ( )
199+ {
200+ Span = new SourceSpan ( firstCell . Span . Start , lastCell . Span . End ) ,
201+ Line = firstCell . Line
202+ } ;
203+ }
204+
187205 // If this cell does not already belong to a row
188206 if ( columnSlice . CurrentCell . Parent is null )
189207 {
@@ -271,7 +289,10 @@ private BlockState HandleContents(BlockProcessor processor, GridTableState table
271289 columnSlice . CurrentCell = new TableCell ( this )
272290 {
273291 ColumnSpan = columnSlice . CurrentColumnSpan ,
274- ColumnIndex = i
292+ ColumnIndex = i ,
293+ Column = columnSlice . Start ,
294+ Line = processor . LineIndex ,
295+ Span = new SourceSpan ( line . Start + columnSlice . Start , line . Start + columnSlice . End )
275296 } ;
276297
277298 columnSlice . BlockProcessor ??= processor . CreateChild ( ) ;
@@ -281,7 +302,8 @@ private BlockState HandleContents(BlockProcessor processor, GridTableState table
281302 }
282303 // Process the content of the cell
283304 columnSlice . BlockProcessor ! . LineIndex = processor . LineIndex ;
284- columnSlice . BlockProcessor . ProcessLine ( sliceForCell ) ;
305+
306+ columnSlice . BlockProcessor . ProcessLinePart ( sliceForCell , sliceForCell . Start - line . Start ) ;
285307 }
286308
287309 // Go to next column
0 commit comments