From 2e0de815170e456ba091f93681dfb8a48a699c01 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 29 Jul 2024 22:04:28 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E9=A2=84=E8=AE=A1=E9=98=85=E8=AF=BB?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YaeBlog.Core/Models/BlogEssay.cs | 3 +++ YaeBlog.Core/Services/RendererService.cs | 19 ++++++++++++++++--- YaeBlog/Pages/Essays.razor | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/YaeBlog.Core/Models/BlogEssay.cs b/YaeBlog.Core/Models/BlogEssay.cs index 5e41f02..b8e60b6 100644 --- a/YaeBlog.Core/Models/BlogEssay.cs +++ b/YaeBlog.Core/Models/BlogEssay.cs @@ -12,6 +12,8 @@ public class BlogEssay : IComparable public required uint WordCount { get; init; } + public required string ReadTime { get; init; } + public List Tags { get; } = []; public required string HtmlContent { get; init; } @@ -25,6 +27,7 @@ public class BlogEssay : IComparable PublishTime = PublishTime, Description = Description, WordCount = WordCount, + ReadTime = ReadTime, HtmlContent = newHtmlContent }; essay.Tags.AddRange(Tags); diff --git a/YaeBlog.Core/Services/RendererService.cs b/YaeBlog.Core/Services/RendererService.cs index be69f7e..3195efb 100644 --- a/YaeBlog.Core/Services/RendererService.cs +++ b/YaeBlog.Core/Services/RendererService.cs @@ -12,7 +12,8 @@ using YamlDotNet.Serialization; namespace YaeBlog.Core.Services; -public partial class RendererService(ILogger logger, +public partial class RendererService( + ILogger logger, EssayScanService essayScanService, MarkdownPipeline markdownPipeline, IDeserializer yamlDeserializer, @@ -38,12 +39,14 @@ public partial class RendererService(ILogger logger, foreach (BlogContent content in preProcessedContents) { MarkdownMetadata? metadata = TryParseMetadata(content); + uint wordCount = GetWordCount(content); BlogEssay essay = new() { Title = metadata?.Title ?? content.FileName, FileName = content.FileName, Description = GetDescription(content), - WordCount = GetWordCount(content), + WordCount = wordCount, + ReadTime = CalculateReadTime(wordCount), PublishTime = metadata?.Date ?? DateTime.Now, HtmlContent = content.FileContent }; @@ -52,6 +55,7 @@ public partial class RendererService(ILogger logger, { essay.Tags.AddRange(metadata.Tags); } + essays.Add(essay); } }); @@ -217,7 +221,7 @@ public partial class RendererService(ILogger logger, foreach (char c in content.FileContent) { if (char.IsControl(c) || char.IsSymbol(c) - || char.IsSeparator(c)) + || char.IsSeparator(c)) { continue; } @@ -229,4 +233,13 @@ public partial class RendererService(ILogger logger, 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'秒'"); + } } diff --git a/YaeBlog/Pages/Essays.razor b/YaeBlog/Pages/Essays.razor index 84adb50..0a05d69 100644 --- a/YaeBlog/Pages/Essays.razor +++ b/YaeBlog/Pages/Essays.razor @@ -32,7 +32,7 @@
- 总字数:@(_essay!.WordCount)字。 + 总字数:@(_essay!.WordCount)字,预计阅读时间 @(_essay!.ReadTime)。