-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathShowBlogPostPage.razor
More file actions
192 lines (170 loc) · 6.14 KB
/
Copy pathShowBlogPostPage.razor
File metadata and controls
192 lines (170 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
@page "/blogPost/{blogPostId}/{slug?}"
@using System.Text
@using Markdig
@using LinkDotNet.Blog.Domain
@using LinkDotNet.Blog.Infrastructure.Persistence
@using LinkDotNet.Blog.Web.Features.Bookmarks
@using LinkDotNet.Blog.Web.Features.Services
@using LinkDotNet.Blog.Web.Features.ShowBlogPost.Components
@using LinkDotNet.Blog.Web.Features.SupportMe.Components
@using LinkDotNet.Blog.Web.Features.Bookmarks.Components
@inject IRepository<BlogPost> BlogPostRepository
@inject IRepository<ShortCode> ShortCodeRepository
@inject IJSRuntime JsRuntime
@inject NavigationManager NavigationManager
@inject IUserRecordService UserRecordService
@inject IOptions<ApplicationConfiguration> AppConfiguration
@inject IOptions<ProfileInformation> ProfileInformation
@inject IOptions<SupportMeConfiguration> SupportConfiguration
@inject IBookmarkService BookmarkService
@if (isLoading)
{
<Loading></Loading>
}
else if (!isLoading && BlogPost is null)
{
<ObjectNotFound></ObjectNotFound>
}
else if (BlogPost is not null)
{
<PageTitle>@BlogPost.Title</PageTitle>
<OgData Title="@BlogPost.Title"
AbsolutePreviewImageUrl="@OgDataImage"
Description="@(Markdown.ToPlainText(BlogPost.ShortDescription))"
Keywords="@BlogPost.TagsAsString"
CanonicalRelativeUrl="@BlogPostCanoncialUrl">
<StructuredData Headline="@BlogPost.Title"
PreviewImage="@BlogPost.PreviewImageUrl"
PreviewFallbackImage="@BlogPost.PreviewImageUrlFallback"
PublishedDate="@BlogPost.UpdatedDate"
Author="@ProfileInformation.Value.Name"/>
</OgData>
<div class="d-flex justify-content-center pt-2 blog-outer-box">
<div class="blog-container">
<div class="blog-inner-content">
<header class="text-center">
<h1 class="fw-bold">@BlogPost.Title</h1></header>
<div class="text-body-secondary d-flex flex-wrap align-items-center gap-3 meta-info">
<div class="d-inline-flex align-items-center">
<span class="date me-1"></span>
<span>@BlogPost.UpdatedDate.ToShortDateString()</span>
</div>
<div class="d-inline-flex align-items-center">
<span class="read-time me-1"></span>
<span>@BlogPost.ReadingTimeInMinutes minute read</span>
</div>
@if (AppConfiguration.Value.UseMultiAuthorMode && BlogPost.AuthorName is not null)
{
<div class="d-inline-flex align-items-center">
<i class="user-tie me-1"></i>
<span>@BlogPost.AuthorName</span>
</div>
}
<div class="d-inline-flex align-items-center">
<BookmarkButton IsBookmarked="isBookmarked" Bookmarked="BlogPostBookmarked"></BookmarkButton>
</div>
@if (BlogPost.Tags is not null && BlogPost.Tags.Any())
{
<div class="d-inline-flex align-items-center flex-wrap">
<span class="blogpost-tag me-2"></span>
<div class="d-flex flex-wrap gap-2">
@foreach (var tag in BlogPost.Tags)
{
<a class="goto-tag badge bg-primary rounded-pill text-decoration-none" href="/searchByTag/@(Uri.EscapeDataString(tag))">@tag</a>
}
</div>
</div>
}
</div>
<div class="pt-2">
<BlogPostAdminActions BlogPostId="@BlogPostId"></BlogPostAdminActions>
</div>
<div class="pt-2">
<TableOfContents Content="@BlogPost.Content" CurrentUri="@NavigationManager.Uri"></TableOfContents>
</div>
<div class="blogpost-content">
@(EnrichWithShortCodes(BlogPost.Content))
</div>
</div>
<div class="d-flex justify-content-between py-2 border-top border-bottom align-items-center">
<Like BlogPost="@BlogPost" OnBlogPostLiked="@UpdateLikes"></Like>
<ShareBlogPost></ShareBlogPost>
</div>
@if (SupportConfiguration.Value.ShowUnderBlogPost)
{
<DonationSection />
}
@if (AppConfiguration.Value.ShowSimilarPosts)
{
<SimilarBlogPostSection BlogPost="@BlogPost" />
}
<CommentSection></CommentSection>
</div>
</div>
@if (AppConfiguration.Value.ShowReadingIndicator)
{
<ReadingIndicator ContainerCssSelector=".blog-inner-content"/>
}
}
@code {
[Parameter, EditorRequired]
public required string BlogPostId { get; set; }
[Parameter]
public string? Slug { get; set; }
private bool isLoading;
private string OgDataImage => BlogPost!.PreviewImageUrlFallback ?? BlogPost.PreviewImageUrl;
private string BlogPostCanoncialUrl => $"blogPost/{BlogPost?.Id}";
private IReadOnlyCollection<ShortCode> shortCodes = [];
private bool isBookmarked;
private BlogPost? BlogPost { get; set; }
protected override async Task OnInitializedAsync()
{
shortCodes = await ShortCodeRepository.GetAllAsync();
}
protected override async Task OnParametersSetAsync()
{
isLoading = true;
BlogPost = await BlogPostRepository.GetByIdAsync(BlogPostId);
isLoading = false;
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await JsRuntime.InvokeVoidAsync("hljs.highlightAll");
_ = UserRecordService.StoreUserRecordAsync();
if (BlogPost is not null && firstRender)
{
isBookmarked = await BookmarkService.IsBookmarked(BlogPost.Id);
StateHasChanged();
}
}
private MarkupString EnrichWithShortCodes(string content)
{
if (shortCodes.Count == 0)
{
return MarkdownConverter.ToMarkupString(content);
}
var sb = new StringBuilder(content);
foreach (var shortCode in shortCodes)
{
sb.Replace($"[[{shortCode.Name}]]", shortCode.MarkdownContent);
}
return MarkdownConverter.ToMarkupString(sb.ToString());
}
private async Task UpdateLikes(bool hasLiked)
{
BlogPost = await BlogPostRepository.GetByIdAsync(BlogPostId)
?? throw new InvalidOperationException("Blog post not found");
BlogPost.Likes = hasLiked ? BlogPost.Likes + 1 : BlogPost.Likes - 1;
await BlogPostRepository.StoreAsync(BlogPost);
}
private async Task BlogPostBookmarked()
{
if (BlogPost is null)
{
return;
}
isBookmarked = !isBookmarked;
await BookmarkService.SetBookmark(BlogPost.Id, isBookmarked);
StateHasChanged();
}
}