Skip to content

Commit

Permalink
Fix quoteblock with setexheading when no lazy continuations are involved
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Oct 5, 2020
1 parent a781ae1 commit 8b64ce4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
18 changes: 14 additions & 4 deletions src/Markdig/Parsers/BlockProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ public BlockProcessor(MarkdownDocument document, BlockParserList parsers, Markdo
/// Gets the character position before the indent occurred.
/// </summary>
public int StartBeforeIndent { get; private set; }

/// <summary>
/// Gets a boolean indicating whether the current line being parsed is lazy continuation.
/// </summary>
public bool IsLazy { get; private set; }

/// <summary>
/// Gets the current stack of <see cref="Block"/> being processed.
Expand Down Expand Up @@ -577,6 +582,8 @@ private void UpdateLastBlockAndContainer(int stackIndex = -1)
/// </exception>
private void TryContinueBlocks()
{
IsLazy = false;

// Set all blocks non opened.
// They will be marked as open in the following loop
for (int i = 1; i < OpenedBlocks.Count; i++)
Expand Down Expand Up @@ -712,6 +719,7 @@ private bool TryOpenBlocks(BlockParser[] parsers)
{
for (int j = 0; j < parsers.Length; j++)
{
IsLazy = false;
var blockParser = parsers[j];
if (Line.IsEmpty)
{
Expand All @@ -731,17 +739,17 @@ private bool TryOpenBlocks(BlockParser[] parsers)
continue;
}

bool isLazyParagraph = blockParser is ParagraphBlockParser && lastBlock is ParagraphBlock;
IsLazy = blockParser is ParagraphBlockParser && lastBlock is ParagraphBlock;

var result = isLazyParagraph
var result = IsLazy
? blockParser.TryContinue(this, lastBlock)
: blockParser.TryOpen(this);

if (result == BlockState.None)
{
// If we have reached a blank line after trying to parse a paragraph
// we can ignore it
if (isLazyParagraph && IsBlankLine)
if (IsLazy && IsBlankLine)
{
ContinueProcessingLine = false;
break;
Expand All @@ -752,7 +760,7 @@ private bool TryOpenBlocks(BlockParser[] parsers)
// Special case for paragraph
UpdateLastBlockAndContainer();

if (isLazyParagraph && CurrentBlock is ParagraphBlock paragraph)
if (IsLazy && CurrentBlock is ParagraphBlock paragraph)
{
Debug.Assert(NewBlocks.Count == 0);

Expand Down Expand Up @@ -782,6 +790,8 @@ private bool TryOpenBlocks(BlockParser[] parsers)

// We have a leaf node, we can stop
}

IsLazy = false;
return false;
}

Expand Down
8 changes: 6 additions & 2 deletions src/Markdig/Parsers/ParagraphBlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public override BlockState TryContinue(BlockProcessor processor, Block block)
return BlockState.BreakDiscard;
}

if (ParseSetexHeadings && !processor.IsCodeIndent)
if (!processor.IsCodeIndent && ParseSetexHeadings)
{
return TryParseSetexHeading(processor, block);
}
Expand Down Expand Up @@ -86,7 +86,11 @@ private BlockState TryParseSetexHeading(BlockProcessor state, Block block)

// If we matched a LinkReferenceDefinition before matching the heading, and the remaining
// lines are empty, we can early exit and remove the paragraph
if (!(TryMatchLinkReferenceDefinition(ref paragraph.Lines, state) && paragraph.Lines.Count == 0))
var parent = block.Parent;

bool isSetTextHeading = !state.IsLazy || paragraph.Column == state.Column || !(parent is QuoteBlock || parent is ListItemBlock);

if (!(TryMatchLinkReferenceDefinition(ref paragraph.Lines, state) && paragraph.Lines.Count == 0) && isSetTextHeading)
{
// We discard the paragraph that will be transformed to a heading
state.Discard(paragraph);
Expand Down
2 changes: 1 addition & 1 deletion src/Markdig/Parsers/ThematicBreakParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override BlockState TryOpen(BlockProcessor processor)
if (isSetexHeading)
{
var parent = previousParagraph.Parent;
if ((parent is QuoteBlock && processor.CurrentLineStartPosition > parent.Span.End) || (parent is ListItemBlock && previousParagraph.Column != processor.Column))
if (previousParagraph.Column != processor.Column && (parent is QuoteBlock || parent is ListItemBlock))
{
isSetexHeading = false;
}
Expand Down

0 comments on commit 8b64ce4

Please sign in to comment.