@@ -15,6 +15,9 @@ namespace Markdig.Extensions.Alerts;
15
15
/// <seealso cref="InlineParser" />
16
16
public class AlertInlineParser : InlineParser
17
17
{
18
+ private static readonly TransformedStringCache s_alertTypeClassCache = new (
19
+ type => $ "markdown-alert-{ type . ToLowerInvariant ( ) } ") ;
20
+
18
21
/// <summary>
19
22
/// Initializes a new instance of the <see cref="AlertInlineParser"/> class.
20
23
/// </summary>
@@ -25,27 +28,30 @@ public AlertInlineParser()
25
28
26
29
public override bool Match ( InlineProcessor processor , ref StringSlice slice )
27
30
{
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 ( ) != '!' )
33
32
{
34
33
return false ;
35
34
}
36
35
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 )
40
44
{
41
- slice = saved ;
42
45
return false ;
43
46
}
44
47
45
- c = slice . NextChar ( ) ; // Skip !
48
+ StringSlice saved = slice ;
49
+
50
+ slice . SkipChar ( ) ; // Skip [
51
+ char c = slice . NextChar ( ) ; // Skip !
46
52
47
- var start = slice . Start ;
48
- var end = start ;
53
+ int start = slice . Start ;
54
+ int end = start ;
49
55
while ( c . IsAlpha ( ) )
50
56
{
51
57
end = slice . Start ;
@@ -76,13 +82,13 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
76
82
end = slice . Start ;
77
83
if ( c == '\n ' )
78
84
{
79
- slice . NextChar ( ) ; // Skip \n
85
+ slice . SkipChar ( ) ; // Skip \n
80
86
}
81
87
}
82
88
}
83
89
else if ( c == '\n ' )
84
90
{
85
- slice . NextChar ( ) ; // Skip \n
91
+ slice . SkipChar ( ) ; // Skip \n
86
92
}
87
93
break ;
88
94
}
@@ -103,8 +109,9 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
103
109
Column = quoteBlock . Column ,
104
110
} ;
105
111
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 ( ) ) ) ;
108
115
109
116
// Replace the quote block with the alert block
110
117
var parentQuoteBlock = quoteBlock . Parent ! ;
0 commit comments