dev: CSharp language highlight.

This commit is contained in:
jackfiled 2024-08-28 20:26:41 +08:00
parent 9ebacf87e5
commit d9c17720dc
3 changed files with 37 additions and 1 deletions

View File

@ -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<CodeBlockPostRenderProcessor> logger) : IPostRenderProcessor
{
public async Task<BlogEssay> 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<IElement> 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;
}
}

View File

@ -0,0 +1,9 @@
using static LanguageExt.Prelude;
namespace YaeBlog.Core.Processors.Scanners;
public class CSharpScanner
{
}

View File

@ -12,6 +12,8 @@
<ItemGroup>
<PackageReference Include="AngleSharp" Version="1.1.0" />
<PackageReference Include="LanguageExt.Core" Version="4.4.9" />
<PackageReference Include="LanguageExt.Parsec" Version="4.4.9" />
<PackageReference Include="Markdig" Version="0.34.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.6" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />