diff --git a/src/Markdig.Tests/Specs/PipeTableSpecs.md b/src/Markdig.Tests/Specs/PipeTableSpecs.md index 5f239128..3a452006 100644 --- a/src/Markdig.Tests/Specs/PipeTableSpecs.md +++ b/src/Markdig.Tests/Specs/PipeTableSpecs.md @@ -214,6 +214,76 @@ Column delimiters `|` at the very beginning of a line or just before a line endi ```````````````````````````````` + +A pipe may be present at both the beginning/ending of each line: + +```````````````````````````````` example +|a|b| +|-|-| +|0|1| +. + + + + + + + + + + + + + +
ab
01
+```````````````````````````````` + +Or may be ommitted on one side: + +```````````````````````````````` example +a|b| +-|-| +0|1| +. + + + + + + + + + + + + + +
ab
01
+```````````````````````````````` + +```````````````````````````````` example +|a|b +|-|- +|0|1 +. + + + + + + + + + + + + + +
ab
01
+```````````````````````````````` + + + Single column table can be declared with lines starting only by a column delimiter: ```````````````````````````````` example diff --git a/src/Markdig.Tests/Specs/Specs.cs b/src/Markdig.Tests/Specs/Specs.cs index 931fd2dd..b7fd0c9c 100644 --- a/src/Markdig.Tests/Specs/Specs.cs +++ b/src/Markdig.Tests/Specs/Specs.cs @@ -16631,7 +16631,7 @@ namespace Markdig.Tests TestParser.TestSpec(" a | b |\n-- | --\n| 0 | 1\n| 2 | 3 |\n 4 | 5 ", "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
01
23
45
", "pipetables"); } } - // Single column table can be declared with lines starting only by a column delimiter: + // A pipe may be present at both the beginning/ending of each line: [TestFixture] public partial class TestExtensionsPipeTable { @@ -16642,6 +16642,110 @@ namespace Markdig.Tests // Section: Extensions Pipe Table // // The following CommonMark: + // |a|b| + // |-|-| + // |0|1| + // + // Should be rendered as: + // + // + // + // + // + // + // + // + // + // + // + // + // + //
ab
01
+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 10, "Extensions Pipe Table"); + TestParser.TestSpec("|a|b|\n|-|-|\n|0|1|", "\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
01
", "pipetables"); + } + } + // Or may be ommitted on one side: + [TestFixture] + public partial class TestExtensionsPipeTable + { + [Test] + public void Example011() + { + // Example 11 + // Section: Extensions Pipe Table + // + // The following CommonMark: + // a|b| + // -|-| + // 0|1| + // + // Should be rendered as: + // + // + // + // + // + // + // + // + // + // + // + // + // + //
ab
01
+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 11, "Extensions Pipe Table"); + TestParser.TestSpec("a|b|\n-|-|\n0|1|", "\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
01
", "pipetables"); + } + } + [TestFixture] + public partial class TestExtensionsPipeTable + { + [Test] + public void Example012() + { + // Example 12 + // Section: Extensions Pipe Table + // + // The following CommonMark: + // |a|b + // |-|- + // |0|1 + // + // Should be rendered as: + // + // + // + // + // + // + // + // + // + // + // + // + // + //
ab
01
+ + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 12, "Extensions Pipe Table"); + TestParser.TestSpec("|a|b\n|-|-\n|0|1", "\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
01
", "pipetables"); + } + } + // Single column table can be declared with lines starting only by a column delimiter: + [TestFixture] + public partial class TestExtensionsPipeTable + { + [Test] + public void Example013() + { + // Example 13 + // Section: Extensions Pipe Table + // + // The following CommonMark: // | a // | -- // | b @@ -16664,7 +16768,7 @@ namespace Markdig.Tests // // - Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 10, "Extensions Pipe Table"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 13, "Extensions Pipe Table"); TestParser.TestSpec("| a\n| --\n| b\n| c ", "\n\n\n\n\n\n\n\n\n\n\n\n\n\n
a
b
c
", "pipetables"); } } @@ -16681,9 +16785,9 @@ namespace Markdig.Tests public partial class TestExtensionsPipeTable { [Test] - public void Example011() + public void Example014() { - // Example 11 + // Example 14 // Section: Extensions Pipe Table // // The following CommonMark: @@ -16712,7 +16816,7 @@ namespace Markdig.Tests // // - Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 11, "Extensions Pipe Table"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 14, "Extensions Pipe Table"); TestParser.TestSpec(" a | b \n-------|-------\n 0 | 1 \n 2 | 3 ", "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
01
23
", "pipetables"); } } @@ -16722,9 +16826,9 @@ namespace Markdig.Tests public partial class TestExtensionsPipeTable { [Test] - public void Example012() + public void Example015() { - // Example 12 + // Example 15 // Section: Extensions Pipe Table // // The following CommonMark: @@ -16756,7 +16860,7 @@ namespace Markdig.Tests // // - Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 12, "Extensions Pipe Table"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 15, "Extensions Pipe Table"); TestParser.TestSpec(" a | b | c \n:------|:-------:| ----:\n 0 | 1 | 2 \n 3 | 4 | 5 ", "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
abc
012
345
", "pipetables"); } } @@ -16765,9 +16869,9 @@ namespace Markdig.Tests public partial class TestExtensionsPipeTable { [Test] - public void Example013() + public void Example016() { - // Example 13 + // Example 16 // Section: Extensions Pipe Table // // The following CommonMark: @@ -16782,7 +16886,7 @@ namespace Markdig.Tests // 0 | 1 // 2 | 3

- Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 13, "Extensions Pipe Table"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 16, "Extensions Pipe Table"); TestParser.TestSpec(" a | b\n-------|---x---\n 0 | 1\n 2 | 3 ", "

a | b\n-------|---x---\n0 | 1\n2 | 3

", "pipetables"); } } @@ -16793,9 +16897,9 @@ namespace Markdig.Tests public partial class TestExtensionsPipeTable { [Test] - public void Example014() + public void Example017() { - // Example 14 + // Example 17 // Section: Extensions Pipe Table // // The following CommonMark: @@ -16824,7 +16928,7 @@ namespace Markdig.Tests // // - Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 14, "Extensions Pipe Table"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 17, "Extensions Pipe Table"); TestParser.TestSpec(" *a* | b\n----- |-----\n 0 | _1_\n _2 | 3* ", "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
01
_23*
", "pipetables"); } } @@ -16835,9 +16939,9 @@ namespace Markdig.Tests public partial class TestExtensionsPipeTable { [Test] - public void Example015() + public void Example018() { - // Example 15 + // Example 18 // Section: Extensions Pipe Table // // The following CommonMark: @@ -16847,7 +16951,7 @@ namespace Markdig.Tests // Should be rendered as: //

a | b 0 |

- Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 15, "Extensions Pipe Table"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 18, "Extensions Pipe Table"); TestParser.TestSpec("a | b `\n0 | ` ", "

a | b 0 |

", "pipetables"); } } @@ -16858,9 +16962,9 @@ namespace Markdig.Tests public partial class TestExtensionsPipeTable { [Test] - public void Example016() + public void Example019() { - // Example 16 + // Example 19 // Section: Extensions Pipe Table // // The following CommonMark: @@ -16884,7 +16988,7 @@ namespace Markdig.Tests // // - Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 16, "Extensions Pipe Table"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 19, "Extensions Pipe Table"); TestParser.TestSpec("a | b\n-- | --\n0 | 1", "\n\n\n\n\n\n\n\n\n\n\n\n\n
a b
01
", "pipetables"); } } @@ -16895,9 +16999,9 @@ namespace Markdig.Tests public partial class TestExtensionsPipeTable { [Test] - public void Example017() + public void Example020() { - // Example 17 + // Example 20 // Section: Extensions Pipe Table // // The following CommonMark: @@ -16921,7 +17025,7 @@ namespace Markdig.Tests // // - Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 17, "Extensions Pipe Table"); + Console.WriteLine("Example {0}" + Environment.NewLine + "Section: {0}" + Environment.NewLine, 20, "Extensions Pipe Table"); TestParser.TestSpec("a | b\n-- | --\n[This is a link with a | inside the label](http://google.com) | 1", "\n\n\n\n\n\n\n\n\n\n\n\n\n
ab
This is a link with a | inside the label1
", "pipetables"); } } diff --git a/src/Markdig/Extensions/Tables/PipeTableExtension.cs b/src/Markdig/Extensions/Tables/PipeTableExtension.cs index e29cc8df..4aaeba29 100644 --- a/src/Markdig/Extensions/Tables/PipeTableExtension.cs +++ b/src/Markdig/Extensions/Tables/PipeTableExtension.cs @@ -28,6 +28,8 @@ namespace Markdig.Extensions.Tables public void Setup(MarkdownPipelineBuilder pipeline) { + // Pipe tables require precise source location + pipeline.PreciseSourceLocation = true; if (!pipeline.BlockParsers.Contains()) { pipeline.BlockParsers.Insert(0, new PipeTableBlockParser()); diff --git a/src/Markdig/Extensions/Tables/PipeTableParser.cs b/src/Markdig/Extensions/Tables/PipeTableParser.cs index acbdfa25..25836c78 100644 --- a/src/Markdig/Extensions/Tables/PipeTableParser.cs +++ b/src/Markdig/Extensions/Tables/PipeTableParser.cs @@ -444,8 +444,8 @@ namespace Markdig.Extensions.Tables } // Check the left side of a `|` delimiter - TableColumnAlign align; - if (!ParseHeaderString(delimiter.PreviousSibling, out align)) + TableColumnAlign align = TableColumnAlign.Left; + if (delimiter.PreviousSibling != null && !ParseHeaderString(delimiter.PreviousSibling, out align)) { break; }