Add support for Markdown with syntax highlighting
This commit is contained in:
parent
32eef104fb
commit
19818ef69e
5 changed files with 46 additions and 23 deletions
22
Website.Tests/VIewModels/BlogPostViewModelTests.cs
Normal file
22
Website.Tests/VIewModels/BlogPostViewModelTests.cs
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
using FluentAssertions;
|
||||||
|
using NSubstitute;
|
||||||
|
using Website.Data.States;
|
||||||
|
using Website.Models;
|
||||||
|
using Website.ViewModels;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Website.Tests.VIewModels
|
||||||
|
{
|
||||||
|
public class BlogPostViewModelTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Content_WithMarkdownContent_ReturnsHtml() {
|
||||||
|
var state = new BlogPostState {
|
||||||
|
Post_Content="# header"
|
||||||
|
};
|
||||||
|
var post = new BlogPost(state);
|
||||||
|
var vm = new BlogPostViewModel(post);
|
||||||
|
vm.Content.Should().Be("<h1>header</h1>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
|
using Markdig;
|
||||||
|
using Markdig.SyntaxHighlighting;
|
||||||
using Website.Models;
|
using Website.Models;
|
||||||
|
|
||||||
namespace Website.ViewModels
|
namespace Website.ViewModels
|
||||||
|
@ -8,11 +10,20 @@ namespace Website.ViewModels
|
||||||
public BlogPostViewModel(BlogPost blogPost)
|
public BlogPostViewModel(BlogPost blogPost)
|
||||||
{
|
{
|
||||||
Title = blogPost.Title;
|
Title = blogPost.Title;
|
||||||
Content = blogPost.Content;
|
Content = Markdown.ToHtml(blogPost.Content).Trim();
|
||||||
Timestamp = blogPost.Timestamp;
|
Timestamp = blogPost.Timestamp;
|
||||||
Url = blogPost.Url;
|
Url = blogPost.Url;
|
||||||
|
|
||||||
|
Content = Markdown.ToHtml(blogPost.Content, GetPipeline()).Trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private MarkdownPipeline GetPipeline()=>new MarkdownPipelineBuilder()
|
||||||
|
//.UseAdvancedExtensions()
|
||||||
|
.UseAutoLinks()
|
||||||
|
.UseSoftlineBreakAsHardlineBreak()
|
||||||
|
.UseSyntaxHighlighting()
|
||||||
|
.Build();
|
||||||
|
|
||||||
public string Title { get; private set; }
|
public string Title { get; private set; }
|
||||||
public string Content { get; private set; }
|
public string Content { get; private set; }
|
||||||
public DateTime Timestamp { get; private set; }
|
public DateTime Timestamp { get; private set; }
|
||||||
|
|
|
@ -14,6 +14,6 @@
|
||||||
<hr />
|
<hr />
|
||||||
<h2>Latest Blog Post</h2>
|
<h2>Latest Blog Post</h2>
|
||||||
<h3><a href="/blog/view/@Model.Url">@Model.Title</a></h3>
|
<h3><a href="/blog/view/@Model.Url">@Model.Title</a></h3>
|
||||||
@Model.Content
|
@Html.Raw(Model.Content)
|
||||||
<small>Posted on @Model.Timestamp.ToString()</small>
|
<small>Posted on @Model.Timestamp.ToString()</small>
|
||||||
<p><a href="/blog/view/@Model.Url>">Read more...</a></p>
|
<p><a href="/blog/view/@Model.Url>">Read more...</a></p>
|
|
@ -10,6 +10,8 @@
|
||||||
<PackageReference Include="BuildBundlerMinifier" Version="2.9.406" />
|
<PackageReference Include="BuildBundlerMinifier" Version="2.9.406" />
|
||||||
<PackageReference Include="dapper" Version="1.60.9" />
|
<PackageReference Include="dapper" Version="1.60.9" />
|
||||||
<PackageReference Include="dapper.contrib" Version="1.60.1" />
|
<PackageReference Include="dapper.contrib" Version="1.60.1" />
|
||||||
|
<PackageReference Include="markdig" Version="0.16.0" />
|
||||||
|
<PackageReference Include="Markdig.SyntaxHighlighting" Version="1.1.7" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.App" />
|
<PackageReference Include="Microsoft.AspNetCore.App" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
|
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
|
||||||
<PackageReference Include="mysqlconnector" Version="0.52.0" />
|
<PackageReference Include="mysqlconnector" Version="0.52.0" />
|
||||||
|
|
|
@ -1,29 +1,17 @@
|
||||||
@import "colours.less";
|
@import "colours.less";
|
||||||
|
|
||||||
.code {
|
.editor-colors {
|
||||||
border: 4px solid @control;
|
border: 4px solid #fff;
|
||||||
background: @control2;
|
background: #fff;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
margin: 0;
|
margin-bottom: 10px;
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
|
padding: 6px;
|
||||||
|
|
||||||
ol {
|
pre {
|
||||||
list-style-position: inside;
|
|
||||||
padding: 0;
|
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
tab-size: 4;
|
||||||
|
-moz-tab-size: 4;
|
||||||
li {
|
|
||||||
background: @control;
|
|
||||||
padding: 1px;
|
|
||||||
|
|
||||||
>div {
|
|
||||||
display: inline;
|
|
||||||
}
|
|
||||||
|
|
||||||
&:nth-child(2n) {
|
|
||||||
background: @control2;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue