Skip to content

Fix RoleParser #1536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/testing/req.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ mapped_pages:
stack: preview 9.0, ga 9.1
```

1. Select **Create** to create a new policy, or select **Edit** {icon}`pencil` to open an existing policy.
1. Select **Create** to create a new policy, or select **Edit** {icon}`logo_vulnerability_management` to open an existing policy.


{applies_to}`stack: preview 9.0` This tutorial is based on Elasticsearch 9.0.
This tutorial is based on Elasticsearch 9.0. This tutorial is based on Elasticsearch 9.0.
Expand Down
33 changes: 13 additions & 20 deletions src/Elastic.Markdown/Myst/Roles/RoleParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public abstract class RoleParser<TRole> : InlineParser

public override bool Match(InlineProcessor processor, ref StringSlice slice)
{

var match = slice.CurrentChar;

if (processor.Context is not ParserContext)
Expand All @@ -58,36 +57,30 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
closeSticks++;
i++;
}

if (closeSticks > 1)
return false;

var roleContent = slice.AsSpan()[..i];
if (!Matches(roleContent))
return false;

// {role} has to be followed by `content`
if (span[i] != '`')
return false;
if (span.Length == i - 1)
return false;
var openingBacktickPos = i;
var contentStartPos = i + 1; // Skip the opening backtick

var startContent = i;
i = span[(i + 1)..].IndexOfAny(['`']);
if ((uint)i >= (uint)span.Length)
return false;

var closeBackTicks = 0;
while ((uint)i < (uint)span.Length && span[i] == '`')
var closingBacktickIndex = -1;
for (var j = contentStartPos; j < span.Length; j++)
{
closeBackTicks++;
i++;
if (span[j] != '`')
continue;
closingBacktickIndex = j;
break;
}
if (closeBackTicks > 1)

if (closingBacktickIndex == -1)
return false;

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

var startPosition = slice.Start;
slice.Start = startPosition + roleContent.Length + contentSpan.Length;
Expand All @@ -100,7 +93,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
var end = processor.GetSourcePosition(slice.Start);
var sourceSpan = new SourceSpan(start, end);

var leaf = CreateRole(roleContent.ToString(), contentSpan.Trim().Trim('`').ToString(), processor);
var leaf = CreateRole(roleContent.ToString(), contentSpan.Trim('`').ToString(), processor);
leaf.Delimiter = '{';
leaf.Span = sourceSpan;
leaf.Line = line;
Expand Down
Loading