diff --git a/YaeBlog.Core/Services/EssayScanService.cs b/YaeBlog.Core/Services/EssayScanService.cs index cff5b11..29c5cac 100644 --- a/YaeBlog.Core/Services/EssayScanService.cs +++ b/YaeBlog.Core/Services/EssayScanService.cs @@ -35,6 +35,11 @@ public partial class EssayScanService( ? new FileInfo(Path.Combine(drafts.FullName, content.FileName + ".md")) : new FileInfo(Path.Combine(posts.FullName, content.FileName + ".md")); + if (!isDraft) + { + content.Metadata.Date = DateTime.Now; + } + if (targetFile.Exists) { logger.LogWarning("Blog {} exists, overriding.", targetFile.Name); @@ -45,7 +50,15 @@ public partial class EssayScanService( await writer.WriteAsync("---\n"); await writer.WriteAsync(yamlSerializer.Serialize(content.Metadata)); await writer.WriteAsync("---\n"); - await writer.WriteAsync("\n"); + + if (isDraft) + { + await writer.WriteLineAsync(""); + } + else + { + await writer.WriteAsync(content.FileContent); + } } private async Task> ScanContentsInternal(DirectoryInfo directory) @@ -116,7 +129,7 @@ public partial class EssayScanService( return new ImageScanResult(unusedFiles, notFoundFiles); } - private Task ScanUnusedImagesInternal(IEnumerable contents, + private static Task ScanUnusedImagesInternal(IEnumerable contents, DirectoryInfo root) { Regex imageRegex = ImageRegex(); diff --git a/YaeBlog/Commands/CommandExtensions.cs b/YaeBlog/Commands/CommandExtensions.cs index 18fc659..bb7576d 100644 --- a/YaeBlog/Commands/CommandExtensions.cs +++ b/YaeBlog/Commands/CommandExtensions.cs @@ -81,6 +81,14 @@ public static class CommandExtensions newCommand.SetHandler(async (file, _, _, essayScanService) => { + BlogContents contents = await essayScanService.ScanContents(); + + if (contents.Posts.Any(content => content.FileName == file)) + { + Console.WriteLine("There exists the same title blog in posts."); + return; + } + await essayScanService.SaveBlogContent(new BlogContent { FileName = file, @@ -103,13 +111,13 @@ public static class CommandExtensions BlogContents contents = await essyScanService.ScanContents(); Console.WriteLine($"All {contents.Posts.Count} Posts:"); - foreach (BlogContent content in contents.Posts) + foreach (BlogContent content in contents.Posts.OrderBy(x => x.FileName)) { Console.WriteLine($" - {content.FileName}"); } Console.WriteLine($"All {contents.Drafts.Count} Drafts:"); - foreach (BlogContent content in contents.Drafts) + foreach (BlogContent content in contents.Drafts.OrderBy(x => x.FileName)) { Console.WriteLine($" - {content.FileName}"); } @@ -156,4 +164,53 @@ public static class CommandExtensions } }, new BlogOptionsBinder(), new LoggerBinder(), new EssayScanServiceBinder(), removeOption); } + + public static void AddPublishCommand(this RootCommand rootCommand) + { + Command command = new("publish", "Publish a new blog file."); + rootCommand.AddCommand(command); + + Argument filenameArgument = new(name: "blog name", description: "The published blog filename."); + command.AddArgument(filenameArgument); + + command.SetHandler(async (blogOptions, _, essayScanService, filename) => + { + BlogContents contents = await essayScanService.ScanContents(); + + BlogContent? content = (from blog in contents.Drafts + where blog.FileName == filename + select blog).FirstOrDefault(); + + if (content is null) + { + Console.WriteLine("Target blog does not exist."); + return; + } + + // 将选中的博客文件复制到posts + await essayScanService.SaveBlogContent(content, isDraft: false); + + // 复制图片文件夹 + DirectoryInfo sourceImageDirectory = + new(Path.Combine(blogOptions.Value.Root, "drafts", content.FileName)); + DirectoryInfo targetImageDirectory = + new(Path.Combine(blogOptions.Value.Root, "posts", content.FileName)); + + if (sourceImageDirectory.Exists) + { + targetImageDirectory.Create(); + foreach (FileInfo file in sourceImageDirectory.EnumerateFiles()) + { + file.CopyTo(Path.Combine(targetImageDirectory.FullName, file.Name), true); + } + + sourceImageDirectory.Delete(true); + } + + // 删除原始的文件 + FileInfo sourceBlogFile = new(Path.Combine(blogOptions.Value.Root, "drafts", content.FileName + ".md")); + sourceBlogFile.Delete(); + }, new BlogOptionsBinder(), + new LoggerBinder(), new EssayScanServiceBinder(), filenameArgument); + } } diff --git a/YaeBlog/Program.cs b/YaeBlog/Program.cs index 5bbf620..482e3c7 100644 --- a/YaeBlog/Program.cs +++ b/YaeBlog/Program.cs @@ -8,5 +8,6 @@ rootCommand.AddNewCommand(); rootCommand.AddListCommand(); rootCommand.AddWatchCommand(); rootCommand.AddScanCommand(); +rootCommand.AddPublishCommand(); await rootCommand.InvokeAsync(args);