Skip to content

Commit 1e17dcd

Browse files
authored
Merge pull request #866 from MihaZupan/alert-perf
Improve Alert parsing perf
2 parents 40e5ab1 + bbefce3 commit 1e17dcd

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
lines changed

src/Markdig/Extensions/Alerts/AlertBlock.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public AlertBlock(StringSlice kind) : base(null)
2222
}
2323

2424
/// <summary>
25-
/// Gets or sets the kind of the alert block (e.g `NOTE`, `TIP`, `IMPORTANT`, `WARNING`, `CAUTION`)
25+
/// Gets or sets the kind of the alert block (e.g `NOTE`, `TIP`, `IMPORTANT`, `WARNING`, `CAUTION`).
2626
/// </summary>
2727
public StringSlice Kind { get; set; }
2828

src/Markdig/Extensions/Alerts/AlertInlineParser.cs

+23-16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ namespace Markdig.Extensions.Alerts;
1515
/// <seealso cref="InlineParser" />
1616
public 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!;

src/Markdig/MarkdownPipeline.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected override void Reset(HtmlRenderer instance)
127127
}
128128
}
129129

130-
internal readonly struct RentedHtmlRenderer : IDisposable
130+
internal readonly ref struct RentedHtmlRenderer : IDisposable
131131
{
132132
private readonly HtmlRendererCache _cache;
133133
public readonly HtmlRenderer Instance;

src/Markdig/Syntax/MarkdownObject.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public string ToPositionText()
153153
return Unsafe.As<T>(storage.Trivia);
154154
}
155155

156-
private class DataEntriesAndTrivia
156+
private sealed class DataEntriesAndTrivia
157157
{
158158
private struct DataEntry(object key, object value)
159159
{

src/Markdig/Syntax/QuoteBlock.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public QuoteBlock(BlockParser? parser) : base(parser)
2525

2626
/// <summary>
2727
/// Gets or sets the trivia per line of this QuoteBlock.
28-
/// Trivia: only parsed when <see cref="MarkdownPipeline.TrackTrivia"/> is enabled, otherwise null.
28+
/// Trivia: only parsed when <see cref="MarkdownPipeline.TrackTrivia"/> is enabled.
2929
/// </summary>
3030
public List<QuoteBlockLine> QuoteLines => Trivia;
3131

0 commit comments

Comments
 (0)