How to publish Obsidian notes with Hugo

I’m using Obsidian a lot and in certain moment I decided to use it as a backend for my blog. As I wasn-t able to find out an way how to do that, I created a simple shell script which goes through whole my Obsidian vault and pick the notes dedicated to publishing to my Hugo repository.

What it does

  • Go through whole Obsidian vault and based on publish keyword in the note’s frontmatter copies it into dedicated directory inside my Hugo project
  • Go through these notes and copy the related images / attachments to dedicated directory inside the Hugo project structure.

Limitations

  • So far it’s not possible to handle the linked notes (another MD documents). I didn’t implemented such feature as I’m using it just for blog generation.

Obsidian vault conversion

Script convert.sh is directly located in root of my Hugo project.

      

#!/bin/sh


IFS=$'\n'
blog="/Users/martin/blog"
dir="/Users/martin/blog/content/blog"
obs="/Users/martin/tresor"
static="/Users/martin/blog/static/obsidian/"
  
  
cd $dir ;rm $dir/*; grep -rl 'publish: true' $obs |xargs -I {} cp {} $dir
rm ${static}/*
for file in $(find ${dir} -type f -name "*.md"); do
	for priloha in $(perl -p -e 's/\%(\w\w)/chr hex $1/ge' ${file} | grep "\[.*\]\(.*\)" | perl -ne 'm/\[\[(.*)\]\]/ && print $1."\n"' | grep -v ".md$"); do
		find $obs -name $priloha |xargs -I {} cp {} $static
	done
	perl -p -i -e 's/\[\[(\w+)\]\]/\[image\]\(\/obsidian\/$1\)/g' ${file}
	sed -i -e "/\[\w+]\(\w+\)/ s/ /%20/g" ${file}
done
  
cd $blog
git add --all
git commit -m 'conversion from Obsidian vault'
git push

Site deployment

Site deployment is realised through simple git post-receive hook which directly invokes the deploy.sh script

#!/bin/sh

  
cd ~/sukany.cz/
git pull
doas -u root rm -Rf /var/www/htdocs/*
doas -u root chown -R martin /var/www/htdocs
hugo -d /var/www/htdocs