Skip to content

Commit 7e318b2

Browse files
committed
Fix case where code itself contains < or >
1 parent 428bb0a commit 7e318b2

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/Elastic.Markdown/Myst/CodeBlocks/EnhancedCodeBlockParser.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public override bool Close(BlockProcessor processor, Block block)
119119
}
120120

121121
//update string slices to ignore call outs
122-
if (codeBlock.CallOuts?.Count > 0)
122+
if (codeBlock.CallOuts.Count > 0)
123123
{
124124

125125
var callouts = codeBlock.CallOuts.Aggregate(new Dictionary<int, CallOut>(), (acc, curr) =>
@@ -200,9 +200,11 @@ private static List<CallOut> EnumerateAnnotations(Regex.ValueMatchEnumerator mat
200200

201201
private static List<CallOut> ParseClassicCallOuts(ValueMatch match, ref ReadOnlySpan<char> span, ref int callOutIndex, int originatingLine)
202202
{
203-
var startIndex = span.LastIndexOf("<");
203+
var indexOfLastComment = Math.Max(span.LastIndexOf('#'), span.LastIndexOf("//"));
204+
var startIndex = span.LastIndexOf('<');
204205
if (startIndex <= 0)
205206
return [];
207+
206208
var allStartIndices = new List<int>();
207209
for (var i = 0; i < span.Length; i++)
208210
{
@@ -215,15 +217,17 @@ private static List<CallOut> ParseClassicCallOuts(ValueMatch match, ref ReadOnly
215217
callOutIndex++;
216218
var endIndex = span.Slice(match.Index + individualStartIndex).IndexOf('>') + 1;
217219
var callout = span.Slice(match.Index + individualStartIndex, endIndex);
218-
_ = int.TryParse(callout.Trim(['<', '>']), out var index);
219-
callOuts.Add(new CallOut
220+
if (int.TryParse(callout.Trim(['<', '>']), out var index))
220221
{
221-
Index = index,
222-
Text = callout.TrimStart('/').TrimStart('#').TrimStart().ToString(),
223-
InlineCodeAnnotation = false,
224-
SliceStart = individualStartIndex,
225-
Line = originatingLine,
226-
});
222+
callOuts.Add(new CallOut
223+
{
224+
Index = index,
225+
Text = callout.TrimStart('/').TrimStart('#').TrimStart().ToString(),
226+
InlineCodeAnnotation = false,
227+
SliceStart = indexOfLastComment > 0 ? indexOfLastComment : startIndex,
228+
Line = originatingLine,
229+
});
230+
}
227231
}
228232
return callOuts;
229233
}

0 commit comments

Comments
 (0)