From afc6f2637001ddc3ecba251c608e4068a564534d Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 24 Jan 2024 14:00:55 +0800 Subject: [PATCH] =?UTF-8?q?add:=20=E6=96=87=E7=AB=A0=E6=A6=82=E8=BF=B0?= =?UTF-8?q?=E5=92=8C=E5=AD=97=E6=95=B0=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YaeBlog.Core/Models/BlogEssay.cs | 4 ++ YaeBlog.Core/Services/EssayScanService.cs | 2 +- YaeBlog.Core/Services/RendererService.cs | 58 +++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/YaeBlog.Core/Models/BlogEssay.cs b/YaeBlog.Core/Models/BlogEssay.cs index 218c461..08a3833 100644 --- a/YaeBlog.Core/Models/BlogEssay.cs +++ b/YaeBlog.Core/Models/BlogEssay.cs @@ -8,6 +8,10 @@ public class BlogEssay public required DateTime PublishTime { get; init; } + public required string Description { get; init; } + + public required uint WordCount { get; init; } + public List Tags { get; } = []; public required string HtmlContent { get; init; } diff --git a/YaeBlog.Core/Services/EssayScanService.cs b/YaeBlog.Core/Services/EssayScanService.cs index ca2910d..b486c8f 100644 --- a/YaeBlog.Core/Services/EssayScanService.cs +++ b/YaeBlog.Core/Services/EssayScanService.cs @@ -46,7 +46,7 @@ public class EssayScanService( BlogContent content = new() { - FileName = info.Name, FileContent = await reader.ReadToEndAsync(token) + FileName = info.Name.Split('.')[0], FileContent = await reader.ReadToEndAsync(token) }; contents.Add(content); diff --git a/YaeBlog.Core/Services/RendererService.cs b/YaeBlog.Core/Services/RendererService.cs index ff23b38..2a463e5 100644 --- a/YaeBlog.Core/Services/RendererService.cs +++ b/YaeBlog.Core/Services/RendererService.cs @@ -1,5 +1,6 @@ using System.Collections.Concurrent; using System.Diagnostics; +using System.Text; using Markdig; using Microsoft.Extensions.Logging; using YaeBlog.Core.Abstractions; @@ -41,6 +42,8 @@ public class RendererService(ILogger logger, { Title = metadata?.Title ?? content.FileName, FileName = content.FileName, + Description = GetDescription(content), + WordCount = GetWordCount(content), PublishTime = metadata?.Date ?? DateTime.Now, HtmlContent = content.FileContent }; @@ -61,6 +64,8 @@ public class RendererService(ILogger logger, { Title = essay.Title, FileName = essay.FileName, + Description = essay.Description, + WordCount = essay.WordCount, PublishTime = essay.PublishTime, HtmlContent = Markdown.ToHtml(essay.HtmlContent, markdownPipeline) }; @@ -177,4 +182,57 @@ public class RendererService(ILogger logger, return null; } } + + private string GetDescription(BlogContent content) + { + const string delimiter = ""; + int pos = content.FileContent.IndexOf(delimiter, StringComparison.Ordinal); + StringBuilder builder = new(); + + if (pos == -1) + { + // 自动截取前50个字符 + pos = 50; + } + + for (int i = 0; i < pos; i++) + { + char c = content.FileContent[i]; + + if (char.IsControl(c) || char.IsSymbol(c) || + char.IsSeparator(c) || char.IsPunctuation(c) || + char.IsAsciiLetter(c)) + { + continue; + } + + builder.Append(c); + } + + string description = builder.ToString(); + + logger.LogDebug("Description of {} is {}.", content.FileName, + description); + return description; + } + + private uint GetWordCount(BlogContent content) + { + uint count = 0; + + foreach (char c in content.FileContent) + { + if (char.IsControl(c) || char.IsSymbol(c) + || char.IsSeparator(c)) + { + continue; + } + + count++; + } + + logger.LogDebug("Word count of {} is {}", content.FileName, + count); + return count; + } }