Skip to content

Commit

Permalink
Add bootstrap alert renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
Abrynos committed Mar 15, 2024
1 parent f48331d commit 7132584
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
72 changes: 72 additions & 0 deletions src/Markdig/Extensions/Bootstrap/BootstrapAlertRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// 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;

namespace Markdig.Extensions.Bootstrap;

/// <summary>
/// A HTML renderer for a <see cref="AlertBlock"/> that adds bootstrap classes.
/// </summary>
/// <seealso cref="HtmlObjectRenderer{AlertBlock}" />
public class BootstrapAlertRenderer : HtmlObjectRenderer<AlertBlock>
{
/// <summary>
/// Creates a new instance of this renderer.
/// </summary>
public BootstrapAlertRenderer() { }

/// <inheritdoc />
protected override void Write(HtmlRenderer renderer, AlertBlock obj)
{
AddAttributes(obj);
var lastParagraph = obj.Descendants().OfType<ParagraphBlock>().LastOrDefault();
lastParagraph?.GetAttributes().AddProperty("style", "margin-bottom: 0");

renderer.EnsureLine();
if (renderer.EnableHtmlForBlock)
{
renderer.Write("<div");
renderer.WriteAttributes(obj);
renderer.WriteLine('>');
}

var savedImplicitParagraph = renderer.ImplicitParagraph;
renderer.ImplicitParagraph = false;
renderer.WriteChildren(obj);
renderer.ImplicitParagraph = savedImplicitParagraph;
if (renderer.EnableHtmlForBlock)
{
renderer.WriteLine("</div>");
}

renderer.EnsureLine();
}

private static void AddAttributes(AlertBlock obj)
{
var attributes = obj.GetAttributes();
attributes.AddClass("alert");
attributes.AddProperty("role", "alert");

string? @class = obj.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);
}
}
}
14 changes: 12 additions & 2 deletions src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// 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 Markdig.Extensions.Alerts;
using Markdig.Renderers;
using Markdig.Renderers.Html;
using Markdig.Syntax;
Expand All @@ -24,6 +25,15 @@ public void Setup(MarkdownPipelineBuilder pipeline)

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>())
{
// Needs to be inserted before the original renderer
htmlRenderer.ObjectRenderers.Insert(0, new BootstrapAlertRenderer());
}
}
}

private static void PipelineOnDocumentProcessed(MarkdownDocument document)
Expand All @@ -43,7 +53,7 @@ private static void PipelineOnDocumentProcessed(MarkdownDocument document)
{
node.GetAttributes().AddClass("table");
}
else if (node is QuoteBlock)
else if (node is QuoteBlock and not AlertBlock)
{
node.GetAttributes().AddClass("blockquote");
}
Expand Down

0 comments on commit 7132584

Please sign in to comment.