using System.Net; using System.Text; /// /// Generates HTML for BlogPckt content. /// static class BlogPcktHtmlGenerator { public static string Generate(SiteStandard.Document document, BlogPckt.Content content) { var html = new StringBuilder(); html.AppendLine(""); html.AppendLine(""); html.AppendLine(""); html.AppendLine(""); html.AppendLine(""); html.AppendLine($"{WebUtility.HtmlEncode(document.Title)}"); if (document.Description is not null) { html.AppendLine($""); } html.AppendLine(""); html.AppendLine(""); html.AppendLine("
"); html.AppendLine("
"); html.AppendLine($"

{WebUtility.HtmlEncode(document.Title)}

"); html.AppendLine($""); if (document.Tags is { Count: > 0 }) { html.Append("
"); foreach (var tag in document.Tags) { html.Append($"{WebUtility.HtmlEncode(tag)} "); } html.AppendLine("
"); } html.AppendLine("
"); if (content.Items is not null) { foreach (var item in content.Items) { RenderBlock(html, item); } } html.AppendLine("
"); html.AppendLine(""); html.AppendLine(""); return html.ToString(); } static void RenderBlock(StringBuilder html, System.Text.Json.JsonElement element) { var type = element.TryGetProperty("$type", out var tp) ? tp.GetString() : null; switch (type) { case BlogPckt.Block.Text.TypeId: var text = BlogPckt.Block.Text.FromJson(element); if (text is not null) { html.AppendLine($"

{RenderRichText(text.Plaintext, text.Facets)}

"); } break; case BlogPckt.Block.Heading.TypeId: var heading = BlogPckt.Block.Heading.FromJson(element); if (heading is not null) { var level = Math.Clamp(heading.Level ?? 1, 1, 6); html.AppendLine($"{RenderRichText(heading.Plaintext, heading.Facets)}"); } break; case BlogPckt.Block.CodeBlock.TypeId: var code = BlogPckt.Block.CodeBlock.FromJson(element); if (code is not null) { var langAttr = !string.IsNullOrEmpty(code.Language) ? $" class=\"language-{WebUtility.HtmlEncode(code.Language)}\"" : string.Empty; html.AppendLine($"
{WebUtility.HtmlEncode(code.Plaintext)}
"); } break; case BlogPckt.Block.Image.TypeId: var image = BlogPckt.Block.Image.FromJson(element); if (image?.Attrs is not null) { var attrs = image.Attrs; var altAttr = attrs.Alt is not null ? $" alt=\"{WebUtility.HtmlEncode(attrs.Alt)}\"" : " alt=\"\""; var titleAttr = attrs.Title is not null ? $" title=\"{WebUtility.HtmlEncode(attrs.Title)}\"" : string.Empty; html.AppendLine($"
"); html.AppendLine($""); if (attrs.Title is not null) { html.AppendLine($"
{WebUtility.HtmlEncode(attrs.Title)}
"); } html.AppendLine("
"); } break; case BlogPckt.Block.BulletList.TypeId: var bulletList = BlogPckt.Block.BulletList.FromJson(element); if (bulletList is not null) { RenderBulletList(html, bulletList); } break; case BlogPckt.Block.OrderedList.TypeId: var orderedList = BlogPckt.Block.OrderedList.FromJson(element); if (orderedList is not null) { RenderOrderedList(html, orderedList); } break; case BlogPckt.Block.Blockquote.TypeId: var blockquote = BlogPckt.Block.Blockquote.FromJson(element); if (blockquote is not null) { html.AppendLine("
"); foreach (var item in blockquote.Content) { RenderTypedContent(html, item, inline: false); } html.AppendLine("
"); } break; case BlogPckt.Block.Table.TypeId: var table = BlogPckt.Block.Table.FromJson(element); if (table is not null) { RenderTable(html, table); } break; case BlogPckt.Block.HorizontalRule.TypeId: html.AppendLine("
"); break; case BlogPckt.Block.HardBreak.TypeId: html.AppendLine("
"); break; case BlogPckt.Block.Website.TypeId: var website = BlogPckt.Block.Website.FromJson(element); if (website is not null) { html.AppendLine("
"); html.AppendLine($"{WebUtility.HtmlEncode(website.Title ?? website.Src)}"); if (website.Description is not null) { html.AppendLine($"

{WebUtility.HtmlEncode(website.Description)}

"); } html.AppendLine("
"); } break; case BlogPckt.Block.BlueskyEmbed.TypeId: var bskyEmbed = BlogPckt.Block.BlueskyEmbed.FromJson(element); if (bskyEmbed?.PostRef is not null) { html.AppendLine($"
"); html.AppendLine("

View on Bluesky

"); html.AppendLine("
"); } break; case BlogPckt.Block.Iframe.TypeId: var iframe = BlogPckt.Block.Iframe.FromJson(element); if (iframe is not null) { var heightAttr = iframe.Height.HasValue ? $" height=\"{iframe.Height.Value}\"" : string.Empty; html.AppendLine($""); } break; case BlogPckt.Block.Gallery.TypeId: var gallery = BlogPckt.Block.Gallery.FromJson(element); if (gallery is not null) { html.AppendLine($"
"); } break; case BlogPckt.Block.Mention.TypeId: var mention = BlogPckt.Block.Mention.FromJson(element); if (mention is not null) { html.AppendLine($"@{WebUtility.HtmlEncode(mention.Handle.ToString())}"); } break; case BlogPckt.Block.TaskList.TypeId: var taskList = BlogPckt.Block.TaskList.FromJson(element); if (taskList is not null) { RenderTaskList(html, taskList); } break; } } static void RenderTypedContent(StringBuilder html, object content, bool inline) { switch (content) { case BlogPckt.Block.Text text: if (inline) { html.Append(RenderRichText(text.Plaintext, text.Facets)); } else { html.AppendLine($"

{RenderRichText(text.Plaintext, text.Facets)}

"); } break; case BlogPckt.Block.BulletList bulletList: RenderBulletList(html, bulletList); break; case BlogPckt.Block.OrderedList orderedList: RenderOrderedList(html, orderedList); break; } } static void RenderBulletList(StringBuilder html, BlogPckt.Block.BulletList list) { html.AppendLine(""); } static void RenderOrderedList(StringBuilder html, BlogPckt.Block.OrderedList list) { var startAttr = list.Start.HasValue && list.Start.Value != 1 ? $" start=\"{list.Start.Value}\"" : string.Empty; html.AppendLine($""); foreach (var item in list.Content) { html.Append("
  • "); foreach (var c in item.Content) { RenderTypedContent(html, c, inline: true); } html.AppendLine("
  • "); } html.AppendLine(""); } static void RenderTable(StringBuilder html, BlogPckt.Block.Table table) { html.AppendLine(""); foreach (var row in table.Content) { html.AppendLine(""); foreach (var cell in row.Content) { switch (cell) { case BlogPckt.Block.TableHeader header: var thColspan = header.Colspan is > 1 ? $" colspan=\"{header.Colspan.Value}\"" : string.Empty; var thRowspan = header.Rowspan is > 1 ? $" rowspan=\"{header.Rowspan.Value}\"" : string.Empty; html.Append($""); foreach (var c in header.Content) { if (c is BlogPckt.Block.Text t) { html.Append(RenderRichText(t.Plaintext, t.Facets)); } } html.AppendLine(""); break; case BlogPckt.Block.TableCell tableCell: var tdColspan = tableCell.Colspan is > 1 ? $" colspan=\"{tableCell.Colspan.Value}\"" : string.Empty; var tdRowspan = tableCell.Rowspan is > 1 ? $" rowspan=\"{tableCell.Rowspan.Value}\"" : string.Empty; html.Append($""); foreach (var c in tableCell.Content) { if (c is BlogPckt.Block.Text t) { html.Append(RenderRichText(t.Plaintext, t.Facets)); } } html.AppendLine(""); break; } } html.AppendLine(""); } html.AppendLine("
    "); } static void RenderTaskList(StringBuilder html, BlogPckt.Block.TaskList taskList) { html.AppendLine(""); } static string RenderRichText(string plaintext, List? facets) { if (string.IsNullOrEmpty(plaintext)) { return string.Empty; } if (facets is null || facets.Count == 0) { return WebUtility.HtmlEncode(plaintext); } var utf8Bytes = Encoding.UTF8.GetBytes(plaintext); var sortedFacets = facets.OrderBy(f => f.Index.ByteStart).ToList(); var sb = new StringBuilder(); long currentByte = 0; foreach (var facet in sortedFacets) { var byteStart = Math.Min(facet.Index.ByteStart, utf8Bytes.Length); var byteEnd = Math.Min(facet.Index.ByteEnd, utf8Bytes.Length); if (byteStart < currentByte) { continue; } if (byteStart > currentByte) { var before = Encoding.UTF8.GetString(utf8Bytes, (int)currentByte, (int)(byteStart - currentByte)); sb.Append(WebUtility.HtmlEncode(before)); } if (byteEnd > byteStart) { var facetText = Encoding.UTF8.GetString(utf8Bytes, (int)byteStart, (int)(byteEnd - byteStart)); var encoded = WebUtility.HtmlEncode(facetText); foreach (var feature in facet.Features) { encoded = feature switch { BlogPckt.Richtext.FacetBold => $"{encoded}", BlogPckt.Richtext.FacetItalic => $"{encoded}", BlogPckt.Richtext.FacetCode => $"{encoded}", BlogPckt.Richtext.FacetStrikethrough => $"{encoded}", BlogPckt.Richtext.FacetUnderline => $"{encoded}", BlogPckt.Richtext.FacetHighlight => $"{encoded}", BlogPckt.Richtext.FacetLink link => $"{encoded}", BlogPckt.Richtext.FacetDidMention didMention => $"{encoded}", BlogPckt.Richtext.FacetAtMention atMention => $"{encoded}", _ => encoded, }; } sb.Append(encoded); } currentByte = byteEnd; } if (currentByte < utf8Bytes.Length) { var remaining = Encoding.UTF8.GetString(utf8Bytes, (int)currentByte, utf8Bytes.Length - (int)currentByte); sb.Append(WebUtility.HtmlEncode(remaining)); } return sb.ToString(); } }