Skip to Content

Convenient Joplin Exports on FreeBSD 13.1

A few weeks ago I wrote about how to install Joplin on FreeBSD via jails. This set up is working fairly well for me, and I’m really digging the terminal-based interface.

I’ve discovered a snag though. Exporting via the terminal application isn’t great. In particular, exporting PDF is not possible. My jails-based installation doesn’t help, since you’ll export into the jail’s filesystem, not your own one.

So this is a short blog post detailing my Joplin export workflow.

Exporting as root

It’s pretty easy to simply export all Joplin’s notes:

bastille console joplin
joplin export /export --format md
exit

If we check out what’s in the export directory, we see that it contains a set of directories, each containing the contents of a notebook.

ls -l /usr/local/bastille/jails/joplin/root/export
total 201
drwxr-xr-x  3 root  wheel  423 Aug  9 13:34 Old Notes
drwxr-xr-x  3 root  wheel   14 Aug  9 13:34 Personal
drwxr-xr-x  3 root  wheel    5 Aug  9 13:34 Templates
drwxr-xr-x  3 root  wheel   28 Aug  9 13:34 To Do
drwxr-xr-x  2 root  wheel   10 Aug  9 13:34 _resources
drwxr-xr-x  3 root  wheel    5 Aug  9 13:34 recipes

The _resources directory contains the static content (images and what have you) that are embedded in the markdown notes. Let’s clean out the directory and then try run the export from outside the jail (as root).

rm -rf /usr/local/bastille/jails/joplin/root/export/*

# check it's empty
ls -l /usr/local/bastille/jails/joplin/root/export/

bastille cmd joplin joplin export /export --format md

# now it's not empty
ls -l /usr/local/bastille/jails/joplin/root/export/

One thing you’ll notice is that the files are owned by root. This is a problem since we actually want our regular non-root user to be able to interact with them.

Making exports usable by a regular user

I’m sure there is a more elegant way to do this, but I just took the fastest path to success.

Firstly, we mount a local user directory to the right path inside the jail. You might need to make sure you’ve cleaned out / deleted the folder in the jail and reboot for fstab to pick it up.

Note 1: Replace [USER] with your username in the code that follows

Note 2: this mount command results in a persistent mount.

bastille mount joplin /usr/home/[USER]/notes export nullfs rw 0 0

Once you’ve rebooted, run the export again and verify it works.

/usr/local/bin/bastille cmd joplin joplin export /export --format md

The exported notes should all appear in your local notes folder. Note that the notes are all readonly. This works for me because I don’t have a need to edit notes outside of Joplin.

I do, however, want to be able to refresh the exported notes directory. In order to accomplish this, we need to employ a bit of sudo / scripting / aliasing hackery.

First, we create a little bash script and put it in [USER]/bin:

#!/bin/bash

rm -rf /usr/home/[USER]/notes/* 
bastille cmd joplin joplin export /export --format md

We remove the original notes because Joplin doesn’t overwrite, it adds new files witha ‘filename-1.md` type naming scheme.

Next, we add the ability to execute this to /usr/local/etc/sudoers.d/[USER]:

[USER] ALL=(root) /usr/local/bin/bash /usr/home/[USER]/bin/get-notes

Finally we add an alias to the user’s .bashrc:

alias get-notes="sudo bash /usr/home/[USER]/bin/get-notes"
alias gn="get-notes"

Now, if we open a new terminal, we should be able to execute the command gn to rebuild our notes directory.

Bonus: Read a note in pretty html

Ok, we have these markdown notes. We might want to read them in a pretty format, or even share them.

We can use pandoc to achieve this.

pkg install hs-pandoc

Then it’s a simple question of creating a css template and little bash script.

You can download my css template here

Here’s my bash script, called view-note:

#!/bin/bash

for fname in "$@"; do
  /usr/local/bin/pandoc -s -f gfm -t html "$fname" -c /usr/home/[USER]/bin/github-pandoc.css > /tmp/tempfile.html
firefox /tmp/tempfile.html &
done

Note the funny “@” syntax. This is to cope with whitespaces in filenames.

I put both the css file and my bash script in /home/[USER]/bin and then I create this alias in my .bashrc

alias view-notes="bash /usr/home/[USER]/bin/view-notes"
alias vn="view-notes"

Not I can execute vn [filename] to view that nore in pretty HTML in my browser. For instance, here is what opens up in Firefox when I type in vn notes/recipes/red_lentil_soup.md.