From b7ae04bdbaeff0a23bbd71a6a5af3dc900cf2b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20G=C3=B6ls?= <6608231+Abrynos@users.noreply.github.com> Date: Mon, 18 Mar 2024 09:04:47 +0100 Subject: [PATCH 1/4] Make alert block headers case-invariant --- src/Markdig/Extensions/Alerts/AlertBlockRenderer.cs | 4 +++- src/Markdig/Extensions/Alerts/AlertInlineParser.cs | 4 ++-- src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs | 5 ++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Markdig/Extensions/Alerts/AlertBlockRenderer.cs b/src/Markdig/Extensions/Alerts/AlertBlockRenderer.cs index f9b95d61..1309f568 100644 --- a/src/Markdig/Extensions/Alerts/AlertBlockRenderer.cs +++ b/src/Markdig/Extensions/Alerts/AlertBlockRenderer.cs @@ -61,7 +61,9 @@ protected override void Write(HtmlRenderer renderer, AlertBlock obj) /// The kind of the alert to render public static void DefaultRenderKind(HtmlRenderer renderer, StringSlice kind) { - string? html = kind.AsSpan() switch + Span upperKind = stackalloc char[kind.Length]; + kind.AsSpan().ToUpperInvariant(upperKind); + string? html = upperKind switch { "NOTE" => "

Note

", "TIP" => "

Tip

", diff --git a/src/Markdig/Extensions/Alerts/AlertInlineParser.cs b/src/Markdig/Extensions/Alerts/AlertInlineParser.cs index f5a5881d..6f95c5b6 100644 --- a/src/Markdig/Extensions/Alerts/AlertInlineParser.cs +++ b/src/Markdig/Extensions/Alerts/AlertInlineParser.cs @@ -46,7 +46,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice) var start = slice.Start; var end = start; - while (c.IsAlphaUpper()) + while (c.IsAlpha()) { end = slice.Start; c = slice.NextChar(); @@ -86,7 +86,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice) } break; } - else if (!c.IsSpaceOrTab()) + else if (!c.IsSpaceOrTab()) { slice = saved; return false; diff --git a/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs b/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs index 560cc7bf..483377bb 100644 --- a/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs +++ b/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs @@ -61,7 +61,10 @@ private static void PipelineOnDocumentProcessed(MarkdownDocument document) var attributes = node.GetAttributes(); attributes.AddClass("alert"); attributes.AddProperty("role", "alert"); - string? @class = alertBlock.Kind.AsSpan() switch + + Span upperKind = new char[alertBlock.Kind.Length]; + alertBlock.Kind.AsSpan().ToUpperInvariant(upperKind); + string? @class = upperKind switch { "NOTE" => "alert-primary", "TIP" => "alert-success", From 42bd65caafc65fb8adf116ca80805271413a5740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20G=C3=B6ls?= <6608231+Abrynos@users.noreply.github.com> Date: Mon, 18 Mar 2024 09:18:39 +0100 Subject: [PATCH 2/4] Apply feedback --- src/Markdig/Extensions/Alerts/AlertBlockRenderer.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Markdig/Extensions/Alerts/AlertBlockRenderer.cs b/src/Markdig/Extensions/Alerts/AlertBlockRenderer.cs index 1309f568..6ac4aed7 100644 --- a/src/Markdig/Extensions/Alerts/AlertBlockRenderer.cs +++ b/src/Markdig/Extensions/Alerts/AlertBlockRenderer.cs @@ -50,6 +50,7 @@ protected override void Write(HtmlRenderer renderer, AlertBlock obj) { renderer.WriteLine(""); } + renderer.EnsureLine(); } @@ -61,6 +62,11 @@ protected override void Write(HtmlRenderer renderer, AlertBlock obj) /// The kind of the alert to render public static void DefaultRenderKind(HtmlRenderer renderer, StringSlice kind) { + if (kind.Length >= 16) + { + return; + } + Span upperKind = stackalloc char[kind.Length]; kind.AsSpan().ToUpperInvariant(upperKind); string? html = upperKind switch From 03390e4f71a5e7364622ef6efb2e5a55a3327cad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20G=C3=B6ls?= <6608231+Abrynos@users.noreply.github.com> Date: Mon, 18 Mar 2024 10:28:36 +0100 Subject: [PATCH 3/4] Misc. --- .../Bootstrap/BootstrapExtension.cs | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs b/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs index 483377bb..6f511eaa 100644 --- a/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs +++ b/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs @@ -41,6 +41,7 @@ public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) private static void PipelineOnDocumentProcessed(MarkdownDocument document) { + Span upperKind = new char[16]; foreach (var node in document.Descendants()) { if (node.IsInline) @@ -61,22 +62,23 @@ private static void PipelineOnDocumentProcessed(MarkdownDocument document) var attributes = node.GetAttributes(); attributes.AddClass("alert"); attributes.AddProperty("role", "alert"); - - Span upperKind = new char[alertBlock.Kind.Length]; - alertBlock.Kind.AsSpan().ToUpperInvariant(upperKind); - string? @class = upperKind switch + if (alertBlock.Kind.Length <= upperKind.Length) { - "NOTE" => "alert-primary", - "TIP" => "alert-success", - "IMPORTANT" => "alert-info", - "WARNING" => "alert-warning", - "CAUTION" => "alert-danger", - _ => null, - }; + alertBlock.Kind.AsSpan().ToUpperInvariant(upperKind); + string? @class = upperKind.Slice(0, alertBlock.Kind.Length) switch + { + "NOTE" => "alert-primary", + "TIP" => "alert-success", + "IMPORTANT" => "alert-info", + "WARNING" => "alert-warning", + "CAUTION" => "alert-danger", + _ => null, + }; - if (@class is not null) - { - attributes.AddClass(@class); + if (@class is not null) + { + attributes.AddClass(@class); + } } var lastParagraph = alertBlock.Descendants().OfType().LastOrDefault(); From c19ba5b0ebe32f7350019b8fb37b8e2b7f85f12a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20G=C3=B6ls?= <6608231+Abrynos@users.noreply.github.com> Date: Mon, 25 Mar 2024 12:29:22 +0100 Subject: [PATCH 4/4] Add fallback value in order to mark unknown alert kinds in some way as well --- .../Extensions/Bootstrap/BootstrapExtension.cs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs b/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs index 6f511eaa..db360ae1 100644 --- a/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs +++ b/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs @@ -65,20 +65,15 @@ private static void PipelineOnDocumentProcessed(MarkdownDocument document) if (alertBlock.Kind.Length <= upperKind.Length) { alertBlock.Kind.AsSpan().ToUpperInvariant(upperKind); - string? @class = upperKind.Slice(0, alertBlock.Kind.Length) switch + attributes.AddClass(upperKind.Slice(0, alertBlock.Kind.Length) switch { "NOTE" => "alert-primary", "TIP" => "alert-success", "IMPORTANT" => "alert-info", "WARNING" => "alert-warning", "CAUTION" => "alert-danger", - _ => null, - }; - - if (@class is not null) - { - attributes.AddClass(@class); - } + _ => "alert-dark", + }); } var lastParagraph = alertBlock.Descendants().OfType().LastOrDefault();