using Humanizer; using Markdig.Renderers; using Markdig.Renderers.Html; namespace OliverBooth.Markdown.Callout; /// /// Represents an HTML renderer which renders a . /// internal sealed class CalloutRenderer : HtmlObjectRenderer { private static readonly Dictionary CalloutTypes = new() { ["NOTE"] = "pencil", ["ABSTRACT"] = "clipboard-list", ["INFO"] = "info", ["TODO"] = "circle-check", ["TIP"] = "flame", ["SUCCESS"] = "check", ["QUESTION"] = "circle-help", ["WARNING"] = "triangle-alert", ["FAILURE"] = "x", ["DANGER"] = "zap", ["BUG"] = "bug", ["EXAMPLE"] = "list", ["CITE"] = "quote", ["UPDATE"] = "calendar-check", }; /// protected override void Write(HtmlRenderer renderer, CalloutBlock block) { renderer.EnsureLine(); if (renderer.EnableHtmlForBlock) { RenderAsHtml(renderer, block); } else { RenderAsText(renderer, block); } renderer.EnsureLine(); } private static void RenderAsHtml(HtmlRenderer renderer, CalloutBlock block) { string title = block.Title.Text; ReadOnlySpan type = block.Type.AsSpan(); Span upperType = stackalloc char[type.Length]; type.ToUpperInvariant(upperType); if (!CalloutTypes.TryGetValue(upperType.ToString(), out string? lucideClass)) { lucideClass = "pencil"; } var typeString = type.ToString().ToLowerInvariant(); renderer.Write($"
"); renderer.Write("
"); renderer.Write(title.Length == 0 ? typeString.Humanize(LetterCasing.Sentence) : title); renderer.WriteLine("
"); renderer.WriteChildren(block); renderer.WriteLine("
"); renderer.EnsureLine(); } private static void RenderAsText(HtmlRenderer renderer, CalloutBlock block) { string title = block.Title.Text; ReadOnlySpan type = block.Type.AsSpan(); renderer.WriteLine(title.Length == 0 ? type.ToString().ToUpperInvariant() : title.ToUpperInvariant()); renderer.WriteChildren(block); renderer.EnsureLine(); } }