Skip to main content
  1. Posts/

Rendering diagrams in Hugo

··520 words·3 mins
Author
Hairizuan Noorazman
Software engineering experiments, implementation notes, and lessons learned.

There is an interesting Javascript project that allows one to use just plain old text and convert those said text into diagrams.

graph TD Start --> Stop

Text to that converts to the above diagram:

graph TD
    Start --> Stop

Mermaid Documentation Page
https://mermaidjs.github.io/

Mermaid js source code
https://github.com/knsv/mermaid

Let’s say we would want to use this diagram conversion tool in conjuction with hugo; how should we do it? Do we somehow need to embed html partial snippets all over the place etc?

Luckily, Hugo has a mechanism to do this via shortcodes. Links to the website is below

Hugo Shortcodes: https://gohugo.io/content-management/shortcodes/

Essentially, the shortcut somehow injects html into the rendered html page from the markdown file. Since its plain old html, we can inject all kinds of html templates, html templates with javascript. That’s essentially how one would be able to embed the youtube video player into blog posts.

{{ youtube XXXXXX }}

Pretend the XXXXXX is some sort of youtube id (It is the v parameter of the youtube url.)

Hugo supports several shortcodes, including instagram and twitter.

However, let’s go back to using Mermaid with Hugo. Hugo does not support Mermaid.js out of the box, one cannot just use shortcode to inject snippets of mermaid js html all over the blog. However, there is a mechanism to build it (also available in the Hugo website). However, if one wants some reference on how to add such functionality, they can look at the following example.

Mermaidjs + Hugo example
https://github.com/matcornic/hugo-theme-learn

This website relies on Hugo and Mermaidjs which would generate the pages with diagrams svgs on them as ncessary.

There are a few things we need to take note while building the shortcode to support it:

  • Do not have the mermaid.js be imported to the site via a CDN. When running locally, the browser will block all external scripts from running locally.
  • There are some breaking changes for mermaid.js - take note of them
  • Hugo website kind of mentioned that one can put js and css files into the static folder. If one comes from the frontend development work, it becomes easy to assume that the js and css files would be compiled and imported as part of local. This is definitely not true in this case; there is a need to actually have a html snippet that actually does the importing of the required files to the frontend.
  • Steps Involved:
    • Add css and js files required for mermaid.js library in the static folder.
    • Have the mermaid.html shortcode be put into the shortcode folder of layouts
    • Have a partial HTML snippet in partials to be able to import the html into the final rendered HTML output. The partial HTML snippet should be importing the css and js locally. There is a need to activate for mermaid.js scripts for the script to know when to activate and convert the text to diagram.

The result would appear as below which would allow you to render the graph above.

{&lbrace;<mermaid>&rbrace;}
graph TD
    Start --> Stop
{&lbrace;</mermaid>&rbrace;}

Just a little fun experiment to see how far this Hugo framework can take me.

Related

CORS with Golang Microservices and Elm Frontend is difficult

··1597 words·8 mins
I am still building up my personal pet project: https://github.com/hairizuanbinnoorazman/slides-to-video; the aim of this project is a personal one - to build up a set of microservices that is able to be deployed in various ways such as locally via Docker Compose or even to Kubernetes or the serverless Cloud Run platform on Google Cloud Platform. There was a previous blog post describing an initial part of this journey: Lessons on building the project - Part 1

Build Chat App with Golang Websocket and Elm Frontend

··1446 words·7 mins
While building Elm based frontends, I decided to take the opportunity to learn on how to craft a chat application. Truthfully, I’ve never really built one before (nor do I need to). But it does seem like an interesting programming exercise to kind of go thru - in order to understand how such applications are built, deployed, scaled and managed. For the frontend, I’m mostly set to use Elm (probably you’ve seen a previous post on my “dislike” for other Javascript based frameworks, which is essentially all the popular ones in the market). For backend, I will probably stick to Golang since that is the language I’m most comfortable with (all hail statically typed languages)

BMI Calculator

··347 words·2 mins
BMI or Body Mass Index is calculated by taking one’s weight (in kilogram) and divided by the square of the height of the person (in metres). You can utilize the following tool below to quickly calculate this.

Elm Frontend in Hugo Static Site

··1840 words·9 mins
To view the Elm component in action - scroll down to the Elm Component Demonstration section Motivation # I wanted to learn to try to write a Frontend Application that provide some sort of dynamic functionality - e.g. doing quick mathematical calculation, fetching data from some backend APIs etc. However, the frontend world is a pretty complex world (and continues to be so to date) - there are many factors to take note when writing it:

comments powered by Disqus