feat: 美化文章界面 #3

Merged
jackfiled merged 8 commits from feat-markdown into master 2024-07-29 22:32:27 +08:00
3 changed files with 20 additions and 4 deletions
Showing only changes of commit 2e0de81517 - Show all commits

View File

@ -12,6 +12,8 @@ public class BlogEssay : IComparable<BlogEssay>
public required uint WordCount { get; init; } public required uint WordCount { get; init; }
public required string ReadTime { get; init; }
public List<string> Tags { get; } = []; public List<string> Tags { get; } = [];
public required string HtmlContent { get; init; } public required string HtmlContent { get; init; }
@ -25,6 +27,7 @@ public class BlogEssay : IComparable<BlogEssay>
PublishTime = PublishTime, PublishTime = PublishTime,
Description = Description, Description = Description,
WordCount = WordCount, WordCount = WordCount,
ReadTime = ReadTime,
HtmlContent = newHtmlContent HtmlContent = newHtmlContent
}; };
essay.Tags.AddRange(Tags); essay.Tags.AddRange(Tags);

View File

@ -12,7 +12,8 @@ using YamlDotNet.Serialization;
namespace YaeBlog.Core.Services; namespace YaeBlog.Core.Services;
public partial class RendererService(ILogger<RendererService> logger, public partial class RendererService(
ILogger<RendererService> logger,
EssayScanService essayScanService, EssayScanService essayScanService,
MarkdownPipeline markdownPipeline, MarkdownPipeline markdownPipeline,
IDeserializer yamlDeserializer, IDeserializer yamlDeserializer,
@ -38,12 +39,14 @@ public partial class RendererService(ILogger<RendererService> logger,
foreach (BlogContent content in preProcessedContents) foreach (BlogContent content in preProcessedContents)
{ {
MarkdownMetadata? metadata = TryParseMetadata(content); MarkdownMetadata? metadata = TryParseMetadata(content);
uint wordCount = GetWordCount(content);
BlogEssay essay = new() BlogEssay essay = new()
{ {
Title = metadata?.Title ?? content.FileName, Title = metadata?.Title ?? content.FileName,
FileName = content.FileName, FileName = content.FileName,
Description = GetDescription(content), Description = GetDescription(content),
WordCount = GetWordCount(content), WordCount = wordCount,
ReadTime = CalculateReadTime(wordCount),
PublishTime = metadata?.Date ?? DateTime.Now, PublishTime = metadata?.Date ?? DateTime.Now,
HtmlContent = content.FileContent HtmlContent = content.FileContent
}; };
@ -52,6 +55,7 @@ public partial class RendererService(ILogger<RendererService> logger,
{ {
essay.Tags.AddRange(metadata.Tags); essay.Tags.AddRange(metadata.Tags);
} }
essays.Add(essay); essays.Add(essay);
} }
}); });
@ -217,7 +221,7 @@ public partial class RendererService(ILogger<RendererService> logger,
foreach (char c in content.FileContent) foreach (char c in content.FileContent)
{ {
if (char.IsControl(c) || char.IsSymbol(c) if (char.IsControl(c) || char.IsSymbol(c)
|| char.IsSeparator(c)) || char.IsSeparator(c))
{ {
continue; continue;
} }
@ -229,4 +233,13 @@ public partial class RendererService(ILogger<RendererService> logger,
count); count);
return count; return count;
} }
private static string CalculateReadTime(uint wordCount)
{
// 据说语文教学大纲规定中国高中问阅读现代文的速度是600字每分钟
int second = (int)wordCount / 10;
TimeSpan span = new TimeSpan(0, 0, second);
return span.ToString("mm'分 'ss'秒'");
}
} }

View File

@ -32,7 +32,7 @@
<div class="row px-4 py-1"> <div class="row px-4 py-1">
<div class="col-auto fw-light"> <div class="col-auto fw-light">
总字数:@(_essay!.WordCount)字。 总字数:@(_essay!.WordCount)字,预计阅读时间 @(_essay!.ReadTime)
</div> </div>
</div> </div>