fix optional space after Atx heading leaders

This commit is contained in:
Ruud Poutsma
2020-10-23 19:29:12 +02:00
parent 87269d88b9
commit 55eaadce67
4 changed files with 36 additions and 2 deletions

View File

@@ -482,5 +482,22 @@ namespace Markdig.Helpers
}
return true;
}
public bool Overlaps(StringSlice other)
{
if (Length == 0 || other.Length == 0)
{
return false;
}
if (other.End >= Start)
{
return true;
}
if (other.Start <= End)
{
return true;
}
return false;
}
}
}

View File

@@ -72,10 +72,16 @@ namespace Markdig.Parsers
// A space is required after leading #
if (leadingCount > 0 && leadingCount <= MaxLeadingCount && (c.IsSpaceOrTab() || c == '\0'))
{
StringSlice whitespace = StringSlice.Empty;
if (c.IsSpaceOrTab())
{
whitespace = new StringSlice(processor.Line.Text, processor.Start + leadingCount, processor.Start + leadingCount);
}
// Move to the content
var headingBlock = new HeadingBlock(this)
{
HeaderChar = matchingChar,
WhitespaceAfterAtxHeaderChar = whitespace,
Level = leadingCount,
Column = column,
Span = { Start = sourcePosition },
@@ -129,7 +135,14 @@ namespace Markdig.Parsers
// Setup the source end position of this element
headingBlock.Span.End = processor.Line.End;
headingBlock.AfterWhitespace = new StringSlice(processor.Line.Text, processor.Line.End + 1, sourceEnd);
var wsa = new StringSlice(processor.Line.Text, processor.Line.End + 1, sourceEnd);
headingBlock.AfterWhitespace = wsa;
if (wsa.Overlaps(headingBlock.WhitespaceAfterAtxHeaderChar))
{
// prevent double whitespace allocation in case of closing # i.e. "# #"
headingBlock.WhitespaceAfterAtxHeaderChar = StringSlice.Empty;
}
// We expect a single line, so don't continue
return BlockState.Break;

View File

@@ -48,7 +48,8 @@ namespace Markdig.Renderers.Normalize
: new string('#', obj.Level);
renderer.Write(obj.BeforeWhitespace);
renderer.Write(headingText).Write(' ');
renderer.Write(headingText);
renderer.Write(obj.WhitespaceAfterAtxHeaderChar);
renderer.WriteLeafInline(obj);
renderer.Write(obj.AfterWhitespace);
renderer.WriteLine(obj.Newline);

View File

@@ -38,5 +38,8 @@ namespace Markdig.Syntax
public int HeaderCharCount { get; set; }
public Newline SetextNewline { get; set; }
// space is mandatory after # only when there is heading content. I.e. "#" is valid
public StringSlice WhitespaceAfterAtxHeaderChar { get; set; }
}
}