Skip to content

Commit

Permalink
Fix an edgecase where substitutions could not track the delimiter pro…
Browse files Browse the repository at this point in the history
…perly when an empty line preceded it and followed it (#155)
  • Loading branch information
Mpdreamz authored Jan 8, 2025
1 parent a63046d commit 7b1c16a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Elastic.Markdown/Myst/Substitution/SubstitutionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)

// We've already skipped the opening sticks. Account for that here.
startPosition -= openSticks;
startPosition = Math.Max(startPosition, 0);

var key = content.ToString().Trim(['{', '}']);
var found = false;
Expand All @@ -145,11 +146,14 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
replacement = value.ToString() ?? string.Empty;
}

var start = processor.GetSourcePosition(startPosition, out var line, out var column);
var end = processor.GetSourcePosition(slice.Start);
var sourceSpan = new SourceSpan(start, end);

var substitutionLeaf = new SubstitutionLeaf(content.ToString(), found, replacement)
{
Delimiter = slice.Text[startPosition],
Span = new SourceSpan(processor.GetSourcePosition(startPosition, out var line, out var column),
processor.GetSourcePosition(slice.Start)),
Delimiter = '{',
Span = sourceSpan,
Line = line,
Column = column,
DelimiterCount = openSticks
Expand Down
22 changes: 22 additions & 0 deletions tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,25 @@ public void OnlySeesGlobalVariable() =>
public void HasError() => Collector.Diagnostics.Should().HaveCount(1)
.And.Contain(d => d.Message.Contains("{hello-world} can not be redeclared in front matter as its a global substitution"));
}

public class ReplaceInHeader(ITestOutputHelper output) : InlineTest(output,
"""
---
sub:
hello-world: "Hello World!"
---
## {{hello-world}} [#custom-anchor]
"""
)
{

[Fact]
public void OnlySeesGlobalVariable() =>
Html.Should().Contain("<h2>Hello World!");

[Fact]
public void HasNoErrors() => Collector.Diagnostics.Should().HaveCount(0);

}

0 comments on commit 7b1c16a

Please sign in to comment.