style: improve layout of blog/article

This commit is contained in:
Oliver Booth 2023-08-08 00:28:33 +01:00
parent e5f01f66a9
commit 12309c0bf1
Signed by: oliverbooth
GPG Key ID: 725DB725A0D9EE61
2 changed files with 38 additions and 13 deletions

View File

@ -1,9 +1,32 @@
@page "/blog/{year:int}/{month:int}/{day:int}/{slug}"
@using Humanizer
@model OliverBooth.Pages.Blog.Article
@if (Model.Post is { } post)
{
<nav style="--bs-breadcrumb-divider: '>';" aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item">
<a asp-page="/blog/index">Blog</a>
</li>
<li class="breadcrumb-item active" aria-current="page">@post.Title</li>
</ol>
</nav>
<h1>@post.Title</h1>
<p class="text-muted">@post.Published.ToString("MMMM dd, yyyy") &bull; @Model.Author?.Name</p>
@Html.Raw(Model.SanitizeContent(post.Body))
}
<p class="text-muted">
<img class="blog-author-icon" src="https://gravatar.com/avatar/@Model.Author?.AvatarHash?s=28">
@Model.Author?.Name
&bull;
<abbr data-bs-toggle="tooltip" data-bs-title="@post.Published.ToString("f")">
@post.Published.Humanize()
</abbr>
</p>
<article>
@Html.Raw(Model.SanitizeContent(post.Body))
</article>
<hr>
}

View File

@ -1,4 +1,4 @@
using Humanizer;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using OliverBooth.Data;
@ -15,13 +15,13 @@ public class Article : PageModel
_dbContextFactory = dbContextFactory;
}
public Author? Author { get; private set; }
public Author Author { get; private set; }
public BlogPost? Post { get; private set; }
public BlogPost Post { get; private set; } = new();
public string SanitizeContent(string content)
{
content = content.Replace("<more>", string.Empty);
content = content.Replace("<!--more-->", string.Empty);
while (content.Contains("\n\n"))
{
@ -31,20 +31,22 @@ public class Article : PageModel
return Markdig.Markdown.ToHtml(content.Trim());
}
public void OnGet(int year, int month, string slug)
public IActionResult OnGet(int year, int month, int day, string slug)
{
using BlogContext context = _dbContextFactory.CreateDbContext();
Post = context.BlogPosts.FirstOrDefault(p => p.Published.Year == year &&
p.Published.Month == month &&
p.Slug == slug);
p.Published.Day == day &&
p.Slug == slug)!;
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (Post is null)
{
Response.StatusCode = 404;
return NotFound();
}
else
{
Author = context.Authors.FirstOrDefault(a => a.Id == Post.AuthorId);
}
Author = context.Authors.FirstOrDefault(a => a.Id == Post.AuthorId)!;
return Page();
}
}