Set friendly date for blog posts

This commit is contained in:
Robert Marshall 2019-04-28 11:43:05 +01:00
parent fa143f5ab4
commit 035f442ba0
5 changed files with 61 additions and 5 deletions

View file

@ -1,3 +1,4 @@
using System;
using FluentAssertions;
using NSubstitute;
using Website.Data.States;
@ -18,5 +19,38 @@ namespace Website.Tests.VIewModels
var vm = new BlogPostViewModel(post);
vm.Content.Should().Be("<h1>header</h1>");
}
[Fact]
public void Timestamp_OnThe1st_IsFriendlyString() {
var state = new BlogPostState {
Post_Content = "",
Post_Timestamp = new DateTime(2018, 10, 01, 15, 1, 25)
};
var post = new BlogPost(state);
var vm = new BlogPostViewModel(post);
vm.Timestamp.Should().Be("Monday the 1st of October 2018");
}
[Fact]
public void Timestamp_OnThe2nd_IsFriendlyString() {
var state = new BlogPostState {
Post_Content = "",
Post_Timestamp = new DateTime(2018, 10, 02, 15, 1, 25)
};
var post = new BlogPost(state);
var vm = new BlogPostViewModel(post);
vm.Timestamp.Should().Be("Tuesday the 2nd of October 2018");
}
[Fact]
public void Timestamp_OnThe3rd_IsFriendlyString() {
var state = new BlogPostState {
Post_Content = "",
Post_Timestamp = new DateTime(2018, 10, 03, 15, 1, 25)
};
var post = new BlogPost(state);
var vm = new BlogPostViewModel(post);
vm.Timestamp.Should().Be("Wednesday the 3rd of October 2018");
}
}
}

View file

@ -10,12 +10,34 @@ namespace Website.ViewModels
public BlogPostViewModel(BlogPost blogPost)
{
Title = blogPost.Title;
Timestamp = blogPost.Timestamp;
Timestamp = FormatTimestamp(blogPost.Timestamp);
Url = blogPost.Url;
Content = Markdown.ToHtml(blogPost.Content, GetPipeline()).Trim();
}
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 string FormatTimestamp(DateTime timestamp) {
var suffix = GetDaySuffix(timestamp.Day);
return timestamp.ToString($@"dddd \t\h\e d{suffix} \o\f MMMM yyyy");
}
protected MarkdownPipeline GetPipeline()=>new MarkdownPipelineBuilder()
//.UseAdvancedExtensions()
.UseAutoLinks()
@ -25,7 +47,7 @@ namespace Website.ViewModels
public string Title { get; private set; }
public string Content { get; protected set; }
public DateTime Timestamp { get; private set; }
public string Timestamp { get; private set; }
public string Url { get; private set; }
}
}

View file

@ -6,4 +6,4 @@
@Html.Raw(Model.Content)
<small>Posted on @Model.Timestamp.ToString()</small>
<small>Posted on @Model.Timestamp</small>

View file

@ -23,7 +23,7 @@
<article>
<h2><a href="/blog/view/@post.Url">@post.Title</a></h2>
@Html.Raw(post.Content)
<small>Posted on @post.Timestamp.ToString()</small>
<small>Posted on @post.Timestamp</small>
<p><a href="/blog/view/@post.Url">Read more...</a></p>
</article>
}

View file

@ -15,5 +15,5 @@
<h2>Latest Blog Post</h2>
<h3><a href="/blog/view/@Model.Url">@Model.Title</a></h3>
@Html.Raw(Model.Content)
<small>Posted on @Model.Timestamp.ToString()</small>
<small>Posted on @Model.Timestamp</small>
<p><a href="/blog/view/@Model.Url>">Read more...</a></p>