backstage icon indicating copy to clipboard operation
backstage copied to clipboard

[TechDocs] - Support Mermaid Diagrams Using the Addon Framework

Open nmatare opened this issue 5 years ago • 44 comments

Expected Behavior

Expecting techdocs/backstage to render custom JS / mermaid plugin.

image

Current Behavior

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

image

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

nmatare avatar Jan 17 '21 20:01 nmatare

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.

OrkoHunter avatar Jan 17 '21 21:01 OrkoHunter

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.

stale[bot] avatar Mar 29 '21 09:03 stale[bot]

Don't think this should get closed...

@OrkoHunter

nmatare avatar Apr 05 '21 21:04 nmatare

Re-opening then! Also cc @backstage/techdocs-core

OrkoHunter avatar Apr 12 '21 10:04 OrkoHunter

Thanks Orko!

nmatare avatar Apr 12 '21 14:04 nmatare

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.

kuangp avatar Jun 03 '21 18:06 kuangp

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.

stale[bot] avatar Aug 02 '21 21:08 stale[bot]

@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 😊

dtuite avatar Oct 04 '21 22:10 dtuite

@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.

kuangp avatar Oct 10 '21 20:10 kuangp

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.

github-actions[bot] avatar Dec 09 '21 20:12 github-actions[bot]

Still interested

kuangp avatar Dec 09 '21 20:12 kuangp

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 avatar Dec 27 '21 05:12 lakshikaparihar

@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 ;))

OrkoHunter avatar Jan 05 '22 11:01 OrkoHunter

There is also some things that have been added here: #8804 maybe yan can verify that this documentation works?

benjdlambert avatar Jan 10 '22 16:01 benjdlambert

Explicitly linking this PR as well, for the record: https://github.com/backstage/techdocs-container/pull/26

iamEAP avatar Jan 10 '22 17:01 iamEAP

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]
```

johanneswuerbach avatar Jan 11 '22 17:01 johanneswuerbach

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

dmckernanacv avatar Mar 01 '22 14:03 dmckernanacv

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-core team 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

iamEAP avatar Mar 15 '22 16:03 iamEAP

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:

iamEAP avatar Apr 27 '22 14:04 iamEAP

@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

image

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 🤔

johanneswuerbach avatar Apr 27 '22 22:04 johanneswuerbach