Skip to content

Commit 59bd053

Browse files
committed
Enable giscus
1 parent b3f1fa0 commit 59bd053

File tree

10 files changed

+98
-3
lines changed

10 files changed

+98
-3
lines changed

LinkDotNet.Blog.UnitTests/LinkDotNet.Blog.UnitTests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
<ItemGroup>
4848
<Folder Include="Infrastructure\Persistence" />
4949
</ItemGroup>
50+
51+
<ItemGroup>
52+
<Compile Remove="Web\Shared\Services\LocalStorageServiceTests.cs" />
53+
</ItemGroup>
5054
<PropertyGroup>
5155
<CodeAnalysisRuleSet>..\stylecop.analyzers.ruleset</CodeAnalysisRuleSet>
5256
</PropertyGroup>

LinkDotNet.Blog.Web/AppConfiguration.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using LinkDotNet.Domain;
1+
using LinkDotNet.Blog.Web.Shared.Services;
2+
using LinkDotNet.Domain;
23

34
namespace LinkDotNet.Blog.Web
45
{
@@ -24,6 +25,10 @@ public record AppConfiguration
2425

2526
public bool IsAboutMeEnabled { get; init; }
2627

27-
public ProfileInformation ProfileInformation { get; set; }
28+
public ProfileInformation ProfileInformation { get; init; }
29+
30+
public Giscus Giscus { get; init; }
31+
32+
public bool IsGiscusEnabled { get; init; }
2833
}
2934
}

LinkDotNet.Blog.Web/AppConfigurationFactory.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using LinkDotNet.Domain;
1+
using LinkDotNet.Blog.Web.Shared.Services;
2+
using LinkDotNet.Domain;
23
using Microsoft.Extensions.Configuration;
34

45
namespace LinkDotNet.Blog.Web
@@ -8,6 +9,7 @@ public static class AppConfigurationFactory
89
public static AppConfiguration Create(IConfiguration config)
910
{
1011
var profileInformation = config.GetSection("AboutMeProfileInformation").Get<ProfileInformation>();
12+
var giscus = config.GetSection("Giscus").Get<Giscus>();
1113
var configuration = new AppConfiguration
1214
{
1315
BlogName = config["BlogName"],
@@ -19,6 +21,8 @@ public static AppConfiguration Create(IConfiguration config)
1921
BlogPostsPerPage = int.Parse(config["BlogPostsPerPage"]),
2022
ProfileInformation = profileInformation,
2123
IsAboutMeEnabled = profileInformation != null,
24+
Giscus = giscus,
25+
IsGiscusEnabled = giscus != null,
2226
};
2327

2428
return configuration;

LinkDotNet.Blog.Web/Pages/BlogPostPage.razor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@inject IRepository<BlogPost> blogPostRepository
99
@inject IJSRuntime jsRuntime
1010
@inject IUserRecordService userRecordService
11+
@inject ICommentService commentService
1112
@inherits MarkdownComponentBase
1213

1314
<div class="page">
@@ -40,6 +41,8 @@
4041
</div>
4142
</div>
4243
<Like BlogPost="@BlogPost" OnBlogPostLiked="@UpdateLikes"></Like>
44+
<div class="giscus">
45+
</div>
4346
</div>
4447
</div>
4548
}
@@ -66,6 +69,7 @@
6669
{
6770
await userRecordService.StoreUserRecordAsync();
6871
await jsRuntime.InvokeVoidAsync("hljs.highlightAll");
72+
await commentService.EnableCommentSection("giscus");
6973
StateHasChanged();
7074
}
7175
}

LinkDotNet.Blog.Web/Pages/_Host.cshtml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,26 @@
4747
<script async src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/js/all.min.js" integrity="sha512-RXf+QSDCUQs5uwRKaDoXt55jygZZm2V++WUZduaU/Ui/9EGp3f/2KZVahFZBKGH0s774sd3HmrhUy+SgOFQLVQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
4848
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
4949
<script async src="components/slideshow.js" ></script>
50+
V
51+
<script>
52+
window.initGiscus = (divClass, giscus) => {
53+
const script = document.createElement('script');
54+
script.src = 'https://giscus.app/client.js'
55+
script.setAttribute('data-repo', giscus.repository)
56+
script.setAttribute('data-repo-id', giscus.repositoryId)
57+
script.setAttribute('data-category', giscus.category)
58+
script.setAttribute('data-category-id', giscus.categoryId)
59+
script.setAttribute('data-mapping', 'title')
60+
script.setAttribute('data-reactions-enabled', '0')
61+
script.setAttribute('data-emit-metadata', '0')
62+
script.setAttribute('data-theme', 'light')
63+
script.crossOrigin = 'anonymous'
64+
65+
const elementToAppend = document.getElementsByClassName(divClass)[0]
66+
if (elementToAppend) {
67+
elementToAppend.appendChild(script)
68+
}
69+
}
70+
</script>
5071
</body>
5172
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace LinkDotNet.Blog.Web.Shared.Services
2+
{
3+
public class Giscus
4+
{
5+
public string Repository { get; set; }
6+
7+
public string RepositoryId { get; set; }
8+
9+
public string Category { get; set; }
10+
11+
public string CategoryId { get; set; }
12+
}
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Threading.Tasks;
2+
using Microsoft.JSInterop;
3+
4+
namespace LinkDotNet.Blog.Web.Shared.Services
5+
{
6+
public class GiscusService : ICommentService
7+
{
8+
private readonly IJSRuntime jsRuntime;
9+
10+
public GiscusService(IJSRuntime jsRuntime)
11+
{
12+
this.jsRuntime = jsRuntime;
13+
}
14+
15+
public async Task EnableCommentSection(string className)
16+
{
17+
var giscus = new Giscus
18+
{
19+
Repository = "linkdotnet/Blog.Discussions",
20+
RepositoryId = "MDEwOlJlcG9zaXRvcnk0MDc1MzQ0OTA=",
21+
Category = "General",
22+
CategoryId = "DIC_kwDOGEp7ms4B_Fx_",
23+
};
24+
25+
await jsRuntime.InvokeVoidAsync("initGiscus", "giscus", giscus);
26+
}
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Threading.Tasks;
2+
3+
namespace LinkDotNet.Blog.Web.Shared.Services
4+
{
5+
public interface ICommentService
6+
{
7+
Task EnableCommentSection(string className);
8+
}
9+
}

LinkDotNet.Blog.Web/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void ConfigureServices(IServiceCollection services)
5454
services.AddSingleton<ISortOrderCalculator, SortOrderCalculator>();
5555
services.AddScoped<IUserRecordService, UserRecordService>();
5656
services.AddScoped<IDashboardService, DashboardService>();
57+
services.AddScoped<ICommentService, GiscusService>();
5758
}
5859

5960
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

LinkDotNet.Blog.Web/appsettings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,11 @@
2828
"Name": "Steven Giesel",
2929
"Heading": "Software Engineer",
3030
"ProfilePictureUrl": "assets/profile-picture.webp"
31+
},
32+
"Giscus": {
33+
"Repository": "",
34+
"RepositoryId": "",
35+
"Category": "",
36+
"CategoryId": ""
3137
}
3238
}

0 commit comments

Comments
 (0)