Fix issue with blog preview when content under 1000 chars.

This commit is contained in:
Robert Marshall 2019-08-26 15:04:32 +01:00
parent 3a8aedd653
commit 0f64014b30
6 changed files with 38 additions and 10 deletions

View file

@ -0,0 +1,31 @@
using FluentAssertions;
using Website.Data.States;
using Website.Models;
using Website.ViewModels;
using Xunit;
namespace Website.Tests.VIewModels
{
public class BlogPostPreviewViewModelTests
{
[Fact]
public void Constructor_WithContentOver1000Characters_LimitsContentTo1000Chars() {
var state = new BlogPostState {
Post_Content = new string('a', 1001)
};
var post = new BlogPost(state);
var vm = new BlogPostPreviewViewModel(post);
vm.Content.Length.Should().Be(1000);
}
[Fact]
public void Constructor_WithContentUnder1000Characters_ContentIsIdenticalLength() {
var state = new BlogPostState {
Post_Content = new string('a', 900)
};
var post = new BlogPost(state);
var vm = new BlogPostPreviewViewModel(post);
vm.Content.Length.Should().Be(900);
}
}
}

View file

@ -10,13 +10,13 @@ namespace Website.Tests.VIewModels
public class BlogPostViewModelTests public class BlogPostViewModelTests
{ {
[Fact] [Fact]
public void Content_WithMarkdownContent_ReturnsHtml() { public void ContentHtml_WithMarkdownContent_ReturnsHtml() {
var state = new BlogPostState { var state = new BlogPostState {
Post_Content="# header" Post_Content="# header"
}; };
var post = new BlogPost(state); var post = new BlogPost(state);
var vm = new BlogPostViewModel(post); var vm = new BlogPostViewModel(post);
vm.Content.Should().Be("<h1>header</h1>"); vm.ContentHtml.Should().Be("<h1>header</h1>");
} }
[Fact] [Fact]

View file

@ -5,9 +5,6 @@ namespace Website.ViewModels
{ {
public class BlogPostPreviewViewModel:BlogPostViewModel public class BlogPostPreviewViewModel:BlogPostViewModel
{ {
public BlogPostPreviewViewModel(BlogPost blogPost):base(blogPost) public BlogPostPreviewViewModel(BlogPost blogPost) : base(blogPost) => Content = Content.Length < 1000 ? Content : Content.Substring(0, 1000);
{
Content = Markdown.ToHtml(blogPost.Content.Substring(0, 1000), GetPipeline()).Trim();
}
} }
} }

View file

@ -12,8 +12,7 @@ namespace Website.ViewModels
Title = blogPost.Title; Title = blogPost.Title;
Timestamp = FormatTimestamp(blogPost.Timestamp); Timestamp = FormatTimestamp(blogPost.Timestamp);
Url = blogPost.Url; Url = blogPost.Url;
Content = blogPost.Content;
Content = Markdown.ToHtml(blogPost.Content, GetPipeline()).Trim();
} }
string GetDaySuffix(int day) { string GetDaySuffix(int day) {
@ -49,5 +48,6 @@ namespace Website.ViewModels
public string Content { get; protected set; } public string Content { get; protected set; }
public string Timestamp { get; private set; } public string Timestamp { get; private set; }
public string Url { get; private set; } public string Url { get; private set; }
public object ContentHtml => Markdown.ToHtml(Content, GetPipeline()).Trim();
} }
} }

View file

@ -4,6 +4,6 @@
ViewData["Title"] = Model.Title; ViewData["Title"] = Model.Title;
} }
@Html.Raw(Model.Content) @Html.Raw(Model.ContentHtml)
<small>Posted on @Model.Timestamp</small> <small>Posted on @Model.Timestamp</small>

View file

@ -22,7 +22,7 @@
@foreach (var post in Model.Posts) { @foreach (var post in Model.Posts) {
<article> <article>
<h2><a href="/blog/view/@post.Url">@post.Title</a></h2> <h2><a href="/blog/view/@post.Url">@post.Title</a></h2>
@Html.Raw(post.Content) @Html.Raw(post.ContentHtml)
<small>Posted on @post.Timestamp</small> <small>Posted on @post.Timestamp</small>
<p><a href="/blog/view/@post.Url">Read more...</a></p> <p><a href="/blog/view/@post.Url">Read more...</a></p>
</article> </article>