docfx icon indicating copy to clipboard operation
docfx copied to clipboard

Addin for diagrams support

Open pascalberger opened this issue 6 years ago • 28 comments

An addin which adds support for writing diagrams would be nice (e.g. by using mermaid)

pascalberger avatar Dec 11 '17 13:12 pascalberger

Duplicate of #2347

ChristophLindemann avatar Jan 08 '18 14:01 ChristophLindemann

Not necessary a duplicate as #2347 explicitely asks for PlantUML support, which requires additional infrastructure and therefore might not be suitable for every situation. I can also live with any JS implementation (like e.g. the mentioned mermaid).

pascalberger avatar Jan 08 '18 14:01 pascalberger

I'm in desperate need for that so I'm working on a mermaid renderer plugin. I have a working proof of concept (source code)

I'm pretty happy with the experience it give in combination with vs code and that mermaid plugin image

image

Beside code quality (this is a quick and dirty poc) I'd like to provide a bit of options like give the control over rendering parameter somewhere in the docfx.json file (which would translate as additional mermaid arguments). If anyone has a bit experience with docfx and can point me on the right direction I'd love that.

[Solved] ~~In addition I couldn't make it work with pdf generation (see #2427).~~

sandorfr avatar Feb 09 '18 02:02 sandorfr

I've added support for https://www.graphviz.org/ to my branch.

sandorfr avatar Feb 12 '18 06:02 sandorfr

If anyone is interested in giving feedback or anything, I've made the packages available here: https://www.myget.org/F/gl-unstable/api/v3/index.json

you need to install mermaid cli and graphviz

nuget install mermaid.renderer -OutputDirectory <Output>
nuget install graphviz.renderer -OutputDirectory <Output>
 "template": [
      "default",
      "<Output>/mermaid.renderer.1.0.0/content",
      "<Output>/graphviz.renderer.1.0.0/content"
    ],

sandorfr avatar Feb 12 '18 23:02 sandorfr

Hi! I would like to test your plugin but i need some instruction how to install it on my setup. I am using Windows 10 with VS2017. Thanks!

devLucius avatar Apr 04 '18 13:04 devLucius

@devLucius

As mentionned above you need to install the prerequisities (mermaid cli and graphviz).

Then the steps are the same as with an other plugins, except for the source which is from another feed which you'll need to register first ( https://docs.microsoft.com/en-us/nuget/tools/cli-ref-sources ).

In addition, I'd highly recommand using Visual Studio Code instead as you will have the live preview for both mermaid and graphviz with those vs code plugins:

  • https://marketplace.visualstudio.com/items?itemName=bierner.markdown-mermaid
  • https://marketplace.visualstudio.com/items?itemName=geeklearningio.graphviz-markdown-preview

sandorfr avatar Apr 05 '18 05:04 sandorfr

A little legwork but you can edit the HTML after running DocFX to add Mermaid diagrams. Manual method to add Mermaid flow charts

  1. Created a markdown file in StackEdit.io with Mermaid Flow Chart Diagrams.
  2. Exported the markdown and put it in the article directory; updated toc.
  3. Run DocFX
  4. Exported the markdown file from StackEdit.io as a styled HTML file. (This displays the diagram correctly.)
  5. Opened the downloaded styled HTML file and docfx/_site's version in NotePadd++.
  6. Copied <link rel="stylesheet" href="https://stackedit.io/style.css" /> into the docFX version's <head> tag.
  7. Found the code for the diagram in the StackEdit.io version and copied that to the corresponding location.
  8. The diagram boxes' label text was small and white (done by DocFX overriding its style), so I replaced all class="label" with nothing, to delete that class.
  9. docfx.exe serve _site; open localhost in a browser; Now Works!

Just figured this out and haven't tried to optimise this process. Hopefully, it can provide some insight for automation or at least provide a temporary solution to add handcrafted Mermaid Flow Charts (haven't tested Gantt or sequence).

However, you will have to do this every time you rebuild or store that page separately. In my case it was an article, so I didn't have too much of a problem but the API Documentation doesn't really integrate well.

It would be really cool if DocFx automatically created these kinds of diagrams; that can show class relation/dependency, business logic, and more. Is that what you asked for, @pascalberger?

@sandorfr, I noticed your version displayed the diagram item labels with white text, which isn't easy to read. docfx.vendor.css line 1086 .label{display:inline;padding:.2em .6em .3em;font-size:75%;color:#fff;border-radius:.25em} was what was changing my labels, that was fixed by removing class="label" from the HTML file.

Zenith-Convoy avatar Mar 02 '19 17:03 Zenith-Convoy

@sandorfr I tried to install your plugin, without success:

nuget install mermaid.renderer -OutputDirectory Documentation/Plugins/
Feeds used:
  https://api.nuget.org/v3/index.json
  C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\

Installing package 'mermaid.renderer' to 'Documentation/Plugins/'.
  GET https://api.nuget.org/v3/registration3-gz-semver2/mermaid.renderer/index.json
  NotFound https://api.nuget.org/v3/registration3-gz-semver2/mermaid.renderer/index.json 506ms
Unable to find package 'mermaid.renderer'

NormandErwan avatar Jun 26 '19 11:06 NormandErwan

Because you are not fetching from the right source --> https://www.myget.org/F/gl-unstable/api/v3/index.json

sandorfr avatar Jun 27 '19 04:06 sandorfr

Thank you, you already explained this above, I haven't caught it.

You should consider writing a quick README on your repo !

NormandErwan avatar Jun 27 '19 08:06 NormandErwan

Simple add mermaid for docfx as:

  1. add mermaid.min.js to your docfx template
  2. Edit article file just add
<div class="mermaid">
graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;
</div>

now render page using docfx and graph appear.

EurekaChen avatar Aug 17 '19 01:08 EurekaChen

@sandorfr Just tried your plugins, but:

Error:[BuildCore.Build Document.CompilePhaseHandlerWithIncremental.ConceptualDocumentProcessor.Build.BuildConceptualDocument](<MYFILE>)Markup failed: System cannot find the file specified (translated)

Logerfo avatar Oct 03 '19 13:10 Logerfo

I guess it’s because mermaid cli has been renamed. I have not really had much time to spend on this since POC. Docfx being in limbo between .net core and .net, I’m not keen to invest time on it.

sandorfr avatar Oct 04 '19 23:10 sandorfr

You can use mermaid API to resolve this issue.

Add this to your docfx template. I do it on scripts.tmpl.partial.

<!-- mermaid support -->
<script src="https://unpkg.com/[email protected]/dist/mermaid.min.js"></script>
<script>
    mermaid.initialize({
        startOnLoad: false
    });

    window.onload = function () {
        const elements = document.getElementsByClassName("lang-mermaid");
        let index = 0;

        while (elements.length != 0) {
            let element = elements[0];
            mermaid.render('graph'+index, element.innerText, (svgGraph) => {                    
                element.parentElement.outerHTML = "<div class='mermaid'>" + svgGraph + "</div>";
            });
            ++index;
        }
    }
</script>

Then on your md file, you can use normal markdown markup to define a graph.


This should display 2 graph generated by mermaidjs

``` mermaid
graph LR
    A -- text --> B -- text2 --> C
```

``` mermaid
sequenceDiagram
    Alice->John: Hello John, how are you?
    Note over Alice,John: A typical interaction
```

sachabruttin avatar Dec 05 '19 16:12 sachabruttin

Hello @sachabruttin , Your solution works well to display mermaid design. This is fantastic. However, as soon as I add your partial script part to the default template the navigation bar in top of the screen disappear. I'm new on docfx (started yesterday) and I don't know why. Here is what I did:

  1. I create a mytemplate folder
  2. I create a partials folder in my template one.
  3. I create a file named scripts.tmpl.partial
  4. I put your code in it
  5. I updated the docfx.json file "template": ["default","mytemplate"],
  6. I launched docfx
  7. I launched docfx serve _site

When I reach the first page, I do not have the navigation part on top of the screen. I should have "Article", "Tutorials", etc. If I open the page where I put the mermaid design (by entering the url) it appears well

Any idea what I did wrong?

Addendum: I try to reduce the file to just and I still have the issue.

Thank you.

huguesr avatar Dec 12 '19 17:12 huguesr

I found the solution. You have to add the following lines at the begining of the code

huguesr avatar Dec 12 '19 18:12 huguesr

@sachabruttin Im able to work with you approach for the html side. For the pdf it doesn't seems to work. I did exactly the same for the pdf as i did for the html site.

Corji avatar Feb 10 '20 16:02 Corji

A simplified version of @sachabruttin solution with the latest version of mermaid.min.js.

<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/mermaid.min.js"
        integrity="sha384-dV0RNdYNgPqjamYrB83Q21NNeuYk1jpKyUBkKQrT+c7krNFk4k39ktYZV4mOdMV1"
        crossorigin="anonymous"></script>
<script>
    mermaid.initialize({
        startOnLoad: false
    });
    mermaid.init(undefined, ".lang-mermaid");
</script>

christianlegault avatar Jun 23 '20 21:06 christianlegault

Interesting thread:

https://twitter.com/bradygaster/status/1375147944760930305

https://fluidframework.com/docs/advanced/summarizer/

Fluid uses Mermaid in their docs, with accessibility addressed:

https://github.com/tylerbutler/FluidFramework/commit/2597379ae57b5a01aee33fff9482d64ae1212c01#diff-cf2a02b7ba760f1160fc918b8f2b175dad122a4fd64790689e7cf6e3b9ed0fed

AlexHedley avatar Mar 27 '21 09:03 AlexHedley

Will this be available anytime soon?

neeraj9 avatar Sep 03 '21 02:09 neeraj9

Just in case this feature is being de-proritized due to not appearing relevant, Microsoft have themselves demonstrated a requirement for diagram support. In particular if you look at https://docs.microsoft.com/en-us/aspnet/core/blazor/components/lifecycle?view=aspnetcore-6.0 there is clearly a need to be able to show lifecycle flowcharts etc without resorting to slightly-skewed (scanned?) images etc which are then hard to update.

webprofusion-chrisc avatar May 05 '22 06:05 webprofusion-chrisc

I had a need to add this in. Just like everyone else is saying - markdown diagram support is quickly becoming a standard and mermaid is a top contender.

Here is the content of templates/mermaid/partials/scripts.tmpl.partial to use Mermaid v9.1.3.

<!-- Required styles -->
<script type="text/javascript" 
        src="{{_rel}}styles/docfx.vendor.js"></script>
<script type="text/javascript" 
        src="{{_rel}}styles/docfx.js"></script>
<script type="text/javascript" 
        src="{{_rel}}styles/main.js"></script>
<!-- Load v9.1.3 of Mermaid -->
<!-- Hash obtained via https://www.srihash.org/ -->
<script type="text/javascript" 
        src="https://unpkg.com/[email protected]/dist/mermaid.min.js"
        integrity="sha384-LnGjpNDrP4cp7MIk4CpRa/lPNclrf839ryYVFx1T1mPSV3RGAZ7nlBa7pqcyGY/K" 
        crossorigin="anonymous"></script>
<!-- Initialize Mermaid -->
<script>
    mermaid.initialize({
        startOnLoad: false
    });
    mermaid.init(undefined, ".lang-mermaid");
</script>

PS don't forget to add "templates/mermaid" to the templates section of your docfx.json file.

jldubz avatar Jun 28 '22 19:06 jldubz

@jldubz That works, but you also need font-awesome to get icons working: https://mermaid-js.github.io/mermaid/#/flowchart?id=basic-support-for-fontawesome

Adding this is sufficient:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" />

timstokman avatar Jul 04 '22 23:07 timstokman

@timstokman

Adding this is sufficient:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" />

Can you provide a full example of where this line is being added (full file path and file content is optimal) to avoid any confusion?

jldubz avatar Jul 06 '22 13:07 jldubz

@jldubz This is my scripts.tmpl.partial file:

<!-- Required styles -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" />
<script type="text/javascript" 
        src="{{_rel}}styles/docfx.vendor.js"></script>
<script type="text/javascript" 
        src="{{_rel}}styles/docfx.js"></script>
<script type="text/javascript" 
        src="{{_rel}}styles/main.js"></script>
<!-- Load v9.1.3 of Mermaid -->
<!-- Hash obtained via https://www.srihash.org/ -->
<script type="text/javascript" 
        src="https://unpkg.com/[email protected]/dist/mermaid.min.js"
        integrity="sha384-LnGjpNDrP4cp7MIk4CpRa/lPNclrf839ryYVFx1T1mPSV3RGAZ7nlBa7pqcyGY/K" 
        crossorigin="anonymous"></script>
<!-- Initialize Mermaid -->
<script>
    mermaid.initialize({
        startOnLoad: false
    });
    mermaid.init(undefined, ".lang-mermaid");
</script>

Without font-awesome included, the mermaid icon functionality doesn't work.

timstokman avatar Jul 06 '22 14:07 timstokman

So at some point this template stopped working for me. Took me a bit to find out why.

If you have multiple templates, like here, the latest scripts.tmpl.partial overwrites the one from the main template. So including mermaid like this will break the main template (which broke about everything for me). You'll need to include the main template's scripts.tmpl.partial, like this:

<!-- Required styles -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
<script type="text/javascript" src="{{_rel}}styles/docfx.vendor.js"></script>
<script type="text/javascript" src="{{_rel}}styles/docfx.js"></script>
<script type="text/javascript" src="{{_rel}}styles/main.js"></script>
<!-- Load Mermaid -->
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<!-- Initialize Mermaid -->
<script>
    mermaid.initialize({
        startOnLoad: false
    });
    mermaid.init(undefined, ".lang-mermaid");
</script>
<script type="text/javascript" src="{{_rel}}styles/docfx.vendor.min.js"></script>
<script type="text/javascript" src="{{_rel}}styles/docfx.js"></script>
<script type="text/javascript" src="{{_rel}}styles/main.js"></script>

Bit of a hack, but it works. Might be nice if there was better support for stuff like this in docfx.

timstokman avatar Apr 05 '23 12:04 timstokman