meta tags refactor

This commit is contained in:
Laura Hausmann 2023-02-03 21:45:07 +01:00
parent 696d9f3f89
commit 6e1a3ed50f
Signed by: zotan
GPG Key ID: D044E84C5BE01605
6 changed files with 51 additions and 14 deletions

View File

@ -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;
}
}

View File

@ -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 {

View File

@ -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 <a>, <i>, <b> and <u> html tags
desc = desc.Trim(); // remove leading and trailing whitespace
desc = Regex.Match(desc, @"<p>([^<].*?)<\/p>").Groups[1].ToString().GetUntil("."); // get first sentence
ViewData["og-desc"] = desc;
#if (DEBUG)
Model.Post.UpdateContentAndMetadata();
#endif

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta http-equiv="X-UA-Compatible" content="chrome=1"/>
<meta name="twitter:card" content="summary"/>
<meta name="og:title" content="zotan.pw >> @ViewData["title"]"/>
<meta name="og:description" content="@ViewData["desc"]"/>
@if (ViewData.ContainsKey("subtitle")) {
<title>zotan.pw >> @ViewData["title"] >> @ViewData["subtitle"]</title>
}
else {
<title>zotan.pw >> @ViewData["title"]</title>
}
<meta name="og:title" content="@ViewData["og-title"]"/>
<meta name="og:description" content="@ViewData["og-desc"]"/>
<meta name="og:site_name" content="@Html.Encode(ViewData["og-sitename"])"/>
<meta name="og:image" content="@ViewData["og-image"]"/>
<title>@title</title>
<link rel="stylesheet" href="~/css/site.css?v=@Utils.LinkVersion"/>
@await RenderSectionAsync("head", false)
<environment include="Production">
<script async defer data-website-id="06fcb20d-431a-4251-b6cb-859d9a7384ee" src="https://evilthirdparty.click/spyinator3000.js"></script>
</environment>
@await RenderSectionAsync("head", false)
</head>
<body>
<header>