add: 元数据提取
This commit is contained in:
@@ -1,14 +1,15 @@
|
||||
using System.Collections.Concurrent;
|
||||
using Markdig;
|
||||
using Markdig;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using YaeBlog.Core.Exceptions;
|
||||
using YaeBlog.Core.Models;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace YaeBlog.Core.Services;
|
||||
|
||||
public class RendererService(ILogger<RendererService> logger,
|
||||
EssayScanService essayScanService,
|
||||
MarkdownPipeline markdownPipeline,
|
||||
IDeserializer yamlDeserializer,
|
||||
EssayContentService essayContentService)
|
||||
{
|
||||
public async Task RenderAsync()
|
||||
@@ -17,19 +18,56 @@ public class RendererService(ILogger<RendererService> logger,
|
||||
|
||||
Parallel.ForEach(contents, content =>
|
||||
{
|
||||
logger.LogDebug("Render markdown file {}.", content.FileName);
|
||||
MarkdownMetadata? metadata = TryParseMetadata(content);
|
||||
|
||||
BlogEssay essay = new()
|
||||
{
|
||||
Title = content.FileName,
|
||||
PublishTime = DateTime.Now,
|
||||
Title = metadata?.Title ?? content.FileName,
|
||||
PublishTime = metadata?.Date ?? DateTime.Now,
|
||||
HtmlContent = Markdown.ToHtml(content.FileContent, markdownPipeline)
|
||||
};
|
||||
if (metadata is not null)
|
||||
{
|
||||
essay.Tags.AddRange(essay.Tags);
|
||||
}
|
||||
|
||||
if (!essayContentService.TryAdd(essay.Title, essay))
|
||||
{
|
||||
throw new BlogFileException(
|
||||
$"There are two essays with the same name: '{content.FileName}'.");
|
||||
}
|
||||
logger.LogDebug("Render markdown file {}.", essay);
|
||||
logger.LogDebug("{}", essay.HtmlContent);
|
||||
});
|
||||
}
|
||||
|
||||
private MarkdownMetadata? TryParseMetadata(BlogContent content)
|
||||
{
|
||||
string fileContent = content.FileContent.Trim();
|
||||
|
||||
if (!fileContent.StartsWith("---"))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 移除起始的---
|
||||
fileContent = fileContent[3..];
|
||||
|
||||
int lastPos = fileContent.IndexOf("---", StringComparison.Ordinal);
|
||||
if (lastPos is -1 or 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string yamlContent = fileContent[..lastPos];
|
||||
MarkdownMetadata metadata = yamlDeserializer.Deserialize<MarkdownMetadata>(yamlContent);
|
||||
logger.LogDebug("Title: {}, Publish Date: {}.",
|
||||
metadata.Title, metadata.Date);
|
||||
|
||||
// 返回去掉元数据之后的文本
|
||||
lastPos += 3;
|
||||
content.FileContent = fileContent[lastPos..];
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user