using ColorCode.Styling;
using Markdig.Renderers;
using Markdig.Renderers.Wpf;
namespace Markdig.Wpf.ColorCode;
public class ColorCodeWpfExtension : IMarkdownExtension
{
private readonly StyleDictionary _styleDictionary;
///
/// Creates a new with the specified .
///
/// A dictionary indicating how to style the code.
public ColorCodeWpfExtension(StyleDictionary styleDictionary) => _styleDictionary = styleDictionary;
///
/// Sets up this extension for the specified pipeline.
///
/// The pipeline.
public void Setup(MarkdownPipelineBuilder pipeline)
{
}
///
/// Sets up this extension for the specified renderer.
///
/// The pipeline used to parse the document.
/// The renderer.
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
{
if (renderer is not WpfRenderer wpfRenderer)
{
return;
}
var codeBlockRenderer = wpfRenderer.ObjectRenderers.FindExact();
if (codeBlockRenderer != null)
{
wpfRenderer.ObjectRenderers.Remove(codeBlockRenderer);
}
else
{
codeBlockRenderer = new CodeBlockRenderer();
}
wpfRenderer.ObjectRenderers.AddIfNotAlready(
new ColorCodeBlockRenderer(
codeBlockRenderer,
_styleDictionary
)
);
}
}