add: 预计阅读时间

This commit is contained in:
jackfiled 2024-07-29 22:04:28 +08:00
parent 91ae6d8ac1
commit 2e0de81517
3 changed files with 20 additions and 4 deletions

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);
} }
}); });
@ -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>