refactor: determine disqus param in model; reduce duplication

This commit is contained in:
Oliver Booth 2023-08-08 12:39:49 +01:00
parent 95a5a9e93b
commit 9de1a84446
Signed by: oliverbooth
GPG Key ID: 725DB725A0D9EE61
5 changed files with 63 additions and 17 deletions

View File

@ -1,4 +1,6 @@
namespace OliverBooth.Data.Blog;
using SmartFormat;
namespace OliverBooth.Data.Blog;
/// <summary>
/// Represents a blog post.
@ -23,6 +25,24 @@ public sealed class BlogPost : IEquatable<BlogPost>
/// <value><see langword="true" /> if comments are enabled; otherwise, <see langword="false" />.</value>
public bool EnableComments { get; set; } = true;
/// <summary>
/// Gets or sets the base URL of the Disqus comments for the blog post.
/// </summary>
/// <value>The Disqus base URL.</value>
public string? DisqusDomain { get; set; }
/// <summary>
/// Gets or sets the identifier of the Disqus comments for the blog post.
/// </summary>
/// <value>The Disqus identifier.</value>
public string? DisqusIdentifier { get; set; }
/// <summary>
/// Gets or sets the URL path of the Disqus comments for the blog post.
/// </summary>
/// <value>The Disqus URL path.</value>
public string? DisqusPath { get; set; }
/// <summary>
/// Gets the ID of the blog post.
/// </summary>
@ -87,4 +107,37 @@ public sealed class BlogPost : IEquatable<BlogPost>
{
return Id;
}
/// <summary>
/// Gets the Disqus identifier for the blog post.
/// </summary>
/// <returns>The Disqus identifier.</returns>
public string GetDisqusIdentifier()
{
return string.IsNullOrWhiteSpace(DisqusIdentifier) ? $"post-{Id}" : Smart.Format(DisqusIdentifier, this);
}
/// <summary>
/// Gets the Disqus domain for the blog post.
/// </summary>
/// <returns>The Disqus domain.</returns>
public string GetDisqusDomain()
{
return string.IsNullOrWhiteSpace(DisqusDomain)
? "https://oliverbooth.dev/blog"
: Smart.Format(DisqusDomain, this);
}
/// <summary>
/// Gets the Disqus URL for the blog post.
/// </summary>
/// <returns>The Disqus URL.</returns>
public string GetDisqusUrl()
{
string path = string.IsNullOrWhiteSpace(DisqusPath)
? $"{Published:yyyy/MM/dd}/{Slug}/"
: Smart.Format(DisqusPath, this);
return $"{GetDisqusDomain()}/{path}";
}
}

View File

@ -25,5 +25,8 @@ internal sealed class BlogPostConfiguration : IEntityTypeConfiguration<BlogPost>
builder.Property(e => e.IsRedirect).IsRequired();
builder.Property(e => e.RedirectUrl).IsRequired(false);
builder.Property(e => e.EnableComments).IsRequired();
builder.Property(e => e.DisqusDomain).IsRequired(false);
builder.Property(e => e.DisqusIdentifier).IsRequired(false);
builder.Property(e => e.DisqusPath).IsRequired(false);
}
}

View File

@ -21,6 +21,7 @@
<PackageReference Include="NLog" Version="5.2.3"/>
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.3"/>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0"/>
<PackageReference Include="SmartFormat.NET" Version="3.2.2"/>
<PackageReference Include="X10D" Version="3.2.2"/>
<PackageReference Include="X10D.Hosting" Version="3.2.2"/>
</ItemGroup>

View File

@ -1,4 +1,4 @@
@page "/blog/{year:int}/{month:int}/{day:int}/{slug}"
@page "/blog/{year:int}/{month:int}/{day:int}/{slug}"
@using Humanizer
@model OliverBooth.Pages.Blog.Article
@ -7,13 +7,6 @@
return;
}
@{
bool isLegacyPost = Model.IsWordPressLegacyPost;
string disqusDomain = isLegacyPost ? "https://blog.oliverbooth.dev" : "https://oliverbooth.dev/blog";
string disqusId = isLegacyPost ? $"{post.WordPressId} {disqusDomain}/?p={post.WordPressId}" : post.Id.ToString();
var disqusUrl = $"{disqusDomain}/{post.Published:yyyy/MM/dd}/{post.Slug}/";
}
<nav style="--bs-breadcrumb-divider: '>';" aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item">
@ -41,7 +34,7 @@
@if (post.EnableComments)
{
<span>&bull;</span>
<a href="#disqus_thread" data-disqus-identifier="@disqusId">0 Comments</a>
<a href="#disqus_thread" data-disqus-identifier="@post.GetDisqusIdentifier()">0 Comments</a>
}
</p>
@ -56,8 +49,8 @@
<div id="disqus_thread"></div>
<script>
var disqus_config = function () {
this.page.url = "@disqusUrl";
this.page.identifier = "@disqusId";
this.page.url = "@post.GetDisqusUrl()";
this.page.identifier = "@post.GetDisqusIdentifier()";
this.page.title = "@post.Title";
this.page.postId = "@(post.WordPressId ?? post.Id)";
};

View File

@ -15,10 +15,6 @@
var month = published.ToString("MM");
var day = published.ToString("dd");
bool isLegacyPost = post.WordPressId is not null;
string disqusDomain = isLegacyPost ? "https://blog.oliverbooth.dev" : "https://oliverbooth.dev/blog";
string disqusId = isLegacyPost ? $"{post.WordPressId} {disqusDomain}/?p={post.WordPressId}" : post.Id.ToString();
<div class="card blog-card" style="margin-bottom: 50px;">
<div class="card-body">
<h2>
@ -46,7 +42,7 @@
asp-route-month="@month"
asp-route-day="@day"
asp-route-slug="@post.Slug"
asp-fragment="disqus_thread" data-disqus-identifier="@disqusId">
asp-fragment="disqus_thread" data-disqus-identifier="@post.GetDisqusIdentifier()">
0 Comments
</a>
}