chore centralized OpenGraph logic

This commit is contained in:
Mia Rose Winter 2024-09-18 13:37:11 +02:00
parent 8f4518fef6
commit f5949b1bca
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
4 changed files with 68 additions and 52 deletions

View file

@ -0,0 +1,63 @@
@using Microsoft.Extensions.Options
@using Wave.Data
@using Wave.Utilities
@inject IOptions<Customization> Customizations
@inject IOptions<Features> Features
@inject NavigationManager Navigation
@if (Subject is null) {
<meta property="og:type" content="website">
<meta property="og:title" content="@Customizations.Value.AppName">
<meta property="og:site_name" content="@Customizations.Value.AppName">
<meta property="og:url" content="@Navigation.BaseUri">
@if (!string.IsNullOrWhiteSpace(Customizations.Value.LogoLink)) {
<meta property="og:image" content="@Customizations.Value.LogoLink">
} else {
<meta property="og:image" content="@Navigation.ToAbsoluteUri("/img/logo.png")">
}
@if (!string.IsNullOrWhiteSpace(Customizations.Value.AppDescription)) {
<meta name="description" content="@Customizations.Value.AppDescription">
<meta property="og:description" content="@Customizations.Value.AppDescription">
}
@if (Features.Value.Rss) {
<link rel="alternate" type="application/rss+xml" title="RSS Feed on @Customizations.Value.AppName" href="@Navigation.ToAbsoluteUri("/rss/rss.xml")">
<link rel="alternate" type="application/atom+xml" title="Atom RSS Feed on @Customizations.Value.AppName" href="@Navigation.ToAbsoluteUri("/rss/atom.xml")">
}
} else if (Subject is Article article) {
<meta property="og:title" content="@article.Title">
<meta property="og:description" content="@string.Format(Localizer!["Meta_Description"], Customizations.Value.AppName, article.BodyPlain[..Math.Min(80, article.BodyPlain.Length)] + "... ")">
<meta property="og:url" content="@Navigation.ToAbsoluteUri("/article/" + article.Id)">
<meta property="og:image" content="@Navigation.ToAbsoluteUri("/api/user/pfp/" + article.Author.Id)">
<meta property="og:type" content="article">
<meta property="og:article:author" content="@article.Author.Name">
<meta property="og:article:published_time" content="@article.PublishDate.ToString("u")">
@if (article.LastModified.HasValue) {
<meta property="og:article:modified_time" content="@article.LastModified.Value.ToString("u")">
}
<meta property="og:site_name" content="@Customizations.Value.AppName">
@if (Features.Value.Rss) {
<link rel="alternate" type="application/rss+xml" title="RSS Feed on @Customizations.Value.AppName" href="@Navigation.ToAbsoluteUri("/rss/rss.xml")">
<link rel="alternate" type="application/atom+xml" title="Atom RSS Feed on @Customizations.Value.AppName" href="@Navigation.ToAbsoluteUri("/rss/atom.xml")">
}
<link rel="canonical" href="@ArticleUtilities.GenerateArticleLink(article, new Uri(Navigation.BaseUri))" />
} else if (Subject is ApplicationUser user) {
<meta property="og:title" content="@string.Format(Localizer!["Meta_OpenGraph_Title"], user.Name, Customizations.Value.AppName)">
<meta property="og:description" content="@string.Format(Localizer["Meta_Description"], user.Name, Customizations.Value.AppName)">
<meta property="og:type" content="profile">
<meta property="og:image" content="@Navigation.ToAbsoluteUri("/api/user/pfp/" + user.Id)">
<meta property="og:site_name" content="@Customizations.Value.AppName">
@if (Features.Value.Rss) {
<link rel="alternate" type="application/rss+xml" title="RSS Feed on @Customizations.Value.AppName | User @user.FullName" href="@Navigation.ToAbsoluteUri("/rss/rss.xml?author=@user.Id")">
<link rel="alternate" type="application/atom+xml" title="Atom RSS Feed on @Customizations.Value.AppName | User @user.FullName" href="@Navigation.ToAbsoluteUri("/rss/atom.xml?author=@user.Id")">
}
}
@code {
[Parameter]
public object? Subject { get; set; }
[Parameter]
public IStringLocalizer? Localizer { get; set; }
}

View file

@ -28,26 +28,7 @@
<meta name="author" content="@Article.Author.Name">
<meta name="description" content="@string.Format(Localizer["Meta_Description"], Customizations.Value.AppName, Article.BodyPlain[..Math.Min(80, Article.BodyPlain.Length)] + "... ")">
<!-- Open Graph -->
<meta property="og:title" content="@Article.Title">
<meta property="og:description" content="@string.Format(Localizer["Meta_Description"], Customizations.Value.AppName, Article.BodyPlain[..Math.Min(80, Article.BodyPlain.Length)] + "... ")">
<meta property="og:url" content="@Navigation.ToAbsoluteUri("/article/" + Article.Id)">
<meta property="og:image" content="@Navigation.ToAbsoluteUri("/api/user/pfp/" + Article.Author.Id)">
<meta property="og:type" content="article">
<meta property="og:article:author" content="@Article.Author.Name">
<meta property="og:article:published_time" content="@Article.PublishDate.ToString("u")">
@if (Article.LastModified.HasValue) {
<meta property="og:article:modified_time" content="@Article.LastModified.Value.ToString("u")">
}
<meta property="og:site_name" content="@Customizations.Value.AppName">
@if (Features.Value.Rss) {
<link rel="alternate" type="application/rss+xml" title="RSS Feed on @Customizations.Value.AppName" href="/rss/rss.xml">
<link rel="alternate" type="application/atom+xml" title="Atom RSS Feed on @Customizations.Value.AppName" href="/rss/atom.xml">
}
@if (Article is not null) {
<link rel="canonical" href="@ArticleUtilities.GenerateArticleLink(Article, new Uri(Navigation.BaseUri))" />
}
<OpenGraph Subject="Article" Localizer="Localizer" />
</HeadContent>
}

View file

@ -8,28 +8,12 @@
@inject IOptions<Customization> Customizations
@inject IOptions<Features> Features
@inject NavigationManager Navigation
@inject IDbContextFactory<ApplicationDbContext> ContextFactory;
@inject IDbContextFactory<ApplicationDbContext> ContextFactory
@inject IStringLocalizer<Home> Localizer
@inject IMessageDisplay Message
<HeadContent>
<meta property="og:type" content="website">
<meta property="og:title" content="@Customizations.Value.AppName">
<meta property="og:site_name" content="@Customizations.Value.AppName">
<meta property="og:url" content="@Navigation.BaseUri">
@if (!string.IsNullOrWhiteSpace(Customizations.Value.LogoLink)) {
<meta property="og:image" content="@Customizations.Value.LogoLink">
} else {
<meta property="og:image" content="@Navigation.ToAbsoluteUri("/img/logo.png")">
}
@if (!string.IsNullOrWhiteSpace(Customizations.Value.AppDescription)) {
<meta name="description" content="@Customizations.Value.AppDescription">
<meta property="og:description" content="@Customizations.Value.AppDescription">
}
@if (Features.Value.Rss) {
<link rel="alternate" type="application/rss+xml" title="RSS Feed on @Customizations.Value.AppName" href="/rss/rss.xml">
<link rel="alternate" type="application/atom+xml" title="Atom RSS Feed on @Customizations.Value.AppName" href="/rss/atom.xml">
}
<OpenGraph />
@if (Page >= TotalPages) {
<meta name="robots" content="noindex">
@ -40,7 +24,7 @@
}
</HeadContent>
<PageTitle>@(Customizations.Value.AppName ?? "Wave")</PageTitle>
<PageTitle>@(Customizations.Value.AppName)</PageTitle>
<div class="grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-4 xl:grid-rows-4 gap-4">
<div class="sm:col-span-2 flex flex-col">

View file

@ -4,10 +4,8 @@
@using Microsoft.Extensions.Options
@using Wave.Utilities
@inject NavigationManager Navigation
@inject IDbContextFactory<ApplicationDbContext> ContextFactory
@inject IOptions<Customization> Customizations
@inject IOptions<Features> Features
@inject IStringLocalizer<UserView> Localizer
@inject IMessageDisplay Message
@ -16,17 +14,7 @@
<meta name="author" content="@User.Name">
<meta name="description" content="@string.Format(Localizer["Meta_Description"], User.Name, Customizations.Value.AppName)">
<!-- Open Graph -->
<meta property="og:title" content="@string.Format(Localizer["Meta_OpenGraph_Title"], User.Name, Customizations.Value.AppName)">
<meta property="og:description" content="@string.Format(Localizer["Meta_Description"], User.Name, Customizations.Value.AppName)">
<meta property="og:type" content="profile">
<meta property="og:image" content="@Navigation.ToAbsoluteUri("/api/user/pfp/" + User.Id)">
<meta property="og:site_name" content="@Customizations.Value.AppName">
@if (Features.Value.Rss) {
<link rel="alternate" type="application/rss+xml" title="RSS Feed on @Customizations.Value.AppName | User @User.FullName" href="/rss/rss.xml?author=@User.Id">
<link rel="alternate" type="application/atom+xml" title="Atom RSS Feed on @Customizations.Value.AppName | User @User.FullName" href="/rss/atom.xml?author=@User.Id">
}
<OpenGraph Subject="User" Localizer="Localizer" />
}
@if (User is null) {
<meta name="robots" content="noindex">