Compare commits

...

2 Commits

Author SHA1 Message Date
4fd464fd34 Merge branch 'master' into feat-highlight 2024-09-08 22:44:57 +08:00
d9c17720dc dev: CSharp language highlight. 2024-08-28 20:26:41 +08:00
3 changed files with 37 additions and 1 deletions

View File

@ -1,11 +1,12 @@
using AngleSharp; using AngleSharp;
using AngleSharp.Dom; using AngleSharp.Dom;
using Microsoft.Extensions.Logging;
using YaeBlog.Core.Abstractions; using YaeBlog.Core.Abstractions;
using YaeBlog.Core.Models; using YaeBlog.Core.Models;
namespace YaeBlog.Core.Processors; namespace YaeBlog.Core.Processors;
public class CodeBlockPostRenderProcessor : IPostRenderProcessor public class CodeBlockPostRenderProcessor(ILogger<CodeBlockPostRenderProcessor> logger) : IPostRenderProcessor
{ {
public async Task<BlogEssay> ProcessAsync(BlogEssay essay) public async Task<BlogEssay> ProcessAsync(BlogEssay essay)
{ {
@ -20,10 +21,34 @@ public class CodeBlockPostRenderProcessor : IPostRenderProcessor
foreach (IElement element in preElements) foreach (IElement element in preElements)
{ {
element.ClassList.Add("p-3 text-bg-secondary rounded-1"); 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); return essay.WithNewHtmlContent(document.DocumentElement.OuterHtml);
} }
public string Name => nameof(CodeBlockPostRenderProcessor); 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> <ItemGroup>
<PackageReference Include="AngleSharp" Version="1.1.0" /> <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="Markdig" Version="0.34.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.6" /> <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.6" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />