Wiki by Hugo and Vimwiki

Last Update:

Status: ON-GOING

If you meant to check my digital garden, click here: Wiki.

This page talks about the current implementation of said digital garden.

On this page (click to [un]hide)

Personal Knowledge Management (PKM) System

Having a second brain is getting more popular. In it’s simplest form, it’s a bunch of personal notes on different topics compiled over a period of time. When these notes are linked to one another so that they might form new ideas, then their sum becomes a knowledge base—a low-tech brain if you will—that one can use to help with Thinking.

go to top ⤒

Assumptions

This write-up assumes the following that the reader:

go to top ⤒

Markdown experience for your wiki

The out-of-the-box .wiki syntax of Vimwiki is a derivative of Markdown, but not the variant that’s widely adopted. So, for a cohesive experience, I included the following snippet init.vim it so I can use the Markdown syntax, that is, text files with .md extension:

let g:vimwiki_list = [{'path': '~/path/to/vimwiki/',
                      \ 'syntax': 'markdown', 'ext': '.md'}]

With this I can now write notes using .md and NOT .wiki, which is important because Hugo only works with the former.

go to top ⤒

Integration with Hugo

Create a wiki section in Hugo directory

I made a separate section in Hugo’s site’s folder structure, under content, called wiki, into which I’ll copy my PKM files. (For more info on this, Hugo’s Quick Start is a helpful guide.)

mkdir content/wiki/
go to top ⤒

Laying out the wiki, Hugo way

Ensure that there is a wiki folder under layouts/:

mkdir layouts/wiki/

I populate this folder with single.html and list.html files; the former is a template for how each page in the wiki would look like, and the latter is for the list of all the notes.

The following are the stripped down version for single.html:

{{ define "main" }}

<h1>{{ .Title }}</h1>

{{ .Content }}

{{ end }}

And now, the list.html:

{{ define "main" }}

<h1>{{ .Title }}</h1>

{{ range .Pages.ByLastmod }}
<a href="{{ .RelPermalink }}">{{ .Title }}</a><br>
{{ end }}

{{ end }}
go to top ⤒

Index files

I created an _index.md file placed under the content/wiki/ section, and this just contains the title of that page, like so:

---
title: "Digital Garden 🌱"
---

Meanwhile, recall VimWiki also has its own index.md (originally index.wiki out of the box). When copying the offline PKM files into content/wiki/ section, we don’t need to copy this particular index file to not cause any weird issues.

To be clear, I don’t have my PKM within this Hugo’s site directory. I am keeping it in a separate folder, to have compartmentalization. This friction is non-issue for now as will be apparent in the next few commands.

go to top ⤒

Copy PKM files to Hugo site

To migrate, I run a few commands in my terminal:

cp -rv /path/to/offline-wiki/*.md /home/user/site-directory/content/wiki/
rm /home/user/site-directory/content/wiki/index.md

PRO TIP: To make this easier, I have included these lines in a Makefile that builds and publishes my website. When I run make build in my terminal, my blog update now includes the latest changes from my wiki. (Note: making a Makefile is outside the scope of this post.)

go to top ⤒

Preparing each note to be Hugo-ready

For this to work, each note must be Hugo-ready. Writing the notes in Markdown is already half the work. Next is to ensure each note has a front matter. For more info on this, check Hugo’s page on Front Matter.

go to top ⤒

Automatically add front matter

To automatically add front matter each time you create a note, create a Python script VimWiki can call. Let’s call this gen-wiki-template and place it on Vim config directory, say ~/.vim/bin/gen-wiki-template. The script itself contains the following lines:

#!/usr/bin/python
import sys
import datetime

template = """---
title: ""
date: {date}
draft: false
math: false
---

{{{{< toc >}}}}

"""

date = datetime.date.today()
        # if len(sys.argv) < 2
        # Expecting filename in YYYY-MM-DD.foo format
        # else sys.argv[1].rsplit(".", 1)[0])
print(template.format(date=date))

Let’s break it down:

Feel free to edit to meet your front matter needs.

go to top ⤒

Config the init.vim to call the script

To automatically call the script, that is, to add the front matter, add this line below to your vim init.vim.

au BufNewFile ~/path/to/wiki/*.md :silent 0r !~/.vim/bin/gen-wiki-template '%'

This means that every time a new file (note) is created within the wiki folder, gen-wiki-template script is run. The script just echoes the front matter on the blank file.

Having front matter in the wiki is helpful because I can set draft: true and not worry about making a note publicly available yet still accessible to me (via offline PKM).

go to top ⤒

Generate!

At this point, when you generate your site using hugo command, your wiki should now be included and is ready to be published online.

BONUS: Vimwiki on-the-go with Markor and Syncthing

Edit notes on your Android smartphone by using:

Further reading

In case you, dear reader, still want to have your own digital garden but cannot be bothered to take on the (relatively) steep learning curve of both Hugo and VimWiki, consider looking into the resource/s below:


  1. Interestingly, to document my shortcodes without activating them, I also had to escape the curly braces here! For more info, see Chris Liatas’s post on “Escaping Hugo Shortcodes” ↩︎



Useful? Consider supporting me.

Back to Index :: Log :: Projects


"Wiki by Hugo and Vimwiki" project is licensed under CC BY-NC 4.0