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

View File

@ -12,7 +12,8 @@ using YamlDotNet.Serialization;
namespace YaeBlog.Core.Services;
public partial class RendererService(ILogger<RendererService> logger,
public partial class RendererService(
ILogger<RendererService> logger,
EssayScanService essayScanService,
MarkdownPipeline markdownPipeline,
IDeserializer yamlDeserializer,
@ -38,12 +39,14 @@ public partial class RendererService(ILogger<RendererService> 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<RendererService> logger,
{
essay.Tags.AddRange(metadata.Tags);
}
essays.Add(essay);
}
});
@ -217,7 +221,7 @@ public partial class RendererService(ILogger<RendererService> 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<RendererService> 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'秒'");
}
}

View File

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