diff --git a/src/Markdig/Extensions/Tables/GridTableParser.cs b/src/Markdig/Extensions/Tables/GridTableParser.cs
index 4dfb0d6d..f917a073 100644
--- a/src/Markdig/Extensions/Tables/GridTableParser.cs
+++ b/src/Markdig/Extensions/Tables/GridTableParser.cs
@@ -131,11 +131,12 @@ namespace Markdig.Extensions.Tables
// | ------------- | ------------ | ---------------------------------------- |
// Calculate the colspan for the new row
int columnIndex = -1;
- foreach (var columnSlice in columns)
+ for (int i = 0; i < columns.Count; i++)
{
+ var columnSlice = columns[i];
if (line.PeekCharExtra(columnSlice.Start) == '|')
{
- columnIndex++;
+ columnIndex = i;
}
if (columnIndex >= 0)
{
@@ -301,8 +302,9 @@ namespace Markdig.Extensions.Tables
{
var columns = tableState.ColumnSlices;
TableRow currentRow = null;
- foreach (var columnSlice in columns)
+ for (int i = 0; i < columns.Count; i++)
{
+ var columnSlice = columns[i];
if (columnSlice.CurrentCell != null)
{
if (currentRow == null)
@@ -332,7 +334,8 @@ namespace Markdig.Extensions.Tables
// Else we can create a new cell
columnSlice.CurrentCell = new TableCell(this)
{
- ColumnSpan = columnSlice.CurrentColumnSpan
+ ColumnSpan = columnSlice.CurrentColumnSpan,
+ ColumnIndex = i
};
if (columnSlice.BlockProcessor == null)
diff --git a/src/Markdig/Extensions/Tables/HtmlTableRenderer.cs b/src/Markdig/Extensions/Tables/HtmlTableRenderer.cs
index 34fce972..80328852 100644
--- a/src/Markdig/Extensions/Tables/HtmlTableRenderer.cs
+++ b/src/Markdig/Extensions/Tables/HtmlTableRenderer.cs
@@ -78,10 +78,10 @@ namespace Markdig.Extensions.Tables
{
renderer.Write($" colspan=\"{cell.ColumnSpan}\"");
}
-
- if (table.ColumnDefinitions != null && i < table.ColumnDefinitions.Count)
+ var columnIndex = cell.ColumnIndex == -1 ? i : cell.ColumnIndex;
+ if (table.ColumnDefinitions != null && columnIndex < table.ColumnDefinitions.Count)
{
- switch (table.ColumnDefinitions[i].Alignment)
+ switch (table.ColumnDefinitions[columnIndex].Alignment)
{
case TableColumnAlign.Center:
renderer.Write(" style=\"text-align: center;\"");
diff --git a/src/Markdig/Extensions/Tables/TableCell.cs b/src/Markdig/Extensions/Tables/TableCell.cs
index c55afaae..8fbc9b02 100644
--- a/src/Markdig/Extensions/Tables/TableCell.cs
+++ b/src/Markdig/Extensions/Tables/TableCell.cs
@@ -27,8 +27,14 @@ namespace Markdig.Extensions.Tables
public TableCell(BlockParser parser) : base(parser)
{
ColumnSpan = 1;
+ ColumnIndex = -1;
}
+ ///
+ /// Gets or sets the index of the column to which this cell belongs.
+ ///
+ public int ColumnIndex { get; set; }
+
///
/// Gets or sets the column span this cell is covering. Default is 1.
///