2023-08-06 15:57:23 +01:00
|
|
|
|
using Humanizer;
|
2023-08-08 01:30:32 +01:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
|
|
|
using OliverBooth.Data.Blog;
|
2023-08-08 11:35:22 +01:00
|
|
|
|
using OliverBooth.Services;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
|
|
|
|
|
namespace OliverBooth.Pages.Blog;
|
|
|
|
|
|
|
|
|
|
public class Index : PageModel
|
|
|
|
|
{
|
2023-08-08 11:35:22 +01:00
|
|
|
|
private readonly BlogService _blogService;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
|
2023-08-08 11:35:22 +01:00
|
|
|
|
public Index(BlogService blogService)
|
2023-08-06 15:57:23 +01:00
|
|
|
|
{
|
2023-08-08 11:35:22 +01:00
|
|
|
|
_blogService = blogService;
|
2023-08-06 15:57:23 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string SanitizeContent(string content)
|
|
|
|
|
{
|
2023-08-08 01:30:32 +01:00
|
|
|
|
content = content.Replace("<!--more-->", string.Empty);
|
2023-08-06 15:57:23 +01:00
|
|
|
|
|
|
|
|
|
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<char> span = content.AsSpan();
|
2023-08-08 01:30:32 +01:00
|
|
|
|
int moreIndex = span.IndexOf("<!--more-->", StringComparison.Ordinal);
|
2023-08-06 15:57:23 +01:00
|
|
|
|
trimmed = moreIndex != -1 || span.Length > 256;
|
|
|
|
|
return moreIndex != -1 ? span[..moreIndex].Trim().ToString() : content.Truncate(256);
|
|
|
|
|
}
|
2023-08-08 01:30:32 +01:00
|
|
|
|
|
|
|
|
|
public IActionResult OnGet([FromQuery(Name = "p")] int? postId = null)
|
2023-08-06 15:57:23 +01:00
|
|
|
|
{
|
2023-08-08 11:35:22 +01:00
|
|
|
|
if (!postId.HasValue) return Page();
|
|
|
|
|
if (!_blogService.TryGetWordPressBlogPost(postId.Value, out BlogPost? post)) return NotFound();
|
2023-08-08 01:30:32 +01:00
|
|
|
|
|
2023-08-08 11:35:22 +01:00
|
|
|
|
var route = new
|
2023-08-08 01:30:32 +01:00
|
|
|
|
{
|
2023-08-08 11:35:22 +01:00
|
|
|
|
year = post.Published.Year,
|
|
|
|
|
month = post.Published.Month,
|
|
|
|
|
day = post.Published.Day,
|
|
|
|
|
slug = post.Slug
|
|
|
|
|
};
|
|
|
|
|
return Redirect(Url.Page("/Blog/Article", route)!);
|
2023-08-06 15:57:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|