@@ -15,6 +15,9 @@ namespace Markdig.Extensions.Alerts;
1515/// <seealso cref="InlineParser" />
1616public class AlertInlineParser : InlineParser
1717{
18+ private static readonly TransformedStringCache s_alertTypeClassCache = new (
19+ type => $ "markdown-alert-{ type . ToLowerInvariant ( ) } ") ;
20+
1821 /// <summary>
1922 /// Initializes a new instance of the <see cref="AlertInlineParser"/> class.
2023 /// </summary>
@@ -25,27 +28,30 @@ public AlertInlineParser()
2528
2629 public override bool Match ( InlineProcessor processor , ref StringSlice slice )
2730 {
28- // We expect the alert to be the first child of a quote block. Example:
29- // > [!NOTE]
30- // > This is a note
31- if ( processor . Block is not ParagraphBlock paragraphBlock || paragraphBlock . Parent is not QuoteBlock quoteBlock || paragraphBlock . Inline ? . FirstChild != null
32- || quoteBlock is AlertBlock || quoteBlock . Parent is not MarkdownDocument )
31+ if ( slice . PeekChar ( ) != '!' )
3332 {
3433 return false ;
3534 }
3635
37- var saved = slice ;
38- var c = slice . NextChar ( ) ;
39- if ( c != '!' )
36+ // We expect the alert to be the first child of a quote block. Example:
37+ // > [!NOTE]
38+ // > This is a note
39+ if ( processor . Block is not ParagraphBlock paragraphBlock ||
40+ paragraphBlock . Parent is not QuoteBlock quoteBlock ||
41+ paragraphBlock . Inline ? . FirstChild != null ||
42+ quoteBlock is AlertBlock ||
43+ quoteBlock . Parent is not MarkdownDocument )
4044 {
41- slice = saved ;
4245 return false ;
4346 }
4447
45- c = slice . NextChar ( ) ; // Skip !
48+ StringSlice saved = slice ;
49+
50+ slice . SkipChar ( ) ; // Skip [
51+ char c = slice . NextChar ( ) ; // Skip !
4652
47- var start = slice . Start ;
48- var end = start ;
53+ int start = slice . Start ;
54+ int end = start ;
4955 while ( c . IsAlpha ( ) )
5056 {
5157 end = slice . Start ;
@@ -76,13 +82,13 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
7682 end = slice . Start ;
7783 if ( c == '\n ' )
7884 {
79- slice . NextChar ( ) ; // Skip \n
85+ slice . SkipChar ( ) ; // Skip \n
8086 }
8187 }
8288 }
8389 else if ( c == '\n ' )
8490 {
85- slice . NextChar ( ) ; // Skip \n
91+ slice . SkipChar ( ) ; // Skip \n
8692 }
8793 break ;
8894 }
@@ -103,8 +109,9 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
103109 Column = quoteBlock . Column ,
104110 } ;
105111
106- alertBlock . GetAttributes ( ) . AddClass ( "markdown-alert" ) ;
107- alertBlock . GetAttributes ( ) . AddClass ( $ "markdown-alert-{ alertType . ToString ( ) . ToLowerInvariant ( ) } ") ;
112+ HtmlAttributes attributes = alertBlock . GetAttributes ( ) ;
113+ attributes . AddClass ( "markdown-alert" ) ;
114+ attributes . AddClass ( s_alertTypeClassCache . Get ( alertType . AsSpan ( ) ) ) ;
108115
109116 // Replace the quote block with the alert block
110117 var parentQuoteBlock = quoteBlock . Parent ! ;
0 commit comments