Skip to content

Commit fb1fdf7

Browse files
committed
Fix RoleParser
1 parent ad81fa4 commit fb1fdf7

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

src/Elastic.Markdown/Myst/Roles/RoleParser.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public abstract class RoleParser<TRole> : InlineParser
3131

3232
public override bool Match(InlineProcessor processor, ref StringSlice slice)
3333
{
34-
3534
var match = slice.CurrentChar;
3635

3736
if (processor.Context is not ParserContext)
@@ -58,6 +57,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
5857
closeSticks++;
5958
i++;
6059
}
60+
6161
if (closeSticks > 1)
6262
return false;
6363

@@ -66,28 +66,25 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
6666
return false;
6767

6868
// {role} has to be followed by `content`
69-
if (span[i] != '`')
70-
return false;
71-
if (span.Length == i - 1)
69+
if ((uint)i >= (uint)span.Length || span[i] != '`')
7270
return false;
7371

74-
var startContent = i;
75-
i = span[(i + 1)..].IndexOfAny(['`']);
76-
if ((uint)i >= (uint)span.Length)
77-
return false;
72+
var openingBacktickPos = i;
73+
var contentStartPos = i + 1; // Skip the opening backtick
7874

79-
var closeBackTicks = 0;
80-
while ((uint)i < (uint)span.Length && span[i] == '`')
75+
var closingBacktickIndex = -1;
76+
for (var j = contentStartPos; j < span.Length; j++)
8177
{
82-
closeBackTicks++;
83-
i++;
78+
if (span[j] != '`')
79+
continue;
80+
closingBacktickIndex = j;
81+
break;
8482
}
85-
if (closeBackTicks > 1)
83+
84+
if (closingBacktickIndex == -1)
8685
return false;
8786

88-
// Fix: Ensure we don't exceed the span length when calculating the end index
89-
var endIndex = Math.Min(startContent + i + 2, span.Length);
90-
var contentSpan = span[startContent..endIndex];
87+
var contentSpan = span[openingBacktickPos..(closingBacktickIndex + 1)];
9188

9289
var startPosition = slice.Start;
9390
slice.Start = startPosition + roleContent.Length + contentSpan.Length;
@@ -100,7 +97,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
10097
var end = processor.GetSourcePosition(slice.Start);
10198
var sourceSpan = new SourceSpan(start, end);
10299

103-
var leaf = CreateRole(roleContent.ToString(), contentSpan.Trim().Trim('`').ToString(), processor);
100+
var leaf = CreateRole(roleContent.ToString(), contentSpan.Trim('`').ToString(), processor);
104101
leaf.Delimiter = '{';
105102
leaf.Span = sourceSpan;
106103
leaf.Line = line;

0 commit comments

Comments
 (0)