add: 文章概述和字数统计

This commit is contained in:
jackfiled 2024-01-24 14:00:55 +08:00
parent 2734191166
commit afc6f26370
3 changed files with 63 additions and 1 deletions

View File

@ -8,6 +8,10 @@ public class BlogEssay
public required DateTime PublishTime { get; init; } public required DateTime PublishTime { get; init; }
public required string Description { get; init; }
public required uint WordCount { get; init; }
public List<string> Tags { get; } = []; public List<string> Tags { get; } = [];
public required string HtmlContent { get; init; } public required string HtmlContent { get; init; }

View File

@ -46,7 +46,7 @@ public class EssayScanService(
BlogContent content = new() BlogContent content = new()
{ {
FileName = info.Name, FileContent = await reader.ReadToEndAsync(token) FileName = info.Name.Split('.')[0], FileContent = await reader.ReadToEndAsync(token)
}; };
contents.Add(content); contents.Add(content);

View File

@ -1,5 +1,6 @@
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Diagnostics; using System.Diagnostics;
using System.Text;
using Markdig; using Markdig;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using YaeBlog.Core.Abstractions; using YaeBlog.Core.Abstractions;
@ -41,6 +42,8 @@ public class RendererService(ILogger<RendererService> logger,
{ {
Title = metadata?.Title ?? content.FileName, Title = metadata?.Title ?? content.FileName,
FileName = content.FileName, FileName = content.FileName,
Description = GetDescription(content),
WordCount = GetWordCount(content),
PublishTime = metadata?.Date ?? DateTime.Now, PublishTime = metadata?.Date ?? DateTime.Now,
HtmlContent = content.FileContent HtmlContent = content.FileContent
}; };
@ -61,6 +64,8 @@ public class RendererService(ILogger<RendererService> logger,
{ {
Title = essay.Title, Title = essay.Title,
FileName = essay.FileName, FileName = essay.FileName,
Description = essay.Description,
WordCount = essay.WordCount,
PublishTime = essay.PublishTime, PublishTime = essay.PublishTime,
HtmlContent = Markdown.ToHtml(essay.HtmlContent, markdownPipeline) HtmlContent = Markdown.ToHtml(essay.HtmlContent, markdownPipeline)
}; };
@ -177,4 +182,57 @@ public class RendererService(ILogger<RendererService> logger,
return null; return null;
} }
} }
private string GetDescription(BlogContent content)
{
const string delimiter = "<!--more-->";
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;
}
} }