[TechDocs] - Support Mermaid Diagrams Using the Addon Framework
Expected Behavior
Expecting techdocs/backstage to render custom JS / mermaid plugin.

Current Behavior
mkdocs/techdocs is not rendering custom JS / mermaid plugin; instead only the raw instructions are presented

Context
I'm running a custom tech-docs backend with the mermaid extension installed in the local container. For example:
Tech-docs custom container:
...
pip install --quiet \
mkdocs==1.1.2 \
cookiecutter==1.7.2 \
Markdown==3.2.2 \
pygments==2.6.1 \
mkdocs-material \
mkdocs-jupyter \
mkdocs-mermaid2-plugin \
...
Your Environment
mkdocs-techdocs-core 0.0.13 mkdocs-plugin-mermaid 0.1.1
My mkdocs.yml config:
plugins:
- techdocs-core
- mermaid2
extra_javascript:
- https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML
- https://unpkg.com/mermaid/dist/mermaid.min.js
We'll look more into the MkDocs mermaid2 plugin more to see how we can make it work with TechDocs.
But TechDocs loading an external Javascript from its storage and executing it, will be a security threat for a Backstage app. Hence, TechDocs is only allowed to import HTML and CSS.
One possible approach would be to make the files under extra_javascript available in Backstage/TechDocs frontend; and then when the mermaid diagram's HTML is rendered, it would work as expected. But I'm not too sure.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Don't think this should get closed...
@OrkoHunter
Re-opening then! Also cc @backstage/techdocs-core
Thanks Orko!
I want to report a similar limitation when trying to use MathJax with the python-markdown-math extension. I am able to include the necessary scripts as part of the Backstage frontend but MathJax inserts special <script type="math/tex"> tags into the docs to render its math statements. It looks like Techdocs sanitizes the generated HTML through its transformers:https://github.com/backstage/backstage/blob/4eb2729edcaf97700c2233cae8643b3b4ad8bb1b/plugins/techdocs/src/reader/transformers/sanitizeDOM/index.ts#L25-L31
Is it possible to support whitelisting math/tex type script tags? It looks like santizeHTML should be capable of allowing this through its exclusiveFilter config.
Let me know if I should open a separate issue for this.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
@OrkoHunter What is the concern with "TechDocs loading an external Javascript from its storage and executing it"? Isn't that what most Backstage plugins do? I'm not an engineer so I'm probably not understanding something sensible 😊
@OrkoHunter What is the concern with "TechDocs loading an external Javascript from its storage and executing it"? Isn't that what most Backstage plugins do? I'm not an engineer so I'm probably not understanding something sensible 😊
My understanding is that this will enable regular users to include external JS as part of their TechDocs site, opening the risk for anyone to potentially run malicious JS within Backstage. Plugins OTOH are installed explicitly by the Backstage integrators/admins allowing them to audit them to ensure they are safe to run.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Still interested
As we cannot include extra javascript in our techdoc plugin, So is there any alternative through which we can render either Mermaid, draw.io, or C4 in our techdocs?
@lakshikaparihar Yes! With draw.io you can save a png/svg file and render it just like any other image. I am not sure about the others. (ProTip: You can also use the draw.io VSCode extension and draw using your editor ;))
There is also some things that have been added here: #8804 maybe yan can verify that this documentation works?
Explicitly linking this PR as well, for the record: https://github.com/backstage/techdocs-container/pull/26
To unblock our org we are now using the mermaid-cli to render graphs at build time using https://github.com/johanneswuerbach/markdown-inline-mermaid, which was heavily inspired by the graphviz plugin.
With this plugin mermaid can be used by building a custom docker image:
FROM spotify/techdocs@latest
RUN apk --no-cache add nodejs npm chromium
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
RUN npm install -g @mermaid-js/[email protected]
RUN pip install markdown-inline-mermaid==1.0.2
Afterwards you need to enable the extension to your mkdocs.yml:
markdown_extensions:
- markdown_inline_mermaid
Once that is done, you can use mermaid in your docs:
```mermaid
graph TD
A[Client] --> B[Load Balancer]
```
Hello, my users are also interested in rendering mermaid diagrams in their tech-docs, especially-so since github now supports mermaid in it's own markdown.
Originally posted by @dmckernanacv in https://github.com/backstage/mkdocs-techdocs-core/issues/35#issuecomment-1055517634
Hey everyone following along! Quick update on this based on some contributions from the community recently as well as some forthcoming contributions:
- At this time, the
@backstage/techdocs-coreteam doesn't plan to add native mermaid support as a core feature of TechDocs. - If you're interested in build-time support for mermaid-based diagrams, a community member contributed this "How to Add Mermaid Support" guide, which walks through how one could set that up. Johannes' comment above is another good alternative. Both have their drawbacks (heavy dependencies in both cases)
- For those who'd prefer render-time support for their mermaid diagrams (rather than build-time), the forthcoming TechDocs Addon Framework would be a great way to make it happen. Follow along in the RFC here: #9636
With all of that said! Here's what I'm going to do:
- Label this issue
help-wanted(to indicate: core team is not planning on working on this, but we welcome contributions!) - Rename this issue to indicate that a render-time approach is desired (and again, the Addon Framework would be the preferred solution, in that case)
- Encourage other folks interested in a build-time solution supported by TechDocs core to organize here: https://github.com/backstage/mkdocs-techdocs-core/issues/35
Hey folks following along on this thread! Just sending out an update that the Addon framework has reached a milestone: it's not fully released, but it's functional and documented.
If anyone is interested in trying to implement a <MermaidDiagram /> Addon, now's the time to try. Here are my recommendations:
- Have a read through the TechDocs Addon documentation, in particular the Creating an Addon section.
- Check out the Calling the Javascript API section of the Mermaid JS docs. I suspect this is the basic mechanism an Addon might want to hook into.
- Depending on what your implementation looks like, it could be appropriate in this generic contributed addons package, or it could be its own package.
@iamEAP this is amazing. I took a stab at implementing a mermaid addon and it looks fairly promising.
You can find the code here: https://github.com/johanneswuerbach/backstage-plugin-techdocs-addon-mermaid

The main issue I found seems to be that the language property of a markdown code block isn't available in the rendered html, so the plugin currently relies on some regex to find potential diagrams https://github.com/johanneswuerbach/backstage-plugin-techdocs-addon-mermaid/blob/main/src/Mermaid/hooks.ts#L17 🤔