YaeBlog/YaeBlog.Core/Services/RendererService.cs

113 lines
3.3 KiB
C#
Raw Normal View History

2024-01-23 14:33:35 +08:00
using System.Diagnostics;
using Markdig;
2024-01-17 13:20:32 +08:00
using Microsoft.Extensions.Logging;
using YaeBlog.Core.Exceptions;
using YaeBlog.Core.Models;
2024-01-23 14:33:35 +08:00
using YamlDotNet.Core;
2024-01-19 20:33:41 +08:00
using YamlDotNet.Serialization;
2024-01-17 13:20:32 +08:00
namespace YaeBlog.Core.Services;
public class RendererService(ILogger<RendererService> logger,
EssayScanService essayScanService,
MarkdownPipeline markdownPipeline,
2024-01-19 20:33:41 +08:00
IDeserializer yamlDeserializer,
2024-01-17 13:20:32 +08:00
EssayContentService essayContentService)
{
2024-01-23 14:33:35 +08:00
private readonly Stopwatch _stopwatch = new();
2024-01-17 13:20:32 +08:00
public async Task RenderAsync()
{
2024-01-23 14:33:35 +08:00
_stopwatch.Start();
logger.LogInformation("Render essays start.");
2024-01-17 13:20:32 +08:00
List<BlogContent> contents = await essayScanService.ScanAsync();
2024-01-23 14:33:35 +08:00
List<BlogEssay> essays = [];
await Task.Run(() =>
{
foreach (BlogContent content in contents)
{
MarkdownMetadata? metadata = TryParseMetadata(content);
BlogEssay essay = new()
{
Title = metadata?.Title ?? content.FileName,
FileName = content.FileName,
PublishTime = metadata?.Date ?? DateTime.Now,
HtmlContent = content.FileContent
};
if (metadata?.Tags is not null)
{
essay.Tags.AddRange(metadata.Tags);
}
essays.Add(essay);
}
});
Parallel.ForEach(essays, essay =>
2024-01-17 13:20:32 +08:00
{
2024-01-19 20:33:41 +08:00
2024-01-23 14:33:35 +08:00
BlogEssay newEssay = new()
2024-01-17 13:20:32 +08:00
{
2024-01-23 14:33:35 +08:00
Title = essay.Title,
FileName = essay.FileName,
PublishTime = essay.PublishTime,
HtmlContent = Markdown.ToHtml(essay.HtmlContent, markdownPipeline)
2024-01-17 13:20:32 +08:00
};
2024-01-23 14:33:35 +08:00
newEssay.Tags.AddRange(essay.Tags);
2024-01-17 13:20:32 +08:00
2024-01-23 14:33:35 +08:00
if (!essayContentService.TryAdd(newEssay.FileName, newEssay))
2024-01-17 13:20:32 +08:00
{
throw new BlogFileException(
2024-01-23 14:33:35 +08:00
$"There are two essays with the same name: '{newEssay.FileName}'.");
2024-01-17 13:20:32 +08:00
}
2024-01-23 14:33:35 +08:00
logger.LogDebug("Render markdown file {}.", newEssay);
2024-01-17 13:20:32 +08:00
});
2024-01-23 14:33:35 +08:00
_stopwatch.Stop();
logger.LogInformation("Render finished, consuming {} s.",
_stopwatch.Elapsed.ToString("s\\.fff"));
2024-01-17 13:20:32 +08:00
}
2024-01-19 20:33:41 +08:00
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];
// 返回去掉元数据之后的文本
lastPos += 3;
content.FileContent = fileContent[lastPos..];
2024-01-23 14:33:35 +08:00
try
{
MarkdownMetadata metadata =
yamlDeserializer.Deserialize<MarkdownMetadata>(yamlContent);
logger.LogDebug("Title: {}, Publish Date: {}.",
metadata.Title, metadata.Date);
return metadata;
}
catch (YamlException e)
{
logger.LogWarning("Failed to parse '{}' metadata: {}", yamlContent, e);
return null;
}
2024-01-19 20:33:41 +08:00
}
2024-01-17 13:20:32 +08:00
}