From bdcfed550636bfb0413b243ed90e6e72ae5c6a29 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 25 Jan 2025 14:27:20 +0800 Subject: [PATCH] fix: failed to handle images in draft. --- YaeBlog/Processors/ImagePostRenderProcessor.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/YaeBlog/Processors/ImagePostRenderProcessor.cs b/YaeBlog/Processors/ImagePostRenderProcessor.cs index f64a03a..7b02d86 100644 --- a/YaeBlog/Processors/ImagePostRenderProcessor.cs +++ b/YaeBlog/Processors/ImagePostRenderProcessor.cs @@ -7,7 +7,8 @@ using YaeBlog.Models; namespace YaeBlog.Processors; -public class ImagePostRenderProcessor(ILogger logger, +public class ImagePostRenderProcessor( + ILogger logger, IOptions options) : IPostRenderProcessor { @@ -29,22 +30,27 @@ public class ImagePostRenderProcessor(ILogger logger, if (attr is not null) { logger.LogDebug("Found image link: '{}'", attr.Value); - attr.Value = GenerateImageLink(attr.Value, essay.FileName); + attr.Value = GenerateImageLink(attr.Value, essay.FileName, essay.IsDraft); } } + return essay.WithNewHtmlContent(html.DocumentElement.OuterHtml); } public string Name => nameof(ImagePostRenderProcessor); - private string GenerateImageLink(string filename, string essayFilename) + private string GenerateImageLink(string filename, string essayFilename, bool isDraft) { + // 如果图片路径中没有包含文件名 + // 则添加文件名 if (!filename.Contains(essayFilename)) { filename = Path.Combine(essayFilename, filename); } - filename = Path.Combine(_options.Root, "posts", filename); + filename = isDraft + ? Path.Combine(_options.Root, "drafts", filename) + : Path.Combine(_options.Root, "posts", filename); if (!Path.Exists(filename)) {