Skip to content

Commit 994e399

Browse files
committed
Refactor directive block parsing for efficiency.
Replaced inline dictionary with static readonly `FrozenDictionary` and introduced an alternate lookup for better performance. Adjusted tests and trimmed parsing logic to align with the updated implementation.
1 parent 1b61f91 commit 994e399

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/Elastic.Markdown/Myst/Directives/DirectiveBlockParser.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public DirectiveBlockParser()
3434

3535
private readonly string[] _codeBlocks = ["code", "code-block", "sourcecode"];
3636

37-
private readonly FrozenDictionary<string, int> _unsupportedBlocks = new Dictionary<string, int>
37+
private static readonly FrozenDictionary<string, int> UnsupportedBlocks = new Dictionary<string, int>
3838
{
3939
{ "bibliography", 5 },
4040
{ "blockquote", 6 },
@@ -63,17 +63,19 @@ public DirectiveBlockParser()
6363
{ "seealso", 3 }
6464
}.ToFrozenDictionary();
6565

66+
private static readonly FrozenDictionary<string, int>.AlternateLookup<ReadOnlySpan<char>> UnsupportedLookup =
67+
UnsupportedBlocks.GetAlternateLookup<ReadOnlySpan<char>>();
68+
6669
protected override DirectiveBlock CreateFencedBlock(BlockProcessor processor)
6770
{
6871
var info = processor.Line.AsSpan();
6972

7073
if (processor.Context is not ParserContext context)
7174
throw new Exception("Expected parser context to be of type ParserContext");
7275

73-
// TODO alternate lookup .NET 9
74-
var directive = info.ToString().Trim(['{', '}', '`']);
75-
if (_unsupportedBlocks.TryGetValue(directive, out var issueId))
76-
return new UnsupportedDirectiveBlock(this, directive, issueId, context);
76+
var directive = info.Trim(['{', '}', '`', ':']);
77+
if (UnsupportedLookup.TryGetValue(directive, out var issueId))
78+
return new UnsupportedDirectiveBlock(this, directive.ToString(), issueId, context);
7779

7880
if (info.IndexOf("{tab-set}") > 0)
7981
return new TabSetBlock(this, context);

tests/Elastic.Markdown.Tests/Directives/AdmonitionUnsupportedTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ namespace Elastic.Markdown.Tests.Directives;
1111
public abstract class AdmonitionUnsupportedTests(ITestOutputHelper output, string directive)
1212
: DirectiveTest<UnsupportedDirectiveBlock>(output,
1313
$$"""
14-
:::{{{directive}}}
15-
This is an attention block
16-
:::
17-
A regular paragraph.
18-
"""
14+
:::{{{directive}}}
15+
This is an attention block
16+
:::
17+
A regular paragraph.
18+
"""
1919
)
2020
{
2121
[Fact]

0 commit comments

Comments
 (0)