Skip to content

Commit

Permalink
Swap to Markdown (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
wbaldoumas committed Jan 3, 2022
1 parent f131cfb commit 381f58d
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 24 deletions.
5 changes: 3 additions & 2 deletions src/Coding.Blog/Client/Coding.Blog.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<ItemGroup>
<PackageReference Include="Grpc.Net.Client.Web" Version="2.41.0" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.41.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.0" PrivateAssets="all" />
<PackageReference Include="Markdig" Version="0.26.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.1" PrivateAssets="all" />
<PackageReference Include="TaskTupleAwaiter" Version="2.0.0" />
</ItemGroup>

Expand Down
14 changes: 13 additions & 1 deletion src/Coding.Blog/Client/Pages/PostDetails.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@using Coding.Blog.Engine
@using Coding.Blog.Engine.Clients
@using Coding.Blog.Engine.Extensions
@using Markdig

<div class="container mb-3">
@if (_selectedPost is null)
Expand Down Expand Up @@ -36,7 +37,7 @@
</div>
<div class="row">
<div class="col">
<div>@(new MarkupString(_selectedPost!.Content))</div>
<div>@GeneratePostMarkup(_selectedPost.Content)</div>
</div>
</div>
<div class="row justify-content-between">
Expand Down Expand Up @@ -97,4 +98,15 @@
}

private void NavigateToMain() => _navigationManager.NavigateTo("blog");

private static MarkupString GeneratePostMarkup(string markdown)
{
var pipeline = new MarkdownPipelineBuilder()
.UseAdvancedExtensions()
.Build();

var result = Markdown.ToHtml(markdown, pipeline);

return new MarkupString(result);
}
}
2 changes: 1 addition & 1 deletion src/Coding.Blog/Engine/Clients/CosmicRequestRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ internal static class CosmicRequestRegistry
public static readonly IDictionary<string, CosmicRequest> Requests = new Dictionary<string, CosmicRequest>
{
{ typeof(CosmicBooks).FullName!, new CosmicRequest("books", "title,content,metadata,created_at") },
{ typeof(CosmicPosts).FullName!, new CosmicRequest("posts", "id,slug,title,content,metadata,created_at") },
{ typeof(CosmicPosts).FullName!, new CosmicRequest("posts", "id,slug,title,metadata,created_at") },
};
}
2 changes: 1 addition & 1 deletion src/Coding.Blog/Engine/Mappers/PostMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public override Post Map(CosmicPost source) => new()
Id = source.Id,
Slug = source.Slug,
Title = source.Title,
Content = source.Content,
Content = source.Metadata.Markdown,
DatePublished = Timestamp.FromDateTime(source.DatePublished),
Tags =
{
Expand Down
4 changes: 2 additions & 2 deletions src/Coding.Blog/Engine/Records/CosmicPosts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public record CosmicPost(
[property: JsonProperty("id")] string Id,
[property: JsonProperty("slug")] string Slug,
[property: JsonProperty("title")] string Title,
[property: JsonProperty("content")] string Content,
[property: JsonProperty("created_at")] DateTime DatePublished,
[property: JsonProperty("metadata")] CosmicPostMetadata Metadata
);

public record CosmicPostMetadata(
[property: JsonProperty("hero")] CosmicPostHero Hero,
[property: JsonProperty("tags")] string Tags
[property: JsonProperty("tags")] string Tags,
[property: JsonProperty("markdown")] string Markdown
);

public record CosmicPostHero(
Expand Down
2 changes: 1 addition & 1 deletion src/Coding.Blog/Server/Coding.Blog.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.41.0" />
<PackageReference Include="Grpc.AspNetCore.Web" Version="2.41.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/Tests/Coding.Blog.UnitTests/Coding.Blog.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.2.0" />
<PackageReference Include="FluentAssertions" Version="6.3.0" />
<PackageReference Include="Grpc.Core.Testing" Version="2.42.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
27 changes: 18 additions & 9 deletions src/Tests/Coding.Blog.UnitTests/Mappers/PostMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ public void Map_generates_expected_post()
"12345",
"test-post",
"Test Post",
"This is some content talking about Test Post",
mockDatePublished,
new CosmicPostMetadata(new CosmicPostHero("https://google.com/a", "https://google.com/b"), "tag1, tag2, tag3")
new CosmicPostMetadata(
new CosmicPostHero("https://google.com/a", "https://google.com/b"),
"tag1, tag2, tag3",
"This is some markdown for Test Post"
)
);

var expectedPost = new Post
{
Id = "12345",
Slug = "test-post",
Title = "Test Post",
Content = "This is some content talking about Test Post",
Content = "This is some markdown for Test Post",
DatePublished = Timestamp.FromDateTime(mockDatePublished),
Tags = { new List<string> { "tag1", "tag2", "tag3" } },
Hero = new Hero
Expand Down Expand Up @@ -61,18 +64,24 @@ public void Map_generates_expected_posts()
"12345",
"test-post-a",
"Test Post A",
"This is some content talking about Test Post A",
mockDatePublished,
new CosmicPostMetadata(new CosmicPostHero("https://google.com/a", "https://google.com/b"), string.Empty)
new CosmicPostMetadata(
new CosmicPostHero("https://google.com/a", "https://google.com/b"),
string.Empty,
"This is some markdown talking about Test Post A"
)
);

var cosmicPostB = new CosmicPost(
"123456",
"test-post-b",
"Test Post B",
"This is some content talking about Test Post B",
mockDatePublished,
new CosmicPostMetadata(new CosmicPostHero("https://google.com/b", "https://google.com/c"), "tag1, tag2, tag3")
new CosmicPostMetadata(
new CosmicPostHero("https://google.com/b", "https://google.com/c"),
"tag1, tag2, tag3",
"This is some markdown talking about Test Post B"
)
);

var cosmicPosts = new List<CosmicPost>
Expand All @@ -86,7 +95,7 @@ public void Map_generates_expected_posts()
Id = "12345",
Slug = "test-post-a",
Title = "Test Post A",
Content = "This is some content talking about Test Post A",
Content = "This is some markdown talking about Test Post A",
DatePublished = Timestamp.FromDateTime(mockDatePublished),
Tags = { new List<string>() },
Hero = new Hero
Expand All @@ -101,7 +110,7 @@ public void Map_generates_expected_posts()
Id = "123456",
Slug = "test-post-b",
Title = "Test Post B",
Content = "This is some content talking about Test Post B",
Content = "This is some markdown talking about Test Post B",
DatePublished = Timestamp.FromDateTime(mockDatePublished),
Tags = { new List<string> { "tag1", "tag2", "tag3" } },
Hero = new Hero
Expand Down
17 changes: 12 additions & 5 deletions src/Tests/Coding.Blog.UnitTests/Services/PostsServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,23 @@ public async Task PostsService_generates_expected_response_when_posts_exist()
"12345",
"test-post-a",
"Test Post A",
"This is some content talking about Test Post A",
mockDatePublished,
new CosmicPostMetadata(new CosmicPostHero("https://google.com/a", "https://google.com/b"), string.Empty)
new CosmicPostMetadata(
new CosmicPostHero("https://google.com/a", "https://google.com/b"),
string.Empty,
"This is some markdown talking about Test Post A"
)
),
new(
"123456",
"test-post-b",
"Test Post B",
"This is some content talking about Test Post B",
mockDatePublished,
new CosmicPostMetadata(new CosmicPostHero("https://google.com/b", "https://google.com/c"), string.Empty)
new CosmicPostMetadata(
new CosmicPostHero("https://google.com/b", "https://google.com/c"),
string.Empty,
"This is some markdown talking about Test Post B"
)
)
}
);
Expand All @@ -69,7 +75,7 @@ public async Task PostsService_generates_expected_response_when_posts_exist()
Id = source.Id,
Slug = source.Slug,
Title = source.Title,
Content = source.Content,
Content = source.Metadata.Markdown,
DatePublished = Timestamp.FromDateTime(source.DatePublished),
Hero = new Hero
{
Expand Down Expand Up @@ -138,6 +144,7 @@ public async Task PostsService_generates_expected_response_when_posts_do_not_exi
.GetAsync()
.Returns(mockCosmicPostsClientResponse);

// ReSharper disable once CollectionNeverUpdated.Local
var mockPostMapperResponse = new List<Post>();

_mockPostMapper
Expand Down

0 comments on commit 381f58d

Please sign in to comment.