YaeBlog/YaeBlog.Core/Services/RendererService.cs

36 lines
1.1 KiB
C#
Raw Normal View History

2024-01-17 13:20:32 +08:00
using System.Collections.Concurrent;
using Markdig;
using Microsoft.Extensions.Logging;
using YaeBlog.Core.Exceptions;
using YaeBlog.Core.Models;
namespace YaeBlog.Core.Services;
public class RendererService(ILogger<RendererService> logger,
EssayScanService essayScanService,
MarkdownPipeline markdownPipeline,
EssayContentService essayContentService)
{
public async Task RenderAsync()
{
List<BlogContent> contents = await essayScanService.ScanAsync();
Parallel.ForEach(contents, content =>
{
logger.LogDebug("Render markdown file {}.", content.FileName);
BlogEssay essay = new()
{
Title = content.FileName,
PublishTime = DateTime.Now,
HtmlContent = Markdown.ToHtml(content.FileContent, markdownPipeline)
};
if (!essayContentService.TryAdd(essay.Title, essay))
{
throw new BlogFileException(
$"There are two essays with the same name: '{content.FileName}'.");
}
});
}
}