fix CodeInline parsing & rendering

This commit is contained in:
Ruud Poutsma
2020-10-04 21:49:54 +02:00
parent 111d3d3362
commit 0b79389b14
7 changed files with 46 additions and 22 deletions

View File

@@ -11,26 +11,28 @@ namespace Markdig.Tests.RoundtripSpecs.Inlines
[TestCase("`` ")]
[TestCase(" `` ")]
[TestCase("`a`")]
[TestCase(" `a`")]
[TestCase("`a` ")]
[TestCase(" `a` ")]
[TestCase("`c`")]
[TestCase(" `c`")]
[TestCase("`c` ")]
[TestCase(" `c` ")]
[TestCase("` a`")]
[TestCase(" ` a`")]
[TestCase("` a` ")]
[TestCase(" ` a` ")]
[TestCase("` c`")]
[TestCase(" ` c`")]
[TestCase("` c` ")]
[TestCase(" ` c` ")]
[TestCase("`a `")]
[TestCase(" `a `")]
[TestCase("`a ` ")]
[TestCase(" `a ` ")]
[TestCase("``a``")]
[TestCase("- ```a```")]
[TestCase("p ```a``` p")]
[TestCase("`c `")]
[TestCase(" `c `")]
[TestCase("`c ` ")]
[TestCase(" `c ` ")]
[TestCase("``c``")]
[TestCase("```c```")]
[TestCase("````c````")]
[TestCase("p `a` p")]
[TestCase("p ``a`` p")]
[TestCase("p ```a``` p")]
// broken
//[TestCase("```a```")]
@@ -41,10 +43,15 @@ namespace Markdig.Tests.RoundtripSpecs.Inlines
/// <see cref="CodeInlineParser"/>: intentionally trimmed. TODO: decide on how to handle
//[TestCase("` a `")]
//[TestCase(" ` a `")]
//[TestCase("` a ` ")]
//[TestCase(" ` a ` ")]
public void Test_CodeInline(string value)
[TestCase(" ` a `")]
[TestCase("` a ` ")]
[TestCase(" ` a ` ")]
public void Test(string value)
{
RoundTrip(value);
}
public void TestParagraph(string value)
{
RoundTrip(value);
}

View File

@@ -69,6 +69,7 @@ namespace Markdig.Tests.RoundtripSpecs
RoundTrip(value);
}
[TestCase("- ```a```")]
[TestCase("- i1\n - i1.1\n ```\n code\n ```")]
public void TestCodeBlock(string value)
{

View File

@@ -95,6 +95,10 @@ namespace Markdig.Parsers
for (int i = line.Start; i <= line.End; i++)
{
char c = line.Text[i];
if (c == '`')
{
return false;
}
switch (state)
{
case ParseState.AfterFence:

View File

@@ -87,6 +87,7 @@ namespace Markdig.Parsers.Inlines
}
bool isMatching = false;
bool firstAndLastWasSpace = false;
if (closeSticks == openSticks)
{
string content;
@@ -95,6 +96,7 @@ namespace Markdig.Parsers.Inlines
if (!allSpace && builder.Length > 2 && builder[0] == ' ' && builder[builder.Length - 1] == ' ')
{
content = builder.ToString(1, builder.Length - 2);
firstAndLastWasSpace = true;
}
else
{
@@ -109,7 +111,8 @@ namespace Markdig.Parsers.Inlines
Span = new SourceSpan(processor.GetSourcePosition(startPosition, out int line, out int column), processor.GetSourcePosition(slice.Start - 1)),
Line = line,
Column = column,
DelimiterCount = delimiterCount
DelimiterCount = delimiterCount,
FirstAndLastWasSpace = firstAndLastWasSpace
};
isMatching = true;
}

View File

@@ -32,6 +32,10 @@ namespace Markdig.Renderers.Normalize.Inlines
//}
var delimiterRun = new string(obj.Delimiter, obj.DelimiterCount);
renderer.Write(delimiterRun);
if (obj.FirstAndLastWasSpace)
{
renderer.Write(' ');
}
if (obj.Content.Length != 0)
{
if (obj.Content[0] == obj.Delimiter)
@@ -48,6 +52,10 @@ namespace Markdig.Renderers.Normalize.Inlines
{
renderer.Write(' ');
}
if (obj.FirstAndLastWasSpace)
{
renderer.Write(' ');
}
renderer.Write(delimiterRun);
}
}

View File

@@ -19,7 +19,6 @@ namespace Markdig.Renderers.Normalize
renderer.RenderLinesBefore(paragraph);
renderer.Write(paragraph.BeforeWhitespace);
renderer.WriteLeafInline(paragraph);
//renderer.FinishBlock();
renderer.RenderLineAfterIfNeeded(paragraph);
renderer.RenderLinesAfter(paragraph);
}

View File

@@ -2,6 +2,7 @@
// 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 System.Diagnostics;
namespace Markdig.Syntax.Inlines
@@ -28,5 +29,6 @@ namespace Markdig.Syntax.Inlines
/// </summary>
public string Content { get; set; }
public bool FirstAndLastWasSpace { get; set; }
}
}