diff --git a/src/Markdig.Tests/Specs/PipeTableSpecs.md b/src/Markdig.Tests/Specs/PipeTableSpecs.md
index 39c6eaac..acddfbb4 100644
--- a/src/Markdig.Tests/Specs/PipeTableSpecs.md
+++ b/src/Markdig.Tests/Specs/PipeTableSpecs.md
@@ -136,6 +136,7 @@ a | b
| a |
b |
+ |
@@ -147,9 +148,12 @@ a | b
| 3 |
4 |
+ |
| 5 |
+ |
+ |
@@ -548,3 +552,31 @@ Tests trailing spaces after pipes
````````````````````````````````
+
+** Normalized columns count **
+
+The tables are normalized to the maximum number of columns found in a table
+
+
+```````````````````````````````` example
+a | b
+-- | -
+0 | 1 | 2
+.
+
+
+
+| a |
+b |
+ |
+
+
+
+
+| 0 |
+1 |
+2 |
+
+
+
+````````````````````````````````
\ No newline at end of file
diff --git a/src/Markdig.Tests/Specs/Specs.cs b/src/Markdig.Tests/Specs/Specs.cs
index 1deba4d3..826255a5 100644
--- a/src/Markdig.Tests/Specs/Specs.cs
+++ b/src/Markdig.Tests/Specs/Specs.cs
@@ -16633,6 +16633,7 @@ namespace Markdig.Tests
//
// | a |
// b |
+ // |
//
//
//
@@ -16644,15 +16645,18 @@ namespace Markdig.Tests
//
// | 3 |
// 4 |
+ // |
//
//
// | 5 |
+ // |
+ // |
//
//
//
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 7, "Extensions Pipe Table");
- TestParser.TestSpec("a | b \n-- | --\n0 | 1 | 2\n3 | 4\n5 |", "\n\n\n| a | \nb | \n
\n\n\n\n| 0 | \n1 | \n2 | \n
\n\n| 3 | \n4 | \n
\n\n| 5 | \n
\n\n
", "pipetables|advanced");
+ TestParser.TestSpec("a | b \n-- | --\n0 | 1 | 2\n3 | 4\n5 |", "\n\n\n| a | \nb | \n | \n
\n\n\n\n| 0 | \n1 | \n2 | \n
\n\n| 3 | \n4 | \n | \n
\n\n| 5 | \n | \n | \n
\n\n
", "pipetables|advanced");
}
}
// **Rule #2**
@@ -17227,6 +17231,45 @@ namespace Markdig.Tests
Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 22, "Extensions Pipe Table");
TestParser.TestSpec("| abc | def | \n|---|---|\n| cde| ddd| \n| eee| fff|\n| fff | fffff | \n|gggg | ffff | ", "\n\n\n| abc | \ndef | \n
\n\n\n\n| cde | \nddd | \n
\n\n| eee | \nfff | \n
\n\n| fff | \nfffff | \n
\n\n| gggg | \nffff | \n
\n\n
", "pipetables|advanced");
}
+ }
+ // ** Normalized columns count **
+ //
+ // The tables are normalized to the maximum number of columns found in a table
+ [TestFixture]
+ public partial class TestExtensionsPipeTable
+ {
+ [Test]
+ public void Example023()
+ {
+ // Example 23
+ // Section: Extensions Pipe Table
+ //
+ // The following CommonMark:
+ // a | b
+ // -- | -
+ // 0 | 1 | 2
+ //
+ // Should be rendered as:
+ //
+ //
+ //
+ // | a |
+ // b |
+ // |
+ //
+ //
+ //
+ //
+ // | 0 |
+ // 1 |
+ // 2 |
+ //
+ //
+ //
+
+ Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 23, "Extensions Pipe Table");
+ TestParser.TestSpec("a | b\n-- | - \n0 | 1 | 2", "\n\n\n| a | \nb | \n | \n
\n\n\n\n| 0 | \n1 | \n2 | \n
\n\n
", "pipetables|advanced");
+ }
}
// # Extensions
//
diff --git a/src/Markdig/Extensions/Tables/PipeTableParser.cs b/src/Markdig/Extensions/Tables/PipeTableParser.cs
index 38307f51..9397e845 100644
--- a/src/Markdig/Extensions/Tables/PipeTableParser.cs
+++ b/src/Markdig/Extensions/Tables/PipeTableParser.cs
@@ -458,6 +458,9 @@ namespace Markdig.Extensions.Tables
// Clear cells when we are done
cells.Clear();
+ // Normalize the table
+ table.Normalize();
+
// We don't want to continue procesing delimiters, as we are already processing them here
return false;
}
diff --git a/src/Markdig/Extensions/Tables/Table.cs b/src/Markdig/Extensions/Tables/Table.cs
index 25db3733..121ba8ab 100644
--- a/src/Markdig/Extensions/Tables/Table.cs
+++ b/src/Markdig/Extensions/Tables/Table.cs
@@ -69,5 +69,33 @@ namespace Markdig.Extensions.Tables
}
return true;
}
+
+ ///
+ /// Normalizes the number of columns of this table by taking the maximum columns and appending empty cells.
+ ///
+ public void Normalize()
+ {
+ var maxColumn = 0;
+ for (int i = 0; i < this.Count; i++)
+ {
+ var row = this[i] as TableRow;
+ if (row != null && row.Count > maxColumn)
+ {
+ maxColumn = row.Count;
+ }
+ }
+
+ for (int i = 0; i < this.Count; i++)
+ {
+ var row = this[i] as TableRow;
+ if (row != null)
+ {
+ for (int j = row.Count; j < maxColumn; j++)
+ {
+ row.Add(new TableCell());
+ }
+ }
+ }
+ }
}
}
\ No newline at end of file