Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Markdown with PlantUML

How to use PlantUML with Markdown

PlantUML is a really awesome way to create diagrams by writing code instead of drawing and dragging visual elements. Markdown is a really nice documentation tool.

Here's how I combine the two, to create docs with embedded diagrams.

Step 0: Setup

Get the command-line PlantUML from the download page or your relevant package manager.

Step 1: Create a Markdown file

Use your favorite markdown or text editor. Most (if not all) developer-oriented text editors have some kind of markdown support.

Remember that md files can contain html, and that html is passed-through to the generated html as-is.

When you want to embed a diagram, create a hidden div: <div hidden>.

Now start a code block by indenting or typing 3 backticks. The first line of the code block must be @startuml followed by a file name (with no extension). The following lines should be the actual diagram code, ending with @enduml. End the code block and close the div.

Finally, to actually show the diagram in the document, add an image in markdown: ![](firstDiagram.svg). The name must be the same name as in the @startuml command, with a .svg extension.

All together now

Regular **Markdown** here.

<div hidden>
```
@startuml firstDiagram

Alice -> Bob: Hello
Bob -> Alice: Hi!
		
@enduml
```
</div>

![](firstDiagram.svg)

Some more markdown.

Step 2: Run PlantUML on the Markdown file

On the command line:

plantuml -tsvg FILENAME

Where FILENAME is the name of the markdown file.

For every PlantUML block in the file, one svg diagram is generated. When the markdown to html converter is running, the html will contain image links to the generated images.

Step 3: Convert locally to HTML or upload to GitHub

If you host the files in GitHub (or other services that convert md to html on the fly), the last step is uploading or pushing the files. Make sure to include everything: the markdown and the generated diagrams.

Otherwise, use your favorite tool for converting markdown to html - a markdown editor or a command line tool.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Regular Markdown here.

@startuml firstDiagram

Alice -> Bob: Hello
Bob -> Alice: Hi!

@enduml

Some more markdown.

@sjoulbak

This comment has been minimized.

Copy link

@sjoulbak sjoulbak commented May 1, 2020

Hello noamtamim,

Thank you for writing this! 😄

If you don't want to show the "PlantUML" syntax on github, you can replace <div hidden></div> with inline commenting <!-- -->. PlantUML will still generate the UML.

As for your example:

Regular **Markdown** here.

<!--
@startuml firstDiagram

Alice -> Bob: Hello
Bob -> Alice: Hi!
	
@enduml
-->

![](firstDiagram.svg)

Some more markdown.
@noamtamim

This comment has been minimized.

Copy link
Owner Author

@noamtamim noamtamim commented May 1, 2020

Thanks @sjoulbak, that's indeed more compact.

@fuhrmanator

This comment has been minimized.

Copy link

@fuhrmanator fuhrmanator commented Jul 2, 2020

You can put the PlantUML in a separate file, MyModel.puml and link to it in a Readme using this method. It renders the UML using the web service, rather than having to install PlantUML on a machine and doing an extra step.

Note that this doesn't work well in private repos, because the link to the raw .puml file has a token that tends to change when the repo evolves.

@woahdae

This comment has been minimized.

Copy link

@woahdae woahdae commented Jul 2, 2020

One issue with the HTML comment strategy is you can't use dotted lines, -->.

I tried:

<!--
```
@startuml
Foo --> Bar
@enduml
```
-->

As well as:

<!--
    @startuml
    Foo --> Bar
    @enduml
-->

The former seemed to break the markdown engine (the Marked 2 app) by never closing the first comment at all, the latter still rendered the dotted line as a closing comment. Oh well.

I ended up separating the markdown and the plantuml with a README.md and a README.plantuml, but still using the filename trick shown here. Thanks!

@fuhrmanator

This comment has been minimized.

Copy link

@fuhrmanator fuhrmanator commented Jul 2, 2020

One issue with the HTML comment strategy is you can't use dotted lines, -->.

I logged a bug/feature request for PlantUML at https://forum.plantuml.net/11806/dashed-arrow-also-the-close-comment-blocks-html-xml-markdown -- the maintainer often does quick fixes if it's simple and a popular use case, so we'll see.

@sjoulbak

This comment has been minimized.

Copy link

@sjoulbak sjoulbak commented Jul 13, 2020

@woahdae I solved this by changing the arrow to the other direction:

<!--
@startuml firstDiagram
Bar <-- Foo	
@enduml
-->
@noamtamim

This comment has been minimized.

Copy link
Owner Author

@noamtamim noamtamim commented Jul 13, 2020

@sjoulbak both the workaround suggested in the forum and the actual solution work just fine. Changing arrow direction works, but changes semantics/readability too.
The fix has another advantage: it also works in component diagrams:

http://www.plantuml.com/plantuml/uml/SoWkIImgAStDuOfsZ5NGjLE8Tehb0c85NTru8CSvbiiXDIy5A0y0

@truegate

This comment has been minimized.

Copy link

@truegate truegate commented Oct 20, 2020

awesome!!! Thanks

@wicksome

This comment has been minimized.

Copy link

@wicksome wicksome commented Feb 10, 2021

In my case, I use the plantuml-extension. This chrome extension renders the plantuml code into image.

@verhas

This comment has been minimized.

Copy link

@verhas verhas commented Feb 10, 2021

I am using https://github.com/verhas/jamal Jamal as a preprocessor for Markdown and also to Asciidoc files.
Release 1.6.5 is due in a few days and will contain a PlantUml module.

Using this workflow is somewhat similar to running plantuml extracting the images from the markdown file. It can also use other macro features, include sample codes from the code, fetch parameters and insert into the documentation from project XML files (typically the current version of the documented project). This approach does not have the

<!--
....
    A --> B
-->

As the output file, which is a native Markdown, Asciidoc, whatever format you use, does not contain the UML text anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment