box.matto.nl
Enjoying Open Source Software

Transform Mardown to Vimwiki format with sed

Vimwiki backlist function not working in Markdown

I started with a first try-out to create a Zettelkasten with Vimwiki. For this I use a Vimwiki in Markdown format.

Unfortunately, the :VWB function, which creates a list of backlinks, works only in the original Vimwiki format.

Transformation to Vimwiki format

This is my first try-out for a Zettelkasten, so I don't have many files in it. Therefor, the easiest solution seems to be to a conversion of the current Vimwiki Zettelkasten to the original Vimwiki format.

This turned out to be much simpler than it sounds :)

I do use only some simple formatting:

# Top level header
## Second level header
### Second level header
[description](filename)
[description](url)

These formats has to be transformed to the original Vimwiki

format:

= Top level header =
== Second level header =
=== Third level header ===
[[filename|description]]
[[url|description]]

sed script to do the transformation

As this is not very complicated, I tried to do this with sed.

This is what does the trick:

s/\[\(.*\)\](\(.*\))/[[\2|\1]]/
s/\(^###\) \(.*\)/=== \2 ===/
s/\(^##\) \(.*\)/== \2 ==/
s/\(^#\) \(.*\)/= \2 =/

People with more sed-fui than I have will probably do it more efficient, but it works :)

Put this lines in a file, I called it conversion.sed and run this with some shell script to convert all the files with it to the new format, in a different directory. Also, the files have to be renamed from filename.md to filename.wiki.

I used this as a shell script:

#!/bin/sh

for file in $(ls 0??.md)
do
nieuw="../zettelkasten2/${file%\.md}.wiki"
echo $file $nieuw
cat $file | sed -f conversion.sed > $nieuw
done

My Vimwiki Zettelkasten try-out uses base-36 encoded filenames, like 00a.md, 01n.md, and so on. So far I have about 75 files in it. That is why in the script above I use ls 0??.md.

My Vimwiki runs in a FreeBSD jail, so this used the FreeBSD versions of sed and of the shell.

Have fun!

Tags: vimwiki markdown sed