using Markdig;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using OliverBooth.Data.Blog;
using OliverBooth.Services;
namespace OliverBooth.Pages.Blog;
///
/// Represents the page model for the Article page.
///
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)
{
_blogService = blogService;
_markdownPipeline = markdownPipeline;
}
///
/// Gets the author of the post.
///
/// The author of the post.
public Author Author { get; private set; } = null!;
///
/// Gets a value indicating whether the post is a legacy WordPress post.
///
///
/// if the post is a legacy WordPress post; otherwise, .
///
public bool IsWordPressLegacyPost => Post.WordPressId.HasValue;
///
/// Gets the requested blog post.
///
/// 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))
{
Response.StatusCode = 404;
return NotFound();
}
Post = post;
Author = _blogService.TryGetAuthor(post, out Author? author) ? author : null!;
return Page();
}
}