From ae6f692e80ca359494ab993aa561b3f13bf7eb72 Mon Sep 17 00:00:00 2001 From: niltor Date: Thu, 11 Apr 2024 09:17:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E9=AB=98markdown=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WebApp/data/blogs.json | 13 +----- WebApp/index.html | 20 ++-------- WebApp/sitemap.xml | 8 +--- src/BuildSite/HtmlBuilder.cs | 40 ++++++++++--------- .../BetterCodeBlockRenderer.cs | 32 ++++++++++----- 5 files changed, 50 insertions(+), 63 deletions(-) diff --git a/WebApp/data/blogs.json b/WebApp/data/blogs.json index 769fe9e..e743783 100644 --- a/WebApp/data/blogs.json +++ b/WebApp/data/blogs.json @@ -6,18 +6,9 @@ "Title": "introduction", "Path": "/introduction.html", "FileName": "introduction.md", - "PublishTime": "2024-04-01T10:48:51+08:00", + "PublishTime": "2024-04-07T14:07:45+08:00", "CreatedTime": "2024-03-20T11:51:35+08:00", - "UpdatedTime": "2024-04-01T10:48:51+08:00", - "Catalog": null - }, - { - "Title": "Markdown支持示例", - "Path": "/Markdown%E6%94%AF%E6%8C%81%E7%A4%BA%E4%BE%8B.html", - "FileName": "Markdown支持示例.md", - "PublishTime": "2024-04-01T16:49:23+08:00", - "CreatedTime": "2024-04-01T14:45:14+08:00", - "UpdatedTime": "2024-04-01T16:49:23+08:00", + "UpdatedTime": "2024-04-07T14:07:45+08:00", "Catalog": null } ], diff --git a/WebApp/index.html b/WebApp/index.html index 49f09ae..5f41e06 100644 --- a/WebApp/index.html +++ b/WebApp/index.html @@ -34,18 +34,6 @@
-
- -

- 👨‍💻 NilTor -    - ⏱️ -

-
-
-
📑 introduction @@ -53,7 +41,7 @@

👨‍💻 NilTor    - ⏱️ + ⏱️

@@ -66,16 +54,16 @@
分类
- 全部 [2] + 全部 [1]
存档
- 全部 [2] + 全部 [1] - 2024-04 [2] + 2024-04 [1]
diff --git a/WebApp/sitemap.xml b/WebApp/sitemap.xml index 2aae6b9..6bf84b8 100644 --- a/WebApp/sitemap.xml +++ b/WebApp/sitemap.xml @@ -2,13 +2,7 @@ https://aterdev.github.io/EasyBlog/blogs/introduction.html - 2024-04-01 - daily - 0.9 - - - https://aterdev.github.io/EasyBlog/blogs/Markdown%E6%94%AF%E6%8C%81%E7%A4%BA%E4%BE%8B.html - 2024-04-01 + 2024-04-07 daily 0.9 diff --git a/src/BuildSite/HtmlBuilder.cs b/src/BuildSite/HtmlBuilder.cs index 5ffead5..b013d6c 100644 --- a/src/BuildSite/HtmlBuilder.cs +++ b/src/BuildSite/HtmlBuilder.cs @@ -82,11 +82,13 @@ public void BuildBlogs() List otherFiles = Directory.EnumerateFiles(ContentPath, "*", SearchOption.AllDirectories) .Where(f => !f.EndsWith(".md")) .ToList(); - try + + foreach (var file in files) { - foreach (var file in files) + try { string markdown = File.ReadAllText(file); + string html = Markdown.ToHtml(markdown, pipeline); string relativePath = file.Replace(ContentPath, Path.Combine(Output, "blogs")).Replace(".md", ".html"); @@ -102,27 +104,29 @@ public void BuildBlogs() File.WriteAllText(relativePath, html, Encoding.UTF8); } - Console.WriteLine("✅ generate blog html!"); - - foreach (var file in otherFiles) + catch (Exception e) { - string relativePath = file.Replace(ContentPath, Path.Combine(Output, "blogs")); - string? dir = Path.GetDirectoryName(relativePath); - - if (!Directory.Exists(dir)) - { - Directory.CreateDirectory(dir!); - } - - File.Copy(file, relativePath, true); + Console.WriteLine($"❌ parse markdown error: {file}" + e.Message + e.StackTrace); } - Console.WriteLine("✅ copy blog other files!"); + } - catch (Exception e) + Console.WriteLine("✅ generate blog html!"); + + foreach (var file in otherFiles) { - Console.WriteLine("HtmlBuilder:BuildBlogs:" + e.Message); - throw; + string relativePath = file.Replace(ContentPath, Path.Combine(Output, "blogs")); + string? dir = Path.GetDirectoryName(relativePath); + + if (!Directory.Exists(dir)) + { + Directory.CreateDirectory(dir!); + } + + File.Copy(file, relativePath, true); } + Console.WriteLine("✅ copy blog other files!"); + + } /// diff --git a/src/BuildSite/MarkdownExtension/BetterCodeBlockRenderer.cs b/src/BuildSite/MarkdownExtension/BetterCodeBlockRenderer.cs index 231f213..94436ea 100644 --- a/src/BuildSite/MarkdownExtension/BetterCodeBlockRenderer.cs +++ b/src/BuildSite/MarkdownExtension/BetterCodeBlockRenderer.cs @@ -16,7 +16,7 @@ public BetterCodeBlockRenderer(CodeBlockRenderer underlyingRenderer) protected override void Write(HtmlRenderer renderer, CodeBlock obj) { - if (obj is not FencedCodeBlock fencedCodeBlock || obj.Parser is not FencedCodeBlockParser parser) + if (obj is not FencedCodeBlock fencedCodeBlock || obj.Parser is not FencedCodeBlockParser parser || obj is null) { _underlyingRenderer.Write(renderer, obj); return; @@ -29,22 +29,32 @@ protected override void Write(HtmlRenderer renderer, CodeBlock obj) return; } - var code = GetCode(obj).Trim(); + var code = GetCode(obj)?.Trim(); - var formatter = new HtmlClassFormatter(); - - var html = formatter.GetHtmlString(code, language); - renderer.WriteLine(html); + if (code != null) + { + var formatter = new HtmlClassFormatter(); + var html = formatter.GetHtmlString(code, language); + renderer.WriteLine(html); + return; + } + _underlyingRenderer.Write(renderer, obj); + return; } - private static string GetCode(LeafBlock obj) + + private static string? GetCode(LeafBlock obj) { var str = new StringBuilder(); - foreach (var line in obj.Lines.Lines) + if (obj.Lines.Count > 0) { - if (!string.IsNullOrWhiteSpace(line.Slice.ToString().Trim())) - str.AppendLine(line.Slice.ToString()); + foreach (var line in obj.Lines.Lines) + { + if (!string.IsNullOrWhiteSpace(line.Slice.ToString().Trim())) + str.AppendLine(line.Slice.ToString()); + } + return str.ToString(); } - return str.ToString(); + return null; } }