From d9c17720dcde03d8b3cdccee228b8e0414120f03 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 28 Aug 2024 20:26:41 +0800 Subject: [PATCH] dev: CSharp language highlight. --- .../CodeBlockPostRenderProcessor.cs | 27 ++++++++++++++++++- .../Processors/Scanners/CSharpScanner.cs | 9 +++++++ YaeBlog.Core/YaeBlog.Core.csproj | 2 ++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 YaeBlog.Core/Processors/Scanners/CSharpScanner.cs diff --git a/YaeBlog.Core/Processors/CodeBlockPostRenderProcessor.cs b/YaeBlog.Core/Processors/CodeBlockPostRenderProcessor.cs index 217aa38..2325c07 100644 --- a/YaeBlog.Core/Processors/CodeBlockPostRenderProcessor.cs +++ b/YaeBlog.Core/Processors/CodeBlockPostRenderProcessor.cs @@ -1,11 +1,12 @@ using AngleSharp; using AngleSharp.Dom; +using Microsoft.Extensions.Logging; using YaeBlog.Core.Abstractions; using YaeBlog.Core.Models; namespace YaeBlog.Core.Processors; -public class CodeBlockPostRenderProcessor : IPostRenderProcessor +public class CodeBlockPostRenderProcessor(ILogger logger) : IPostRenderProcessor { public async Task ProcessAsync(BlogEssay essay) { @@ -20,10 +21,34 @@ public class CodeBlockPostRenderProcessor : IPostRenderProcessor foreach (IElement element in preElements) { element.ClassList.Add("p-3 text-bg-secondary rounded-1"); + + IEnumerable codeElements = from e in element.Children + where e.LocalName == "code" + select e; + + foreach (IElement code in codeElements) + { + string? language = (from c in code.ClassList + where c.StartsWith("language-") + select c[9..].ToLower()).FirstOrDefault(); + + if (language is null) + { + continue; + } + + logger.LogDebug("Detect code block of language {}.", language); + code.InnerHtml = HighLightCode(code.InnerHtml, language); + } } return essay.WithNewHtmlContent(document.DocumentElement.OuterHtml); } public string Name => nameof(CodeBlockPostRenderProcessor); + + private static string HighLightCode(string code, string language) + { + return code; + } } diff --git a/YaeBlog.Core/Processors/Scanners/CSharpScanner.cs b/YaeBlog.Core/Processors/Scanners/CSharpScanner.cs new file mode 100644 index 0000000..c5923ef --- /dev/null +++ b/YaeBlog.Core/Processors/Scanners/CSharpScanner.cs @@ -0,0 +1,9 @@ +using static LanguageExt.Prelude; + + +namespace YaeBlog.Core.Processors.Scanners; + +public class CSharpScanner +{ + +} diff --git a/YaeBlog.Core/YaeBlog.Core.csproj b/YaeBlog.Core/YaeBlog.Core.csproj index ef44f03..00cb827 100644 --- a/YaeBlog.Core/YaeBlog.Core.csproj +++ b/YaeBlog.Core/YaeBlog.Core.csproj @@ -12,6 +12,8 @@ + +