Compare commits
2 Commits
master
...
feat-highl
Author | SHA1 | Date | |
---|---|---|---|
4fd464fd34 | |||
d9c17720dc |
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
9
YaeBlog.Core/Processors/Scanners/CSharpScanner.cs
Normal file
9
YaeBlog.Core/Processors/Scanners/CSharpScanner.cs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
using static LanguageExt.Prelude;
|
||||||
|
|
||||||
|
|
||||||
|
namespace YaeBlog.Core.Processors.Scanners;
|
||||||
|
|
||||||
|
public class CSharpScanner
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -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" />
|
||||||
|
|
Loading…
Reference in New Issue
Block a user