using System; using Markdig; using Pek.Markdig.HighlightJs; using Website.Models; namespace Website.ViewModels { public class BlogPostViewModel { public BlogPostViewModel(BlogPost blogPost, bool preview) { Id = blogPost.Id; Title = blogPost.Title; Timestamp = FormatTimestamp(blogPost.Timestamp); Url = blogPost.Url; Content = preview ? blogPost.Draft : blogPost.Content; } private static string GetDaySuffix(int day) { switch (day) { case 1: case 21: case 31: return @"\s\t"; case 2: case 22: return @"\n\d"; case 3: case 23: return @"\r\d"; default: return @"\t\h"; } } private static string FormatTimestamp(DateTime timestamp) { var suffix = GetDaySuffix(timestamp.Day); return timestamp.ToString($@"dddd \t\h\e d{suffix} \o\f MMMM yyyy"); } private static MarkdownPipeline GetPipeline() => new MarkdownPipelineBuilder() //.UseAdvancedExtensions() .UseAutoLinks() .UseSoftlineBreakAsHardlineBreak() .UseHighlightJs() .Build(); public int Id { get; } public string Title { get; } public string Content { get; protected set; } public string Timestamp { get; } public string Url { get; } public string ContentHtml => Markdown.ToHtml(Content, GetPipeline()).Trim(); } }