Skip to content

Instantly share code, notes, and snippets.

@enpassant
Last active July 8, 2025 18:57
Convert VimWiki to HTML (markdown, mediawiki)

With this wiki2html.sh bash script and pandoc program, you can convert markdown to html.

Usage: In the vim list section of the .vimrcfile, include options:

let g:vimwiki_list = [{'path': ‘your_wiki_place',
  \ 'path_html': ‘wiki_html_location’,
  \ 'syntax': 'markdown',
  \ 'ext': '.md',
  \ 'custom_wiki2html': ‘link to the custom converter, i.e. wiki2html.sh'}]

If you want to use this converter for temporary wikis then add these to .vimrc:

  autocmd FileType vimwiki call SetMarkdownOptions()

  function! SetMarkdownOptions()
    call VimwikiSet('syntax', 'markdown')
    call VimwikiSet('custom_wiki2html', 'wiki2html.sh')
  endfunction

:VimwikiAll2HTML and <leader>wh work well.

#!/bin/bash
FORCE="$1"
SYNTAX="$2"
EXTENSION="$3"
OUTPUTDIR="$4"
INPUT="$5"
CSSFILE="$6"
FILE=$(basename "$INPUT")
FILENAME=$(basename "$INPUT" .$EXTENSION)
FILEPATH=${INPUT%$FILE}
OUTDIR=${OUTPUTDIR%$FILEPATH*}
OUTPUT="$OUTDIR"/$FILENAME
CSSFILENAME=$(basename "$6")
HAS_MATH=$(grep -o "\$\$.\+\$\$" "$INPUT")
if [ ! -z "$HAS_MATH" ]; then
MATH="--mathjax=https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
else
MATH=""
fi
# >&2 echo "MATH: $MATH"
sed -r 's/(\[.+\])\(([^)]+)\)/\1(\2.html)/g' <"$INPUT" | pandoc $MATH -s -f $SYNTAX -t html -c $CSSFILENAME | sed -r 's/<li>(.*)\[ \]/<li class="todo done0">\1/g; s/<li>(.*)\[X\]/<li class="todo done4">\1/g' >"$OUTPUT.html"
@maikelthedev
Copy link

Hey man, I took your code and added a few extras, like support for images (jpg for now) and templates.

https://gist.github.com/maikeldotuk/54a91c21ed9623705fdce7bab2989742

@xealits
Copy link

xealits commented Nov 16, 2020

Just a comment, sed does not have an option -r on Mac (it has NetBSD utilities?). The extended regexps are turned on by -E. Also it seems the Linux version of sed may work with all variants -E, -r, --regexp-extended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment