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