-
-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/Markdig/Extensions/Bootstrap/BootstrapAlertRenderer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters