SHA256
46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using Markdig;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace GCBoK.WebStaticBuilder;
|
|
|
|
/// <summary>Markdown processing with Markdig and post-processing.</summary>
|
|
public static class MarkdownProcessor
|
|
{
|
|
private static readonly Lazy<MarkdownPipeline> Pipeline = new(() =>
|
|
new MarkdownPipelineBuilder().UseAdvancedExtensions().Build());
|
|
|
|
public static string RenderMarkdown(string content)
|
|
{
|
|
var pipeline = Pipeline.Value;
|
|
return Markdown.ToHtml(content, pipeline);
|
|
}
|
|
|
|
public static string MermaidPostprocess(string html)
|
|
{
|
|
var pattern1 = @"<pre><code class=""language-mermaid"">(.*?)</code></pre>";
|
|
var regex1 = new Regex(pattern1, RegexOptions.Singleline);
|
|
html = regex1.Replace(html, m =>
|
|
{
|
|
var code = m.Groups[1].Value
|
|
.Replace("<", "<").Replace(">", ">").Replace("&", "&");
|
|
return $"<div class=\"mermaid\">{code}</div>";
|
|
});
|
|
|
|
var pattern2 = @"<pre class=""mermaid"">(.*?)</pre>";
|
|
var regex2 = new Regex(pattern2, RegexOptions.Singleline);
|
|
html = regex2.Replace(html, m =>
|
|
{
|
|
var code = m.Groups[1].Value
|
|
.Replace("<", "<").Replace(">", ">").Replace("&", "&");
|
|
return $"<div class=\"mermaid\">{code}</div>";
|
|
});
|
|
|
|
return html;
|
|
}
|
|
|
|
public static string TablePostprocess(string html)
|
|
{
|
|
return html.Replace("<table>", "<table class=\"gc-table\">");
|
|
}
|
|
}
|