Skip to content

Commit

Permalink
Apply feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Abrynos authored Mar 18, 2024
1 parent 7132584 commit f9e96bc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 77 deletions.
72 changes: 0 additions & 72 deletions src/Markdig/Extensions/Bootstrap/BootstrapAlertRenderer.cs

This file was deleted.

36 changes: 31 additions & 5 deletions src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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;
Expand All @@ -27,12 +28,14 @@ public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
{
if (renderer is HtmlRenderer htmlRenderer)
{
// We only want to add our renderer if we already support alert blocks
if (htmlRenderer.ObjectRenderers.Contains<AlertBlockRenderer>())
var alertRenderer = htmlRenderer.ObjectRenderers.OfType<AlertBlockRenderer>().FirstOrDefault();
if (alertRenderer == null)
{
// Needs to be inserted before the original renderer
htmlRenderer.ObjectRenderers.Insert(0, new BootstrapAlertRenderer());
alertRenderer = new AlertBlockRenderer();
renderer.ObjectRenderers.InsertBefore<QuoteBlockRenderer>(new AlertBlockRenderer());
}

alertRenderer.RenderKind = (_, _) => { };
}
}

Expand All @@ -53,7 +56,30 @@ private static void PipelineOnDocumentProcessed(MarkdownDocument document)
{
node.GetAttributes().AddClass("table");
}
else if (node is QuoteBlock and not AlertBlock)
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<ParagraphBlock>().LastOrDefault();
lastParagraph?.GetAttributes().AddClass("mb-0");
}
else if (node is QuoteBlock)
{
node.GetAttributes().AddClass("blockquote");
}
Expand Down

0 comments on commit f9e96bc

Please sign in to comment.