Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make alert block headers case-invariant #784

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Markdig/Extensions/Alerts/AlertBlockRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected override void Write(HtmlRenderer renderer, AlertBlock obj)
{
renderer.WriteLine("</div>");
}

renderer.EnsureLine();
}

Expand All @@ -61,7 +62,14 @@ protected override void Write(HtmlRenderer renderer, AlertBlock obj)
/// <param name="kind">The kind of the alert to render</param>
public static void DefaultRenderKind(HtmlRenderer renderer, StringSlice kind)
{
string? html = kind.AsSpan() switch
if (kind.Length >= 16)
{
return;
}

Span<char> upperKind = stackalloc char[kind.Length];
Abrynos marked this conversation as resolved.
Show resolved Hide resolved
kind.AsSpan().ToUpperInvariant(upperKind);
string? html = upperKind switch
{
"NOTE" => "<p class=\"markdown-alert-title\"><svg viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z\"></path></svg>Note</p>",
"TIP" => "<p class=\"markdown-alert-title\"><svg viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"M8 1.5c-2.363 0-4 1.69-4 3.75 0 .984.424 1.625.984 2.304l.214.253c.223.264.47.556.673.848.284.411.537.896.621 1.49a.75.75 0 0 1-1.484.211c-.04-.282-.163-.547-.37-.847a8.456 8.456 0 0 0-.542-.68c-.084-.1-.173-.205-.268-.32C3.201 7.75 2.5 6.766 2.5 5.25 2.5 2.31 4.863 0 8 0s5.5 2.31 5.5 5.25c0 1.516-.701 2.5-1.328 3.259-.095.115-.184.22-.268.319-.207.245-.383.453-.541.681-.208.3-.33.565-.37.847a.751.751 0 0 1-1.485-.212c.084-.593.337-1.078.621-1.489.203-.292.45-.584.673-.848.075-.088.147-.173.213-.253.561-.679.985-1.32.985-2.304 0-2.06-1.637-3.75-4-3.75ZM5.75 12h4.5a.75.75 0 0 1 0 1.5h-4.5a.75.75 0 0 1 0-1.5ZM6 15.25a.75.75 0 0 1 .75-.75h2.5a.75.75 0 0 1 0 1.5h-2.5a.75.75 0 0 1-.75-.75Z\"></path></svg>Tip</p>",
Expand Down
4 changes: 2 additions & 2 deletions src/Markdig/Extensions/Alerts/AlertInlineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 12 additions & 12 deletions src/Markdig/Extensions/Bootstrap/BootstrapExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)

private static void PipelineOnDocumentProcessed(MarkdownDocument document)
{
Span<char> upperKind = new char[16];
foreach (var node in document.Descendants())
{
if (node.IsInline)
Expand All @@ -61,19 +62,18 @@ private static void PipelineOnDocumentProcessed(MarkdownDocument document)
var attributes = node.GetAttributes();
attributes.AddClass("alert");
attributes.AddProperty("role", "alert");
string? @class = alertBlock.Kind.AsSpan() switch
if (alertBlock.Kind.Length <= upperKind.Length)
{
"NOTE" => "alert-primary",
"TIP" => "alert-success",
"IMPORTANT" => "alert-info",
"WARNING" => "alert-warning",
"CAUTION" => "alert-danger",
_ => null,
};

if (@class is not null)
{
attributes.AddClass(@class);
alertBlock.Kind.AsSpan().ToUpperInvariant(upperKind);
attributes.AddClass(upperKind.Slice(0, alertBlock.Kind.Length) switch
{
"NOTE" => "alert-primary",
"TIP" => "alert-success",
"IMPORTANT" => "alert-info",
"WARNING" => "alert-warning",
"CAUTION" => "alert-danger",
_ => "alert-dark",
});
}

var lastParagraph = alertBlock.Descendants().OfType<ParagraphBlock>().LastOrDefault();
Expand Down