109

I want to publish some programming documentation I have in a public available repository. This documentation has formatted text, some UML diagrams, and a lot of code examples. I think that GitHub or GitLab are good places to publish this.

To publish the UML diagrams, I would like to have some easy way to keep them updated into the repository and visible as images in the wiki. I don't want to keep the diagrams in my computer (or on the cloud), edit them, generate an image, and then publish it every time.

Is there a way to put the diagrams in the repository (in PlantUML syntax would be ideal), link them in the markdown text, and make the images auto-update every time the diagram is updated?

joanq
  • 1,416
  • 2
  • 10
  • 12

10 Answers10

96

Edit: Alternative with Proxy service

This way is significantly different and simpler than the answer below; it uses the PlantUML proxy service:

http://www.plantuml.com/plantuml/proxy?cache=no&src=https://raw.github.com/plantuml/plantuml-server/master/src/main/webapp/resource/test2diagrams.txt

The GitHub markdown for this would be:

![alternative text](http://www.plantuml.com/plantuml/proxy?cache=no&src=https://raw.github.com/plantuml/plantuml-server/master/src/main/webapp/resource/test2diagrams.txt)

This method suffers from not being able to specify the SVG format (it defaults to PNG), and it is perhaps not possible to work-around the caching bug mentioned in the comments.


After trying the other answer, I discovered the service to be slow and seemingly not up to the latest version of PlantUML.

I've found a different way that's not quite as straightforward, but it works via PlantUML.com's server (in the cloud). As such, it should work anywhere you can hotlink to an image.

It exploits the !includeurl function and is essentially an indirection. The markdown file links to a PlantUML source that includes the diagram's source. This method allows modifying the source in GitHub, and any images in the GitHub markdown files will automatically update. But it requires a tricky step to create the URL to the indirection.

Diagram of how it works

  1. Get the URL to the raw PlantUML source, e.g., https://raw.githubusercontent.com/linux-china/plantuml-gist/master/src/main/uml/plantuml_gist.puml (using the example in the joanq's answer)

  2. Go to http://plantuml.com/plantuml/form (or PlantText.com) and create a one-line PlantUML source that uses the !includeurl URL-TO-RAW-PLANTUML-SOURCE-ON-GITHUB operation. Continuing with the example URL, the PlantUML (meta)source is:

    !includeurl https://raw.githubusercontent.com/linux-china/plantuml-gist/master/src/main/uml/plantuml_gist.puml
    
  3. Copy the image URL from PlantUML.com's image, e.g., http://plantuml.com:80/plantuml/png/FSfB2e0m303Hg-W1RFPUHceiDf36aWzwVEl6tOEPcGGvZXBAKtNljW9eljD9NcCFAugNU15FU3LWadWMh2GPEcVnQBoSP0ujcnS5KnmaWH7-O_kEr8TU and paste it into your GitHub markdown file. This URL won't change.

    ![PlantUML model](http://plantuml.com:80/plantuml/png/3SNB4K8n2030LhI0XBlTy0YQpF394D2nUztBtfUHrE0AkStCVHu0WP_-MZdhgiD1RicMdLpXMJCK3TC3o2iEDwHSxvNVjWNDE43nv3zt731SSLbJ7onzbyeF)
    

Bonus: You can even get access to the SVG format by modifying the plantuml/png/ part of the URL to be plantuml/svg/ as follows

![PlantUML model](http://plantuml.com:80/plantuml/svg/3SNB4K8n2030LhI0XBlTy0YQpF394D2nUztBtfUHrE0AkStCVHu0WP_-MZdhgiD1RicMdLpXMJCK3TC3o2iEDwHSxvNVjWNDE43nv3zt731SSLbJ7onzbyeF)

Example on GitHub

https://github.com/fuhrmanator/course-activity-planner/blob/master/ooad/overview.md

Caveat with private repos

As davidbak pointed out in a comment, the raw file in a private repo will have a URL with token=<LONGSTRINGHERE> in it, and this token changes as the source file updates. Unfortunately, the markdown breaks when this happens, so you have to update the Readme file after you commit the file to GitHub, which is not a great solution.

Fuhrmanator
  • 11,459
  • 6
  • 62
  • 111
  • I'm not sure it's perfect, because GitHub caches images from `.md` files in its camo.githubusercontent.com site. I currently have a use-case diagram I updated, but the updates didn't get to the `.md` file after several days, even when I updated other parts of the `.md` file. I may have to touch the parts that refer to the image. YMMV. – Fuhrmanator Oct 04 '15 at 17:00
  • 6
    Indeed, GitHub seems to only update its cached `camo` site images when the URL changes to the image. A way to update the URL without actually changing it is to add/remove `.svg` (or `.png` if you're using that format). PlantUML doesn't care if you have that extension at the end, but GitHub will think you changed the URL and update its cache in `camo.`. See https://github.com/fuhrmanator/course-activity-planner/commit/45fd5d86153003092593cd028f3fded1eb60917c for details. – Fuhrmanator Oct 04 '15 at 17:07
  • I don't think that GitLab is caching images the same way, so there shouldn't be a problem in my case, but it's good to know how to solve it just in case. – joanq Oct 06 '15 at 08:59
  • 5
    to avoid caching just add some parameter to the url e.g. http://www.plantuml.com/plantuml/proxy?src=https://raw.github.com/plantuml/plantuml-server/master/src/main/webapp/resource/test2diagrams.txt?ttt=1 and increment it each time the uml file(test2diagrams.txt) is modified – ilya1245 May 05 '17 at 10:30
  • To avoid caching `cache=no` url parameter should be added: http://www.plantuml.com/plantuml/proxy?cache=no&src=https://raw.github.com/plantuml/plantuml-server/master/src/main/webapp/resource/test2diagrams.txt – hal Mar 26 '19 at 18:41
  • @hal The caching problem is with GitHub and not PlantUML, so I'm not sure how that cache flag helps. – Fuhrmanator Mar 27 '19 at 11:06
  • 2
    @Fuhrmanator, caching problem is always browser issue, not GitHub or PlantUML. When you use `cache=no` url parameter, PlantUML server will set `Cache-Control: no-cache` header, so browser will not put the image to cache. When you press F5 in browser, new request for the image will be made to PlantUML server. If you pass github url to master branch as `src` (not specific comment), then PlantUML will request latest version of file and will return updated image. – hal Apr 01 '19 at 09:05
  • 1
    is there a way to make this for for private repos too? – Martin Dürrmeier Apr 13 '19 at 23:00
  • 3
    "Alternative with Proxy Service" works great. The way to get it to work with private repos is to include the `?token=` as part of the raw url. Unfortunately, that token changes with every edit of the plantuml source, so you have to work a bit to keep the markdown doc up-to-date. But it is doable. – davidbak Aug 08 '19 at 22:05
  • @hal OK - I updated the answer for the `cache=no` feature. It's important to note that it does take some time for the image to update when the plantuml source is changed. I found some hints at https://github.com/atom/markdown-preview/issues/207#issuecomment-261716706 – Fuhrmanator Dec 03 '19 at 21:54
  • 1
    @MartinDürrmeier If you want to avoid manual handling of the token, consider checking my answer https://stackoverflow.com/a/75919567/4425335, having faced the same issues I created a simple fast CLI tool to handle the previews that you can integrate into your local/CI workflow, I hope it works for you! – Jaroslav Šmolík Apr 03 '23 at 16:29
16

This is an old question, but it comes up early in the searches for solutions to this problem.

UPDATE

GitLab now support both Mermaid and PlantUML diagrams on their public offering. Mermaid is supported out of the box.

```mermaid
sequenceDiagram
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
Alice -> Bob:Another authentication Response
Bob --> Alice: Another authentication Response
```

Example

Orig Answer

The other answers discuss GitHub, but it is worth noting that GitLab has native integration for this, if you are deploying in-house. For some reason, they have never activated the feature on their public offering. There are tickets open to activate the feature.

If you are working of an in-house hosted solution, there are instructions available in the administration documentation

Basically you

  1. stand up your own plantuml server
  2. check the Enable PlantUml in GitLab server configuration
  3. write PlantUml in your markdown
```plantuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
   
Alice -> Bob: Another authentication Request
Alice <-- Bob: Another authentication Response
```
observer
  • 2,925
  • 1
  • 19
  • 38
Jefferey Cave
  • 2,507
  • 1
  • 27
  • 46
  • 4
    gitlab.com supports plantuml now, and test fine. Please to ref [gitlab-plantuml](https://gitlab.com/gitlab-com/gl-infra/readiness/blob/master/plantuml/overview.md) – Jesse Apr 12 '20 at 00:44
  • @Jesse I almost consider that irrelevant given Mermaid support, but plantUML still covers more use cases. Either way, good to hear. – Jefferey Cave Apr 13 '20 at 10:34
10

I found it is possible to do this using plantuml-gist on http://uml.mvnsearch.org/

Just put a valid puml file in the repository and include a link to this site in the documentation.

For example, for the file https://github.com/linux-china/plantuml-gist/blob/master/src/main/uml/plantuml_gist.puml in GitHub, you would use http://uml.mvnsearch.org/github/linux-china/plantuml-gist/blob/master/src/main/uml/plantuml_gist.puml as your link.

Here's what it looks like linked in StackOverflow:

UML image http://uml.mvnsearch.org/github/linux-china/plantuml-gist/blob/master/src/main/uml/plantuml_gist.puml

Unfortunately, it doesn't seem to work with GitLab. I always get a "No PUML file found", even though I have checked the path several times.

EDIT: server at uml.mvnsearch.org seems down.

joanq
  • 1,416
  • 2
  • 10
  • 12
  • This would be even cooler if you showed an example in gollum wiki markup, but it's good enough to figure it out. – Fuhrmanator Sep 18 '15 at 18:18
  • It appears the server that uml.mvnsearch.org is using doesn't grok the latest PlantUML language. I posted an answer that uses PlantUML.com's cloud server. – Fuhrmanator Sep 25 '15 at 14:27
  • @joanq emedded image link possibly broken – mjs Sep 29 '16 at 15:09
4

For Gitlab
Step 1: To enable the PlantUML and insert the PlantUML server location enter image description here Step 2: To create a PlantUML example and then you will see the result on the preview

@startuml
Bob -> Alice : hello
@enduml

enter image description here

For GitHub
Step 1: To create a PlantUML file enter image description here

@startuml
Bob -> Alice : hello
@enduml

Step 2: Get a URL from raw in my example: "https://raw.githubusercontent.com/williehao/nginx-certbot/main/nginx-certbot.iuml" enter image description here enter image description here Step 3: Integrate PlantUML render engine with GitHub markdown enter image description here

![your-UML-diagram-name](http://www.plantuml.com/plantuml/proxy?cache=no&src=https://raw.githubusercontent.com/williehao/nginx-certbot/main/nginx-certbot.iuml)

PS: replace your own raw URL from "https://raw.githubusercontent.com/williehao/nginx-certbot/main/nginx-certbot.iuml" to ****

enter image description here

reference

Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
3

To publish the UML diagrams, I would like to have some easy way to keep them updated into the repository and visible as images in the wiki

This should be possible on GitHub now (Aug. 2022)

Wikis now support math and Mermaid diagrams

Earlier this year (Feb. 2022), GitHub added support for LaTeX style mathematical expressions and Mermaid diagrams in Markdown.

However, until now, GitHub wikis were missing this support.
You can now use these formatting features in GitHub wikis.

For more information about using mathematical expressions and Mermaid with GitHub, see creating diagrams and writing mathematical expressions in the GitHub documentation.

Those diagrams includes ULM ones.

UML

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

Since GitLab supports rendering PlantUML out of the box, you can use the following:

README.md

This is some example text in a Markdown file. Followed by a PlantUML diagram, loaded from an URL.

```plantuml
!include https://gitlab.com/my-drawing.puml
```

When you 'preview' the file, it will render the text together with an image of the PlantUML diagram.

For more information about PlantUMLs !include directive, see https://plantuml.com/preprocessing#393335a6fd28a804

ndequeker
  • 7,932
  • 7
  • 61
  • 93
1

GitLab

Already support PlantUML natively as mentioned in other posts.

GitHub

I wish they support it natively too, but they don't at the moment.

Workaround

I built my own workaround similar to the PlantUML proxy mentioned above, but a little bit better.

TLDR:

Long:

  • It supports arbitrary urls
  • It supports both public and private repos on https://github.com
  • It supports GitHub Enterprise instances
  • It does not expose API tokens as part of the URL like the PlantUML proxy solution
  • It can be used outside of GitHub, e.g. in emails, wiki, presentation etc.
  • Private repos and GHE instances needs your own puml app with API token configured.
  • Examples can be found in https://github.com/lyang/puml
Lin Yang
  • 41
  • 2
1

Good news regarding GitHub!

Just found out, that for Github - Mermamid graphs are nativly supported now within GitHub Issues, GitHub Discussions, pull requests, wikis, and Markdown files.

Just put your graph definition to your Markdown file.

Here is a simple flow chart:

```mermaid
graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;
```
Dirksan
  • 11
  • 1
  • But I did mention that support [6 month ago in this same page](https://stackoverflow.com/a/73300843/6309). – VonC Feb 19 '23 at 19:20
  • @C You're right - did not see / recognize your post between all those poml-for-markdown posts. Sorry for that. – Dirksan Feb 19 '23 at 20:01
0

Sharing my open source project that handles all this for you https://github.com/danielyaa5/puml-for-markdown

enter image description here

How It Works

Using PlantUML Web Service to Render PUML Diagrams

You can use PlantUML using the online web service to generate images on-the-fly. A online demonstration is available at http://www.plantuml.com/plantuml. You can pass the encoded text of your diagrams to the web service in the url path and it will generate an SVG or PNG for you. Here's a simple HelloWorld example http://www.plantuml.com/plantuml/uml/Aov9B2hXil98pSd9LoZFByf9iUOgBial0000. Large diagrams will have very long encoding strings, they can exceed maximum url length. They also don't look very good in markdown files. By default the CLI will use the tinyurl.com service to shorten the link to the diagram.

Encoding PUML Diagrams for the Web Service

The CLI will use the plantuml-encoder package to encode puml files. To support hyperlinking diagrams we need to parse all hyperlinks in the puml files. A dependency graph is created for the files and a DFS is performed where we create links for the leaf nodes first, then replace the links in the parent nodes with the links to the leaf nodes. The puml files are not actually modified, only the puml files content in memory is modified. By default the tinyurl free service is used to shorten the links.

In order to support !include we parse puml files and replace any !include with the contents of the file referenced.

Parsing Markdown

The markdown files are then parsed for markdown comments. If the comments reference a PlantUML file, a link to the web service url will be added next to the comment. Because these links contain the full PlantUML diagram encoding in it, there are no issues using them in private repositories.

Daniel Kobe
  • 9,376
  • 15
  • 62
  • 109
0

Sharing my OSS project you are welcome to try: plantaznik, it is a Rust CLI tool for inlining PlantUML links. You can install it via cargo (cargo install plantaznik) or download prebuild libraries in releases.

Just place the following macros to your README:

<!-- plantaznik:./path/to/plantuml/source.plantuml -->
![](???)

And run:

$ plantaznik README.md

You can easily use it for multiple files, in your git hooks or CI. See readme for more examples.