-
I'm playing with Mermaid, trying to display a class inheritance diagram (flowchart) in mkdocstrings-python. Here's what I currently have: {% macro edges(class) %}
{% for base in class.resolved_bases %}
{{ base.path }} --> {{ class.path }}
{{ edges(base) }}
{% endfor %}
{% endmacro %}
<pre class="mermaid">
<code>
flowchart TD
{{ class.path }}[<span data-autorefs-optional-hover="{{ class.path }}">{{ class.name }}</span>]
{% for base in class.mro() %}
{{ base.path }}[<span data-autorefs-optional-hover="{{ base.path }}">{{ base.name }}</span>]
{% endfor %}
{{ edges(class) }}
</code>
</pre> The autorefs spans are transformed at the end of the MkDocs build, giving something like: <pre class="mermaid">
<code>
flowchart TD
mkdocstrings.handlers.base.BaseHandler[<a href="https://mkdocstrings.github.io/reference/mkdocstrings/handlers/base/#mkdocstrings.handlers.base.BaseHandler" title="mkdocstrings.handlers.base.BaseHandler">BaseHandler</span>]
<!-- other nodes -->
<!-- edges, nothing fancy here -->
</code>
</pre> Then, when loading the page in the browser, Mermaid transforms the However there several issues here:
I know about Mermaid's interaction capabilities, and since callbacks can receive the node ID, I could probably hack something together by rendering links in a hidden divs, collecting them in javascript and pre-defining callbacks that redirect to the right pages, but that feels a bit heavy and not super elegant. Any hints or idea on how to make all this work 🤔? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
OK this is much better with click callbacks actually. <div style="display: none">
<span data-autorefs-optional-hover="{{ class.path }}" id="mermaid-{{ class.path }}">{{ class.name }}</span>
{% for base in class.mro() %}
<span data-autorefs-optional-hover="{{ base.path }}" id="mermaid-{{ base.path }}">{{ base.name }}</span>
{% endfor %}
</div>
<pre class="mermaid"><code>
flowchart TD
{{ class.path }}[{{ class.name }}]
{% for base in class.mro() %}
{{ base.path }}[{{ base.name }}]
{% endfor %}
{{ edges(class) }}
click {{ class.path }} mermaidClick "{{ class.path }}"
{% for base in class.mro() %}
click {{ base.path }} mermaidClick "{{ base.path }}"
{% endfor %}
</code></pre>
<script>
function mermaidClick(nodeID) {
console.log("Clicked on " + nodeID);
window.location.href = document.getElementById("mermaid-" + nodeID).href;
}
</script> However no event is bound on nodes. I suppose it's because the security option is set to strict and not loose? 🤔 |
Beta Was this translation helpful? Give feedback.
-
OK, instead of entering shadowland, where hyenas would be waiting for my poor lion cub self 🦁, I went with yet another approach. By the time the HTML is rendered from Markdown, I have both the diagram contents, and the links for each node. So I just need to update the diagram code to add these links in the right place (in the click href). I use a regex replace. This is working fine 🙂 let diagram = document.getElementById('mermaid-diagram-{{ class.path }}');
diagram.innerHTML = diagram.innerHTML.replace(/click ([\w.]+) href ""/g, function(match, nodeID, offset) {
let link = document.getElementById("mermaid-link-" + nodeID).href;
return `click ${nodeID} href "${link}"`
}); I could not do this in one go because the code that generates the correct link runs at the end of the MkDocs build, in the Thank you again for your help @kamilkrzyskow! Feel free to close if that's you usually do with resolved discussions 🙂 |
Beta Was this translation helpful? Give feedback.
OK, instead of entering shadowland, where hyenas would be waiting for my poor lion cub self 🦁, I went with yet another approach.
By the time the HTML is rendered from Markdown, I have both the diagram contents, and the links for each node. So I just need to update the diagram code to add these links in the right place (in the click href). I use a regex replace. This is working fine 🙂
I could not d…