Skip to content

Commit 7b1c16a

Browse files
authored
Fix an edgecase where substitutions could not track the delimiter properly when an empty line preceded it and followed it (#155)
1 parent a63046d commit 7b1c16a

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/Elastic.Markdown/Myst/Substitution/SubstitutionParser.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
135135

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

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

149+
var start = processor.GetSourcePosition(startPosition, out var line, out var column);
150+
var end = processor.GetSourcePosition(slice.Start);
151+
var sourceSpan = new SourceSpan(start, end);
152+
148153
var substitutionLeaf = new SubstitutionLeaf(content.ToString(), found, replacement)
149154
{
150-
Delimiter = slice.Text[startPosition],
151-
Span = new SourceSpan(processor.GetSourcePosition(startPosition, out var line, out var column),
152-
processor.GetSourcePosition(slice.Start)),
155+
Delimiter = '{',
156+
Span = sourceSpan,
153157
Line = line,
154158
Column = column,
155159
DelimiterCount = openSticks

tests/Elastic.Markdown.Tests/Inline/SubstitutionTest.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,25 @@ public void OnlySeesGlobalVariable() =>
133133
public void HasError() => Collector.Diagnostics.Should().HaveCount(1)
134134
.And.Contain(d => d.Message.Contains("{hello-world} can not be redeclared in front matter as its a global substitution"));
135135
}
136+
137+
public class ReplaceInHeader(ITestOutputHelper output) : InlineTest(output,
138+
"""
139+
---
140+
sub:
141+
hello-world: "Hello World!"
142+
---
143+
144+
## {{hello-world}} [#custom-anchor]
145+
146+
"""
147+
)
148+
{
149+
150+
[Fact]
151+
public void OnlySeesGlobalVariable() =>
152+
Html.Should().Contain("<h2>Hello World!");
153+
154+
[Fact]
155+
public void HasNoErrors() => Collector.Diagnostics.Should().HaveCount(0);
156+
157+
}

0 commit comments

Comments
 (0)