diff --git a/src/Markdig.Tests/Specs/DiagramsSpecs.generated.cs b/src/Markdig.Tests/Specs/DiagramsSpecs.generated.cs index 284183af1..fe78a153e 100644 --- a/src/Markdig.Tests/Specs/DiagramsSpecs.generated.cs +++ b/src/Markdig.Tests/Specs/DiagramsSpecs.generated.cs @@ -17,7 +17,7 @@ public class TestExtensionsMermaidDiagrams // // ## Mermaid diagrams // - // Using a fenced code block with the `mermaid` language info will output a `
` instead of a `pre/code` block: + // Using a fenced code block with the `mermaid` language info will output a `
` block (which is the default for other code block):
         [Test]
         public void ExtensionsMermaidDiagrams_Example001()
         {
@@ -34,14 +34,14 @@ public void ExtensionsMermaidDiagrams_Example001()
             //     ```
             //
             // Should be rendered as:
-            //     
graph TD; + //
graph TD;
             //         A-->B;
             //         A-->C;
             //         B-->D;
             //         C-->D;
-            //     
+ //
- TestParser.TestSpec("```mermaid\ngraph TD;\n A-->B;\n A-->C;\n B-->D;\n C-->D;\n```", "
graph TD;\n A-->B;\n A-->C;\n B-->D;\n C-->D;\n
", "diagrams|advanced", context: "Example 1\nSection Extensions / Mermaid diagrams\n"); + TestParser.TestSpec("```mermaid\ngraph TD;\n A-->B;\n A-->C;\n B-->D;\n C-->D;\n```", "
graph TD;\n    A-->B;\n    A-->C;\n    B-->D;\n    C-->D;\n
", "diagrams|advanced", context: "Example 1\nSection Extensions / Mermaid diagrams\n"); } } diff --git a/src/Markdig.Tests/Specs/DiagramsSpecs.md b/src/Markdig.Tests/Specs/DiagramsSpecs.md index 495c8fbca..1544d5853 100644 --- a/src/Markdig.Tests/Specs/DiagramsSpecs.md +++ b/src/Markdig.Tests/Specs/DiagramsSpecs.md @@ -4,7 +4,7 @@ Adds support for diagrams extension: ## Mermaid diagrams -Using a fenced code block with the `mermaid` language info will output a `
` instead of a `pre/code` block: +Using a fenced code block with the `mermaid` language info will output a `
` block (which is the default for other code block):
 
 ```````````````````````````````` example
 ```mermaid
@@ -15,12 +15,12 @@ graph TD;
     C-->D;
 ```
 .
-
graph TD; +
graph TD;
     A-->B;
     A-->C;
     B-->D;
     C-->D;
-
+
```````````````````````````````` ## nomnoml diagrams diff --git a/src/Markdig/Extensions/Diagrams/DiagramExtension.cs b/src/Markdig/Extensions/Diagrams/DiagramExtension.cs index b04e889a1..5381a3e1a 100644 --- a/src/Markdig/Extensions/Diagrams/DiagramExtension.cs +++ b/src/Markdig/Extensions/Diagrams/DiagramExtension.cs @@ -1,5 +1,5 @@ // Copyright (c) Alexandre Mutel. All rights reserved. -// This file is licensed under the BSD-Clause 2 license. +// This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. using Markdig.Renderers; @@ -22,9 +22,8 @@ public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) if (renderer is HtmlRenderer htmlRenderer) { var codeRenderer = htmlRenderer.ObjectRenderers.FindExact()!; - // TODO: Add other well known diagram languages - codeRenderer.BlocksAsDiv.Add("mermaid"); + codeRenderer.BlockMapping["mermaid"] = "pre"; codeRenderer.BlocksAsDiv.Add("nomnoml"); } } -} \ No newline at end of file +} diff --git a/src/Markdig/Renderers/Html/CodeBlockRenderer.cs b/src/Markdig/Renderers/Html/CodeBlockRenderer.cs index 87bb1a435..b4fb0caeb 100644 --- a/src/Markdig/Renderers/Html/CodeBlockRenderer.cs +++ b/src/Markdig/Renderers/Html/CodeBlockRenderer.cs @@ -1,5 +1,5 @@ // Copyright (c) Alexandre Mutel. All rights reserved. -// This file is licensed under the BSD-Clause 2 license. +// This file is licensed under the BSD-Clause 2 license. // See the license.txt file in the project root for more information. using Markdig.Parsers; @@ -13,8 +13,6 @@ namespace Markdig.Renderers.Html; /// public class CodeBlockRenderer : HtmlObjectRenderer { - private HashSet? _blocksAsDiv; - /// /// Initializes a new instance of the class. /// @@ -25,23 +23,32 @@ public CodeBlockRenderer() { } /// /// Gets a map of fenced code block infos that should be rendered as div blocks instead of pre/code blocks. /// - public HashSet BlocksAsDiv => _blocksAsDiv ??= new HashSet(StringComparer.OrdinalIgnoreCase); + public HashSet BlocksAsDiv { get; } = new HashSet(StringComparer.OrdinalIgnoreCase); + + /// + /// Gets a map of custom block mapping to render as custom blocks instead of pre/code blocks. + /// For example defining {"mermaid", "pre"} will render a block with info `mermaid` as a `pre` block but without the code HTML element. + /// + public Dictionary BlockMapping { get; } = new Dictionary(StringComparer.OrdinalIgnoreCase); protected override void Write(HtmlRenderer renderer, CodeBlock obj) { renderer.EnsureLine(); - if (_blocksAsDiv is not null && (obj as FencedCodeBlock)?.Info is string info && _blocksAsDiv.Contains(info)) + if ((obj as FencedCodeBlock)?.Info is string info && (BlocksAsDiv.Contains(info) || BlockMapping.ContainsKey(info))) { var infoPrefix = (obj.Parser as FencedCodeBlockParser)?.InfoPrefix ?? FencedCodeBlockParser.DefaultInfoPrefix; + var htmlBlock = BlockMapping.TryGetValue(info, out var blockType) ? blockType : "div"; + // We are replacing the HTML attribute `language-mylang` by `mylang` only for a div block // NOTE that we are allocating a closure here if (renderer.EnableHtmlForBlock) { - renderer.Write(" cls.StartsWith(infoPrefix, StringComparison.Ordinal) ? cls.Substring(infoPrefix.Length) : cls) .WriteRaw('>'); @@ -51,7 +58,7 @@ protected override void Write(HtmlRenderer renderer, CodeBlock obj) if (renderer.EnableHtmlForBlock) { - renderer.WriteLine("
"); + renderer.Write(""); } } else diff --git a/src/markdig.sln.DotSettings b/src/markdig.sln.DotSettings index ca5af252d..baf9a14b7 100644 --- a/src/markdig.sln.DotSettings +++ b/src/markdig.sln.DotSettings @@ -3,7 +3,9 @@ This file is licensed under the BSD-Clause 2 license. See the license.txt file in the project root for more information. <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + <Policy><Descriptor Staticness="Instance" AccessRightKinds="Private" Description="Instance fields (private)"><ElementKinds><Kind Name="FIELD" /><Kind Name="READONLY_FIELD" /></ElementKinds></Descriptor><Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /></Policy> True + True TestFolder True True