Obsidian2Hugo exporter in go

Posted on Jan 10, 2022

Today I learned how to parse and transform #markdown created written inside #obsidian for the usage as a source for my #hugo blog and finished my obsidian2hugo exporter.

Instruction

As part of my hugo blog setup using obsidian as my source I developed this small tool. The reason for that simply was: I’m lazy. My goal was to be able to use markdown as close to the standard obsidian format as possible without too much manual work. One thing I didn’t like was to have to put the title into frontmatter and to manually write the description used by hugo as a <meta> description which is important for search engine optimization (SEO). The here introduced obsidian2hugo does exactly this. I t parses a obsidian source (sub)tree, parses every markdown file and extracts (and deletes) a h1 title (if present) and uses the content of a named h2 header (standard ## tl;dr)

golang

To extract and re-write the information I used goldmark for the markdown part and the pageparser created by the folks behind hugo .

The tool

obsidian2hugo can be found here .

obsidian2hugo --help
This tool was created to be able to export blog posts created inside obsidian for the usage inside a hugo blog.

Usage:
  obsidian2hugo [flags]

Flags:
  -t, --descriptionSection string   The content below this h2 header is used as the frontmatter description (default "tl;dr")
  -d, --destination string          Destination of hugo posts folder (e.g. <hugoroot>/content/posts)
  -h, --help                        help for obsidian2hugo
  -k, --keepTitle                   Don't delete h1 header after frontmatter extraction
  -s, --source string               Source to obsidian markdown files (root of blog posts tree, e.g.: <obsidianvault>/blogposts) (default ".")

Installation

go install github.com/HashtagMarkus/obsidian2hugo

Usage example

Obsidian source tree

In this blog, my obsidian folder structure looks like this:

obsidian tree

The base path is <obsidianVault>/blop/til/ (1) and every post is located under a subfolder I named containing the date it should be published and a short name (2). Inside this folder I create one markdown (3) file as well as all images used inside the blogpost (4).

Blog template

My obsidian blogpost template creates the following markdown: Obsidian post template The frontmatter section (1) contains the published tag, which is evaluated by hugo to detemine whether or not to publish the post. The date is the also evaluated, so the post only gets published after this date. Tags are added by myself and used by hugo to group posts accordingly. The title is replaced with the content of the h1 header (2). Per default this markdown header gets removed from the markdown during the export process. The paragraph below the tl;dr h2 header (3) is detected and later used as the frontmatter description.

Export

Once I’m ready to publish, I simply run obsidian -s <obsidianVault>/blog/til -t <hugoroot>/content/posts

This command copies the complete blog post folders of files marked as published: true into the hugo blog posts tree and extracts the above described information. As a result, the exported markdown file inside the hugo directory looks like this:

Markdown after export

As you can see in the above screenshot, the description and title frontmatter tags are changed and the h1 markdown title was removed from the exported markdown content.

The generated content now can published using hugo.

More about hugo