diff --git a/OliverBooth/Pages/Blog/Article.cshtml b/OliverBooth/Pages/Blog/Article.cshtml index f449615..d54ed45 100644 --- a/OliverBooth/Pages/Blog/Article.cshtml +++ b/OliverBooth/Pages/Blog/Article.cshtml @@ -1,6 +1,8 @@ -@page "/blog/{year:int}/{month:int}/{day:int}/{slug}" +@page "/blog/{year:int}/{month:int}/{day:int}/{slug}" @using Humanizer +@using OliverBooth.Services @model OliverBooth.Pages.Blog.Article +@inject BlogService BlogService @if (Model.Post is not { } post) { @@ -39,7 +41,7 @@

- @Html.Raw(Model.SanitizeContent(post.Body)) + @Html.Raw(BlogService.GetContent(post))

diff --git a/OliverBooth/Pages/Blog/Article.cshtml.cs b/OliverBooth/Pages/Blog/Article.cshtml.cs index b6c1086..16a84a5 100644 --- a/OliverBooth/Pages/Blog/Article.cshtml.cs +++ b/OliverBooth/Pages/Blog/Article.cshtml.cs @@ -1,5 +1,4 @@ -using Markdig; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using OliverBooth.Data.Blog; using OliverBooth.Services; @@ -12,17 +11,14 @@ namespace OliverBooth.Pages.Blog; public class Article : PageModel { private readonly BlogService _blogService; - private readonly MarkdownPipeline _markdownPipeline; /// /// Initializes a new instance of the class. /// /// The . - /// The . - public Article(BlogService blogService, MarkdownPipeline markdownPipeline) + public Article(BlogService blogService) { _blogService = blogService; - _markdownPipeline = markdownPipeline; } /// @@ -45,23 +41,6 @@ public class Article : PageModel /// The requested blog post. public BlogPost Post { get; private set; } = null!; - /// - /// Sanitizes the content of the blog post. - /// - /// The content of the blog post. - /// The sanitized content of the blog post. - public string SanitizeContent(string content) - { - content = content.Replace("", string.Empty); - - while (content.Contains("\n\n")) - { - content = content.Replace("\n\n", "\n"); - } - - return Markdown.ToHtml(content.Trim(), _markdownPipeline); - } - public IActionResult OnGet(int year, int month, int day, string slug) { if (!_blogService.TryGetBlogPost(year, month, day, slug, out BlogPost? post)) diff --git a/OliverBooth/Pages/Blog/Index.cshtml b/OliverBooth/Pages/Blog/Index.cshtml index 1bd1a1a..ba7c786 100644 --- a/OliverBooth/Pages/Blog/Index.cshtml +++ b/OliverBooth/Pages/Blog/Index.cshtml @@ -48,7 +48,7 @@ }

-

@Html.Raw(Model.SanitizeContent(Model.TrimContent(post.Body, out bool trimmed)))

+

@Html.Raw(BlogService.GetExcerpt(post, out bool trimmed))

@if (trimmed) diff --git a/OliverBooth/Pages/Blog/Index.cshtml.cs b/OliverBooth/Pages/Blog/Index.cshtml.cs index 0ea63c7..2db453c 100644 --- a/OliverBooth/Pages/Blog/Index.cshtml.cs +++ b/OliverBooth/Pages/Blog/Index.cshtml.cs @@ -1,6 +1,4 @@ -using System.Diagnostics; -using Humanizer; -using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using OliverBooth.Data.Blog; using OliverBooth.Services; @@ -16,26 +14,6 @@ public class Index : PageModel _blogService = blogService; } - public string SanitizeContent(string content) - { - content = content.Replace("", string.Empty); - - while (content.Contains("\n\n")) - { - content = content.Replace("\n\n", "\n"); - } - - return Markdig.Markdown.ToHtml(content.Trim()); - } - - public string TrimContent(string content, out bool trimmed) - { - ReadOnlySpan span = content.AsSpan(); - int moreIndex = span.IndexOf("", StringComparison.Ordinal); - trimmed = moreIndex != -1 || span.Length > 256; - return moreIndex != -1 ? span[..moreIndex].Trim().ToString() : content.Truncate(256); - } - public IActionResult OnGet([FromQuery(Name = "pid")] int? postId = null, [FromQuery(Name = "p")] int? wpPostId = null) { diff --git a/OliverBooth/Services/BlogService.cs b/OliverBooth/Services/BlogService.cs index 0f621f8..57386a8 100644 --- a/OliverBooth/Services/BlogService.cs +++ b/OliverBooth/Services/BlogService.cs @@ -1,4 +1,6 @@ -using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.CodeAnalysis; +using Humanizer; +using Markdig; using Microsoft.EntityFrameworkCore; using OliverBooth.Data; using OliverBooth.Data.Blog; @@ -8,14 +10,17 @@ namespace OliverBooth.Services; public sealed class BlogService { private readonly IDbContextFactory _dbContextFactory; + private readonly MarkdownPipeline _markdownPipeline; /// /// Initializes a new instance of the class. /// /// The . - public BlogService(IDbContextFactory dbContextFactory) + /// The . + public BlogService(IDbContextFactory dbContextFactory, MarkdownPipeline markdownPipeline) { _dbContextFactory = dbContextFactory; + _markdownPipeline = markdownPipeline; } /// @@ -31,6 +36,34 @@ public sealed class BlogService } } + /// + /// Gets the processed content of a blog post. + /// + /// The blog post. + /// The processed content of the blog post. + public string GetContent(BlogPost post) + { + return ProcessContent(post.Body); + } + + /// + /// Gets the processed excerpt of a blog post. + /// + /// The blog post. + /// + /// When this method returns, contains if the content was trimmed; otherwise, + /// . + /// + /// The processed excerpt of the blog post. + public string GetExcerpt(BlogPost post, out bool trimmed) + { + ReadOnlySpan span = post.Body.AsSpan(); + int moreIndex = span.IndexOf("", StringComparison.Ordinal); + trimmed = moreIndex != -1 || span.Length > 256; + string result = moreIndex != -1 ? span[..moreIndex].Trim().ToString() : post.Body.Truncate(256); + return ProcessContent(result); + } + /// /// Attempts to find the author of a blog post. /// @@ -106,4 +139,16 @@ public sealed class BlogService post = context.BlogPosts.FirstOrDefault(p => p.WordPressId == postId); return post is not null; } + + private string ProcessContent(string content) + { + content = content.Replace("", string.Empty); + + while (content.Contains("\n\n")) + { + content = content.Replace("\n\n", "\n"); + } + + return Markdown.ToHtml(content.Trim(), _markdownPipeline); + } }