diff --git a/Backend/Utils.cs b/Backend/Utils.cs index 194f8fc..b06d9fd 100644 --- a/Backend/Utils.cs +++ b/Backend/Utils.cs @@ -38,4 +38,23 @@ public static class Utils { public override Encoding Encoding { get; } } + + public static string Truncate(this string input, int maxLen, string truncateMarker = "...") { + var markerLength = truncateMarker.Length; + if (maxLen < 1 + markerLength) + maxLen = 1 + markerLength; + return input.Length <= maxLen ? input : input[..(maxLen - markerLength)] + truncateMarker; + } + + public static string GetUntil(this string input, string stopAt) { + if (!string.IsNullOrWhiteSpace(input)) { + if (!input.Contains(stopAt)) + return input; + + var charLocation = input.IndexOf(stopAt, StringComparison.Ordinal); + return input[..++charLocation]; + } + + return string.Empty; + } } diff --git a/Pages/Blog.cshtml b/Pages/Blog.cshtml index b550d84..f2736eb 100644 --- a/Pages/Blog.cshtml +++ b/Pages/Blog.cshtml @@ -2,7 +2,7 @@ @model BlogModel @{ ViewData["title"] = "blog"; - ViewData["desc"] = "Blog of a disabled neurodivergent queer person unhappy with the state of the world."; + ViewData["og-desc"] = "Blog of a disabled neurodivergent queer person unhappy with the state of the world."; } @section head { diff --git a/Pages/BlogPost.cshtml b/Pages/BlogPost.cshtml index 67d15d3..c4d6df8 100644 --- a/Pages/BlogPost.cshtml +++ b/Pages/BlogPost.cshtml @@ -1,4 +1,5 @@ @page "/blog/{post}" +@using System.Text.RegularExpressions @using zotanpw.Backend @model BlogPostModel @@ -8,7 +9,19 @@ ViewData["title"] = "blog"; ViewData["subtitle"] = Model.Post.Shorthand; - ViewData["desc"] = Model.Post.Title; + ViewData["og-sitename"] = $"zotan.pw >> {ViewData["title"]}"; + ViewData["og-title"] = Model.Post.Title; + + if (System.IO.File.Exists($"wwwroot/files/blog/{Model.Post.Shorthand}.webp")) + ViewData["og-image"] = $"/files/blog/{Model.Post.Shorthand}.webp"; + + var desc = Model.Post.Content; + desc = Regex.Replace(desc, @"\s+", " "); // collapse whitespace + desc = Regex.Replace(desc, @"(?i)<\/?[aibu][^>]*>", ""); // remove , , and html tags + desc = desc.Trim(); // remove leading and trailing whitespace + desc = Regex.Match(desc, @"

([^<].*?)<\/p>").Groups[1].ToString().GetUntil("."); // get first sentence + ViewData["og-desc"] = desc; + #if (DEBUG) Model.Post.UpdateContentAndMetadata(); #endif diff --git a/Pages/Index.cshtml b/Pages/Index.cshtml index bb5855b..c5fd660 100644 --- a/Pages/Index.cshtml +++ b/Pages/Index.cshtml @@ -3,7 +3,7 @@ @model IndexModel @{ ViewData["title"] = "home"; - ViewData["desc"] = "Website of a queer software, systems, and network engineer."; + ViewData["og-desc"] = "Website of a queer software, systems, and network engineer."; var travelynx = new Database.DbConn().TravelynxInfo.ToList(); } diff --git a/Pages/NowPlaying.cshtml b/Pages/NowPlaying.cshtml index 76198ba..1a827de 100644 --- a/Pages/NowPlaying.cshtml +++ b/Pages/NowPlaying.cshtml @@ -4,6 +4,7 @@ @{ ViewData["title"] = "now playing"; + ViewData["og-desc"] = "Here you can see what kind of music I've been listening to lately."; var db = new Database.DbConn(); var albums = db.AlbumHistory.OrderByDescending(p => p.DateTime).Take(30); } @@ -25,4 +26,4 @@ } - \ No newline at end of file + diff --git a/Pages/Shared/_Layout.cshtml b/Pages/Shared/_Layout.cshtml index 38ff961..32ac688 100644 --- a/Pages/Shared/_Layout.cshtml +++ b/Pages/Shared/_Layout.cshtml @@ -1,23 +1,27 @@ @using zotanpw.Backend +@{ + var title = ViewData.ContainsKey("subtitle") ? $"zotan.pw >> {ViewData["title"]} >> {ViewData["subtitle"]}" : $"zotan.pw >> {ViewData["title"]}"; + if (string.IsNullOrEmpty(ViewData["og-title"] as string)) + ViewData["og-title"] = title; +} - + - - - @if (ViewData.ContainsKey("subtitle")) { - zotan.pw >> @ViewData["title"] >> @ViewData["subtitle"] - } - else { - zotan.pw >> @ViewData["title"] - } + + + + + + @title + + @await RenderSectionAsync("head", false) - @await RenderSectionAsync("head", false)