Skip to content

Instantly share code, notes, and snippets.

@enpassant
Last active May 14, 2024 20:56
Show Gist options
  • Save enpassant/0496e3db19e32e110edca03647c36541 to your computer and use it in GitHub Desktop.
Save enpassant/0496e3db19e32e110edca03647c36541 to your computer and use it in GitHub Desktop.
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"
@enpassant
Copy link
Author

enpassant commented Dec 17, 2016

kepernyokep 2016-12-17 19-08-44

@tinmarino
Copy link

tinmarino commented Jun 30, 2018

Hi, super script : Thank. Could you replace

sed -r 's/(\[.+\])\(([^)]+)\)/\1(\2.html)/g'

by

sed -r 's/(\[.+\])\(([^#)]+)\)/\1(\2.html)/g'

Where the link [^)]* has been replaced by [^#)]* so that you can have inner link like :

[Foo](#foo)

# Foo

Like : https://stackoverflow.com/questions/2822089/how-to-link-to-part-of-the-same-document-in-markdown

@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