diff --git a/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs b/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs index 8344f0a3a..560cc7bf0 100644 --- a/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs +++ b/src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs @@ -1,7 +1,9 @@ // Copyright (c) Alexandre Mutel. All rights reserved. -// This file is licensed under the BSD-Clause 2 license. +// This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. +using System.Linq; +using Markdig.Extensions.Alerts; using Markdig.Renderers; using Markdig.Renderers.Html; using Markdig.Syntax; @@ -24,6 +26,17 @@ public void Setup(MarkdownPipelineBuilder pipeline) public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) { + if (renderer is HtmlRenderer htmlRenderer) + { + var alertRenderer = htmlRenderer.ObjectRenderers.OfType().FirstOrDefault(); + if (alertRenderer == null) + { + alertRenderer = new AlertBlockRenderer(); + renderer.ObjectRenderers.InsertBefore(new AlertBlockRenderer()); + } + + alertRenderer.RenderKind = (_, _) => { }; + } } private static void PipelineOnDocumentProcessed(MarkdownDocument document) @@ -43,6 +56,29 @@ private static void PipelineOnDocumentProcessed(MarkdownDocument document) { node.GetAttributes().AddClass("table"); } + else if (node is AlertBlock alertBlock) // Needs to be before QuoteBlock + { + var attributes = node.GetAttributes(); + attributes.AddClass("alert"); + attributes.AddProperty("role", "alert"); + string? @class = alertBlock.Kind.AsSpan() 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); + } + + var lastParagraph = alertBlock.Descendants().OfType().LastOrDefault(); + lastParagraph?.GetAttributes().AddClass("mb-0"); + } else if (node is QuoteBlock) { node.GetAttributes().AddClass("blockquote");