Skip to content

Commit

Permalink
Fix source span of paragraphs in table cells and cleanup InlineProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Benni committed Aug 25, 2023
1 parent d26822b commit a70ca63
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/Markdig.Tests/MiscTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text.RegularExpressions;

using Markdig.Extensions.AutoLinks;
using Markdig.Extensions.Tables;
using Markdig.Syntax;
using NUnit.Framework;

Expand Down Expand Up @@ -303,4 +304,17 @@ public void RootInlineHasCorrectSourceSpan()
var expectedSourceSpan = new SourceSpan(0, 10);
Assert.That(((LeafBlock)document.LastChild).Inline.Span == expectedSourceSpan);
}

[Test]
public void RootInlineInTableCellHasCorrectSourceSpan()
{
var pipeline = new MarkdownPipelineBuilder().UsePreciseSourceLocation().UseAdvancedExtensions().Build();
pipeline.TrackTrivia = true;

var document = Markdown.Parse("| a | b |\n| --- | --- |\n| <span id=\"dest\"></span><span id=\"DEST\"></span>*dest*<br/> | \\[in\\] The address of the result of the operation.<br/> |", pipeline);

var paragraph = (ParagraphBlock)((TableCell)((TableRow)((Table)document.LastChild).LastChild).First()).LastChild;
Assert.That(paragraph.Inline.Span.Start == paragraph.Inline.FirstChild.Span.Start);
Assert.That(paragraph.Inline.Span.End == paragraph.Inline.LastChild.Span.End);
}
}
7 changes: 6 additions & 1 deletion src/Markdig/Extensions/Tables/PipeTableParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ public bool PostProcess(InlineProcessor state, Inline? root, Inline? lastChild,
{
var paragraph = (ParagraphBlock) cell[0];
state.PostProcessInlines(postInlineProcessorIndex + 1, paragraph.Inline, null, true);
if (paragraph.Inline?.LastChild is not null)
{
paragraph.Inline.Span.End = paragraph.Inline.LastChild.Span.End;
paragraph.UpdateSpanEnd(paragraph.Inline.LastChild.Span.End);
}
}

// Clear cells when we are done
Expand Down Expand Up @@ -520,7 +525,7 @@ private static bool ParseHeaderString(Inline? inline, out TableColumnAlign? alig
// Create aligns until we may have a header row

aligns ??= new List<TableColumnDefinition>();

aligns.Add(new TableColumnDefinition() { Alignment = align });

// If this is the last delimiter, we need to check the right side of the `|` delimiter
Expand Down
3 changes: 1 addition & 2 deletions src/Markdig/Parsers/InlineProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,8 @@ public void ProcessInlineLeaf(LeafBlock leafBlock)
if (leafBlock.Inline.LastChild is not null)
{
leafBlock.Inline.Span.End = leafBlock.Inline.LastChild.Span.End;
leafBlock.UpdateSpanEnd(leafBlock.Inline.Span.End);
}

leafBlock.UpdateSpanEnd(leafBlock.Inline.Span.End);
}

public void PostProcessInlines(int startingIndex, Inline? root, Inline? lastChild, bool isFinalProcessing)
Expand Down

0 comments on commit a70ca63

Please sign in to comment.