mirror of
https://github.com/xoofx/markdig.git
synced 2026-02-04 05:44:50 +00:00
Speed up FencedCodeBlock rendering
This commit is contained in:
@@ -22,4 +22,17 @@ internal static class FrozenDictionaryExtensions
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class FrozenSet<T> : HashSet<T>
|
||||
{
|
||||
public FrozenSet(HashSet<T> set, IEqualityComparer<T> comparer) : base(set, comparer) { }
|
||||
}
|
||||
|
||||
internal static class FrozenSetExtensions
|
||||
{
|
||||
public static FrozenSet<T> ToFrozenSet<T>(this HashSet<T> set, IEqualityComparer<T> comparer)
|
||||
{
|
||||
return new FrozenSet<T>(set, comparer);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -31,11 +31,27 @@ public class CodeBlockRenderer : HtmlObjectRenderer<CodeBlock>
|
||||
/// </summary>
|
||||
public Dictionary<string, string> BlockMapping { get; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
private FrozenSet<string>? _specialBlockMapping;
|
||||
|
||||
private FrozenSet<string> SpecialBlockMapping
|
||||
{
|
||||
get
|
||||
{
|
||||
return _specialBlockMapping ?? CreateNew();
|
||||
|
||||
FrozenSet<string> CreateNew()
|
||||
{
|
||||
HashSet<string> set = [.. BlocksAsDiv, .. BlockMapping.Keys];
|
||||
return _specialBlockMapping = set.ToFrozenSet(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Write(HtmlRenderer renderer, CodeBlock obj)
|
||||
{
|
||||
renderer.EnsureLine();
|
||||
|
||||
if ((obj as FencedCodeBlock)?.Info is string info && (BlocksAsDiv.Contains(info) || BlockMapping.ContainsKey(info)))
|
||||
if ((obj as FencedCodeBlock)?.Info is string info && SpecialBlockMapping.Contains(info))
|
||||
{
|
||||
var infoPrefix = (obj.Parser as FencedCodeBlockParser)?.InfoPrefix ??
|
||||
FencedCodeBlockParser.DefaultInfoPrefix;
|
||||
|
||||
Reference in New Issue
Block a user