michael-o commented on code in PR #619:
URL: 
https://github.com/apache/maven-doxia-sitetools/pull/619#discussion_r2837421734


##########
doxia-site-model/src/main/mdo/site.mdo:
##########
@@ -653,5 +662,102 @@ under the License.
         </field>
       </fields>
     </class>
+
+    <class java.clone="deep">
+      <name>MermaidConfiguration</name>
+      <description>The configuration used for rendering Mermaid diagrams 
(https://mermaid.js.org/) on client-side.</description>
+      <version>2.1.0+</version>
+      <fields>
+        <field xml.tagName="externalJsUrl">
+          <name>externalJs</name>
+          <description>The URL from which to request the Mermaid.js used for 
rendering the diagrams. If not set an embedded version of Mermaid is used 
(which is automatically deployed to the site).</description>
+          <version>2.1.0+</version>
+          <association>
+            <type>ExternalJs</type>
+          </association>
+          <required>false</required>
+          <identifier>true</identifier>
+        </field>
+        <field xml.attribute="true">
+          <name>useTiny</name>
+          <description>If Tiny Mermaid 
(https://github.com/mermaid-js/mermaid/tree/develop/packages/tiny#tiny-mermaid) 
should be used. Only considered if "externalJsUrl" is not set.</description>
+          <version>2.1.0+</version>
+          <type>boolean</type>
+          <required>false</required>
+          <identifier>true</identifier>
+        </field>
+        <field>
+          <name>config</name>
+          <description>The Mermaid configuration in JSON format as described 
in 
https://mermaid.ai/open-source/config/setup/mermaid/interfaces/MermaidConfig.html.</description>
+          <version>2.1.0+</version>
+          <type>String</type>
+          <required>false</required>
+          <identifier>true</identifier>
+        </field>
+      </fields>
+    </class>
+
+    <class java.clone="deep">
+      <name>ExternalJs</name>

Review Comment:
   What is the difference to `externalJs`?



##########
doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/sink/SiteRendererSink.java:
##########
@@ -130,6 +142,122 @@ public void date_() {
         resetTextBuffer();
     }
 
+    @Override
+    public void verbatim(SinkEventAttributes attributes) {
+        if (mermaidConfig != null && 
normalizeClassAttributesForMermaid(attributes)) {
+            containsMermaidDiagram = true;
+            // remove the decoration code for Mermaid diagrams (otherwise 
Skins may add line numbers to the code
+            // element, which breaks Mermaid rendering)
+            SinkEventAttributes filteredAttributes = (SinkEventAttributes)
+                    SinkUtils.filterAttributes(attributes, new String[] 
{SinkEventAttributes.DECORATION});
+            super.verbatim(filteredAttributes);
+        } else {
+            // write subsequent verbatim content to a buffer, to be able to 
detect Mermaid diagrams in it and remove
+            // code element if needed
+            verbatimBuffer = new StringBuilder();
+            super.verbatim(attributes);
+        }
+    }
+
+    @Override
+    public void verbatim_() {
+        flushVerbatimBuffer(false);
+        super.verbatim_();
+    }
+
+    @Override
+    public void inline(SinkEventAttributes attributes) {
+        if (attributes.containsAttributes(SinkEventAttributeSet.Semantics.CODE)
+                && mermaidConfig != null
+                && normalizeClassAttributesForMermaid(attributes)) {
+            containsMermaidDiagram = true;
+            // writes to buffer
+            super.inline(attributes);
+            // remove code element from inline stack to prevent closing it
+            inlineStack.pop();
+            insideMermaidCodeElement = true;
+            // remove the code element from the verbatim buffer, to prevent 
Skins from adding line numbers to it, which
+            // breaks Mermaid rendering
+            flushVerbatimBuffer(true);
+        } else {
+            flushVerbatimBuffer(false);
+            super.inline(attributes);
+        }
+    }
+
+    @Override
+    public void inline_() {
+        if (insideMermaidCodeElement) {
+            // this is the end of the code tag (which has been removed), so no 
need to close it
+            insideMermaidCodeElement = false;
+        } else {
+            super.inline_();
+        }
+    }
+
+    private void flushVerbatimBuffer(boolean stripCodeElement) {
+        if (verbatimBuffer != null) {
+            String buffer = verbatimBuffer.toString();
+            if (stripCodeElement) {
+                // remove code element and instead add attributes to the 
parent element <pre> to prevent the skin from
+                // adding code highlighting/line numbers, which breaks Mermaid 
rendering
+                buffer = buffer.replaceFirst("<pre><code([^>]*)>", "<pre$1>");
+            }
+            verbatimBuffer = null;
+            write(buffer);
+        }
+    }
+
+    /**
+     * Normalize class attributes for Mermaid diagrams, to allow using either 
"mermaid" or "language-mermaid" as class.
+     *
+     * @param attributes the attributes to check and normalize.
+     * @return {@code true} if the attributes indicate a Mermaid diagram, 
{@code false} otherwise.
+     */
+    boolean normalizeClassAttributesForMermaid(SinkEventAttributes attributes) 
{
+        String lang = attributes != null ? (String) 
attributes.getAttribute(SinkEventAttributes.CLASS) : null;
+        if ("language-mermaid"
+                .equals(lang)) { // "language-" prefix is used by some 
markdown parsers, e.g. flexmark-java
+            attributes.addAttribute(SinkEventAttributes.CLASS, "mermaid");
+            return true;
+        } else if ("mermaid".equals(lang)) {
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * Include the Mermaid rendering script (either internal or external) and 
call it on diagrams afterwards.
+     * @see <a 
href="https://mermaid.ai/open-source/intro/getting-started.html#_4-calling-the-mermaid-javascript-api";>Calling
 the Mermaid JavaScript API</a>
+     */
+    private void writeMermaidScript() {
+        if (mermaidConfig.getExternalJs() != null) {
+            write(mermaidConfig.getExternalJs().asScriptTag());
+        } else {
+            write("\n<script src=\"");
+            write(docRenderingContext.getRelativePath());
+
+            if (mermaidConfig.isUseTiny()) {
+                // use integrated tiny version of mermaid, which is smaller 
and faster to load, but has some limitations
+                // (e.g. no sequence diagrams)
+                write("/js/mermaid-" + DefaultSiteRenderer.MERMAID_VERSION + 
".tiny.min.js");
+            } else {
+                // use integrated full version of mermaid, which is larger and 
slower to load, but has all features
+                write("/js/mermaid-" + DefaultSiteRenderer.MERMAID_VERSION + 
".min.js");
+            }
+            write("\"></script>\n");

Review Comment:
   this will create files with mixed line ending and if the site it maintained 
via Git or Subversion, it will fail.



##########
doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/sink/SiteRendererSink.java:
##########
@@ -187,6 +317,10 @@ protected void write(String text) {
             }
         }
 
+        if (verbatimBuffer != null) {
+            verbatimBuffer.append(unifyEOLs(txt));

Review Comment:
   Wouldn't it be better to do it correct right away instead of fixing it?



##########
doxia-site-model/src/main/mdo/site.mdo:
##########
@@ -653,5 +662,102 @@ under the License.
         </field>
       </fields>
     </class>
+
+    <class java.clone="deep">
+      <name>MermaidConfiguration</name>
+      <description>The configuration used for rendering Mermaid diagrams 
(https://mermaid.js.org/) on client-side.</description>

Review Comment:
   It has moved to https://mermaid.ai/open-source



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to