If you're like me, then you use gitlab-flavored markdown quite a bit. The combination of mermaid and plantuml graphs and katex math really makes for a productive experience. However, due to issues like gitlab-org/gitlab#215084 and gitlab-org/gitlab#243533, you probably also want to have some other way to render your notes, rather than relying on gitlab to do it for you.
This guide will show you how to do just that with a tool called pandoc, as long as you're OK with not having all the features of gitlab markdown. At the moment, this setup supports the following features:
- Mermaid Graphs
- PlantUML Graphs
- Katex Math
- Relative and Absolute References to Project Files
- References to Directories with READMEs
This setup does run in CI/CD, which is how I primarily use it, but this guide will focus on how to set this up on your own machine. If you're interested in a CI/CD project template though, I have one on my gitlab. I will tend to update that faster than this document, and I track known issues with it there too.
To produce this setup, I installed:
Dependency | Rationale |
---|---|
mermaid-cli |
Required by timofurrer's pandoc-mermaid-filter |
plantuml |
Required by timofurrer's pandoc-plantuml-filter |
texlive-full [1] |
Required by the pandoc LaTeX template |
My fork of @timofurrer's pandoc-plantuml-filter |
Allows pandoc to render plantuml code blocks as graphs |
My fork of @timofurrer's pandoc-mermaid-filter |
Allow pandoc to make sense of mermaid code blocks as graphs |
This GitHub pandoc html5 template | Used to make the html all pretty :D |
This pandoc LaTeX Template | Used to make the pdf all pretty :D |
@lierdakil's gitlab-math.lua script |
Supports gitlab's special math syntax. |
My fix-links.lua script |
Fixes links and urls to resources within the project. |
... and of course, pandoc itself!
I'm on Manjaro Linux, so I ended up using the following commands to install these:
# install plantuml & mermaid
yarn global add @mermaid-js/mermaid-cli
pamac install plantuml
# Install pandoc plantuml & mermaid filter
pip install git+https://github.com/MyriaCore/pandoc-plantuml-filter.git
pip install git+https://github.com/MyriaCore/pandoc-mermaid-filter.git
# Install lua filters
mkdir -p ~/.pandoc/filters
wget https://gist.githubusercontent.com/lierdakil/00d8143465a488e0b854a3b4bf355cf6/raw/gitlab-math.lua
mv gitlab-math.lua ~/.pandoc/filters/gitlab-math.lua
wget https://gist.githubusercontent.com/MyriaCore/75729707404cba1c0de89cc03b7a6adf/raw/fix-links.lua
mv fix-links.lua ~/.pandoc/filters/fix-links.lua
Just a quick note, you will need to install yarn
to install mermaid-cli
, unfortunately. You could try npm, but that didn't work as well for me.
1: On manjaro, I had to get it from the aur, but if you're on an ubuntu-based distro, you should be able to get it more easily. ↩
All you'll need to do to setup the environment is expose the mermaid binary (assuming you installed via yarn):
# Exposes mmdc (mermaid binary) for us
export PATH=$(yarn global bin):$PATH
I wrote a small script to generate a pdf or html file (at the user's request). If you don't care about the commands and just want to get up and running, I'd recommend putting it somewhere on your $PATH
:
wget https://gist.githubusercontent.com/MyriaCore/75729707404cba1c0de89cc03b7a6adf/raw/570ec66358b95f634a28be69ba5de59854e41b24/gfm.sh
mv gfm.sh /usr/local/bin
# create readme.html
gfm.sh readme.md
gfm.sh -html readme.md
# create readme.pdf
gfm.sh -pdf readme.md
This script basically just provides a nice wrapper for the commands below. If you're interested in understanding those, keep reading. Otherwise, gfm.sh should complete your setup.
Using the Github html5 template:
MERMAID_THEME='neutral' # use gitlab's mermaid theme
pandoc --from markdown --to html5 \
--self-contained --standalone \
--filter pandoc-plantuml \
--filter pandoc-mermaid \
--lua-filter gitlab-math.lua \
--lua-filter fix-links.lua \
--katex --template=GitHub.html5 \
-o <name of output>.html <name of input>.md
The --self-contained
option allows links to images, pictures, and mermaid/plantuml graphs to be embedded with data:...
URIs instead of relative links. This allows the HTML file to be truly portable, if not a bit large.
NOTE: Using --self-contained
did work locally on my laptop, but didn't work with my CI/CD setup. I had to remove this flag in the makefile as a result. See this discussion for more info.
The best template I could find out there is Eisvogel, which is a latex template that will help us create a pdf.
MERMAID_THEME='neutral' # use gitlab's mermaid theme
pandoc --from markdown --to latex \
--self-contained --standalone \
--filter pandoc-plantuml \
--filter pandoc-mermaid \
--lua-filter gitlab-math.lua \
--lua-filter fix-links.lua \
--katex --template=eisvogel \
-o <name of output>.pdf <name of input>.md
In my experience, this template is super buggy in combination with pandoc-mermaid-filter
and pandoc-plantuml-filter
. The images pretty regularly spill over the margins of the document, even when using svg images. I'll look into this further to see if I can tweak it, or find a better template.
NOTE: Using --self-contained
did work locally on my laptop, but didn't work with my CI/CD setup. I had to remove this flag in the makefile as a result. See this discussion for more info.