From 0d10946ec14a09d199120b747a5386321b4576ee Mon Sep 17 00:00:00 2001 From: jackfiled Date: Thu, 22 Jan 2026 16:11:18 +0800 Subject: [PATCH] fix: compress images linked by native html tag. Signed-off-by: jackfiled --- YaeBlog/Services/EssayScanService.cs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/YaeBlog/Services/EssayScanService.cs b/YaeBlog/Services/EssayScanService.cs index 09b1baa..b35404a 100644 --- a/YaeBlog/Services/EssayScanService.cs +++ b/YaeBlog/Services/EssayScanService.cs @@ -134,7 +134,8 @@ public partial class EssayScanService : IEssayScanService } catch (YamlException e) { - _logger.LogWarning("Failed to parser metadata from {name} due to {exception}, skipping", blog.BlogFile.Name, e); + _logger.LogWarning("Failed to parser metadata from {name} due to {exception}, skipping", + blog.BlogFile.Name, e); } } }); @@ -146,7 +147,6 @@ public partial class EssayScanService : IEssayScanService private async Task ScanImagePreBlog(DirectoryInfo directory, string blogName, string content) { - MatchCollection matchResult = ImagePattern.Matches(content); DirectoryInfo imageDirectory = new(Path.Combine(directory.FullName, blogName)); Dictionary usedImages = imageDirectory.Exists @@ -154,10 +154,15 @@ public partial class EssayScanService : IEssayScanService : []; List notFoundImages = []; - foreach (Match match in matchResult) - { - string imageName = match.Groups[1].Value; + // 同时扫描markdown格式和HTML格式的图片 + MatchCollection markdownMatchResult = MarkdownImagePattern.Matches(content); + MatchCollection htmlMatchResult = HtmlImagePattern.Matches(content); + IEnumerable imageNames = from match in markdownMatchResult.Concat(htmlMatchResult) + select match.Groups[1].Value; + + foreach (string imageName in imageNames) + { // 判断md文件中的图片名称中是否包含文件夹名称 // 例如 blog-1/image.png 或者 image.png // 如果不带文件夹名称 @@ -204,7 +209,10 @@ public partial class EssayScanService : IEssayScanService } [GeneratedRegex(@"\!\[.*?\]\((.*?)\)")] - private static partial Regex ImagePattern { get; } + private static partial Regex MarkdownImagePattern { get; } + + [GeneratedRegex("""]*?src\s*=\s*["']([^"']*)["'][^>]*>""")] + private static partial Regex HtmlImagePattern { get; } private DirectoryInfo ValidateRootDirectory()