fix: compress images linked by native html <img> tag.

Signed-off-by: jackfiled <xcrenchangjun@outlook.com>
This commit is contained in:
2026-01-22 16:11:18 +08:00
parent a3791596da
commit 0d10946ec1

View File

@@ -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<ImageResult> ScanImagePreBlog(DirectoryInfo directory, string blogName, string content)
{
MatchCollection matchResult = ImagePattern.Matches(content);
DirectoryInfo imageDirectory = new(Path.Combine(directory.FullName, blogName));
Dictionary<string, bool> usedImages = imageDirectory.Exists
@@ -154,10 +154,15 @@ public partial class EssayScanService : IEssayScanService
: [];
List<FileInfo> 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<string> 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("""<img\s[^>]*?src\s*=\s*["']([^"']*)["'][^>]*>""")]
private static partial Regex HtmlImagePattern { get; }
private DirectoryInfo ValidateRootDirectory()