data-repl="true" attribute. The REPL plugin later enhances these with Run buttons and output panels." /> data-repl="true" attribute. The REPL plugin later enhances these with Run buttons and output panels." /> data-repl="true" attribute. The REPL plugin later enhances these with Run buttons and output panels." />
Class Sealed
public sealed class ReplExtension : IMarkdownExtension

Namespace: Moka.Docs.Parsing.Markdown

Markdig extension that renders fenced code blocks with the "csharp-repl" (or "csharp repl") language as interactive REPL containers with a data-repl="true" attribute. The REPL plugin later enhances these with Run buttons and output panels.

Remarks

This extension is designed to coexist with MermaidExtension by wrapping whatever CodeBlock renderer is currently in the pipeline (including MermaidCodeBlockRenderer). Registration order: Mermaid first, then REPL.

Inheritance

Inherits from: IMarkdownExtension

Methods

Type Relationships
classDiagram
                    style ReplExtension fill:#f9f,stroke:#333,stroke-width:2px
                    ReplExtension --|> IMarkdownExtension : inherits
                
View Source
/// <summary>
///     Markdig extension that renders fenced code blocks with the "csharp-repl" (or "csharp repl")
///     language as interactive REPL containers with a <c>data-repl="true"</c> attribute.
///     The REPL plugin later enhances these with Run buttons and output panels.
/// </summary>
/// <remarks>
///     This extension is designed to coexist with <see cref = "MermaidExtension"/> by wrapping
///     whatever CodeBlock renderer is currently in the pipeline (including MermaidCodeBlockRenderer).
///     Registration order: Mermaid first, then REPL.
/// </remarks>
public sealed class ReplExtension : IMarkdownExtension
{
    /// <inheritdoc/>
    public void Setup(MarkdownPipelineBuilder pipeline)
    {
    // No block parser changes needed — we reuse the built-in FencedCodeBlock parser.
    }

    /// <inheritdoc/>
    public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
    {
        if (renderer is HtmlRenderer htmlRenderer)
        {
            // Find whatever renderer currently handles CodeBlock (could be default
            // CodeBlockRenderer or MermaidCodeBlockRenderer). We wrap it so both
            // Mermaid and REPL code blocks are handled correctly.
            IMarkdownObjectRenderer? existingRenderer = null;
            foreach (IMarkdownObjectRenderer r in htmlRenderer.ObjectRenderers)
            {
                if (r is HtmlObjectRenderer<CodeBlock> codeBlockHandler)
                {
                    existingRenderer = codeBlockHandler;
                    break;
                }
            }

            if (existingRenderer != null)
            {
                htmlRenderer.ObjectRenderers.Remove(existingRenderer);
            }

            htmlRenderer.ObjectRenderers.AddIfNotAlready(new ReplCodeBlockRenderer(existingRenderer as HtmlObjectRenderer<CodeBlock>));
        }
    }
}
Was this page helpful?