|
4 | 4 |
|
5 | 5 | using System.Text.RegularExpressions; |
6 | 6 | using Elastic.Markdown.Diagnostics; |
| 7 | +using Elastic.Markdown.Helpers; |
7 | 8 | using Markdig.Helpers; |
8 | 9 | using Markdig.Parsers; |
9 | 10 | using Markdig.Syntax; |
@@ -87,7 +88,7 @@ public override bool Close(BlockProcessor processor, Block block) |
87 | 88 | var line = lines.Lines[index]; |
88 | 89 | var span = line.Slice.AsSpan(); |
89 | 90 |
|
90 | | - if (ReplaceSubstitutions(context, span, out var replacement)) |
| 91 | + if (span.ReplaceSubstitutions(context.FrontMatter?.Properties, out var replacement)) |
91 | 92 | { |
92 | 93 | var s = new StringSlice(replacement); |
93 | 94 | lines.Lines[index] = new StringLine(ref s); |
@@ -139,37 +140,6 @@ public override bool Close(BlockProcessor processor, Block block) |
139 | 140 | return base.Close(processor, block); |
140 | 141 | } |
141 | 142 |
|
142 | | - private static bool ReplaceSubstitutions(ParserContext context, ReadOnlySpan<char> span, out string? replacement) |
143 | | - { |
144 | | - replacement = null; |
145 | | - var substitutions = context.FrontMatter?.Properties ?? new(); |
146 | | - if (substitutions.Count == 0) |
147 | | - return false; |
148 | | - |
149 | | - var matchSubs = CallOutParser.MatchSubstitutions().EnumerateMatches(span); |
150 | | - |
151 | | - var replaced = false; |
152 | | - foreach (var match in matchSubs) |
153 | | - { |
154 | | - if (match.Length == 0) |
155 | | - continue; |
156 | | - |
157 | | - var spanMatch = span.Slice(match.Index, match.Length); |
158 | | - var key = spanMatch.Trim(['{', '}']); |
159 | | - |
160 | | - // TODO: alternate lookup using span in c# 9 |
161 | | - if (substitutions.TryGetValue(key.ToString(), out var value)) |
162 | | - { |
163 | | - replacement ??= span.ToString(); |
164 | | - replacement = replacement.Replace(spanMatch.ToString(), value); |
165 | | - replaced = true; |
166 | | - } |
167 | | - |
168 | | - } |
169 | | - |
170 | | - return replaced; |
171 | | - } |
172 | | - |
173 | 143 | private static CallOut? EnumerateAnnotations(Regex.ValueMatchEnumerator matches, |
174 | 144 | ref ReadOnlySpan<char> span, |
175 | 145 | ref int callOutIndex, |
|
0 commit comments