Skip to content

Instantly share code, notes, and snippets.

@hotoo
Last active April 15, 2023 01:15
Show Gist options
  • Save hotoo/9866720 to your computer and use it in GitHub Desktop.
Save hotoo/9866720 to your computer and use it in GitHub Desktop.
convert vimwiki to markdown: `sed -f ex -i *.md`
#!/usr/bin/env bash
for x
do
filename=$(echo $x|sed -e "s/\.wiki$/.md/")
sed -f ex $x > $filename
done
s/# \(.*\)$/* \1/g
s/^= \(.*\) =$/# \1/g
s/^== \(.*\) ==$/## \1/g
s/^=== \(.*\) ===$/### \1/g
s/^==== \(.*\) ====$/#### \1/g
s/^===== \(.*\) =====$/##### \1/g
s/^====== \(.*\) ======$/###### \1/g
s/{{{class="brush: *\([^"]*\)"/\`\`\`\1/g
s/{{{class="\([^"]*\)"/\`\`\`\1/g
s/{{{/\`\`\`/g
s/}}}/\`\`\`/g
s/\[\([^] ]\{1,\}\)\]\([^](]\)/![pic](\1)\2/g
s/\[\([^] ]\{1,\}\)\]$/![pic](\1)/g
s/\[\[\(\([^|]\{1,\}\)\|\)\([^]]\{1,\}\)\]\]/[\3](\2.md)/g
s/\[\[\([^]]\{1,\}\)\]\]/[\1](\1.md)/g
s/\[\(https\{0,1\}:\/\/[^ ]*\) \([^]]*\)\]/[\2](\1)/g
s/%% \(.*\)/<!-- \1 -->/g
/%toc.*/d
s/%title \(.*\)/# \1/g
s/%nohtml/- status: draft/g
Copy link

ghost commented Jan 18, 2019

I suggest to quote the filename variable to avoid the issue when there is space in the file name:

sed -f ex "$x" > "$filename"

@thestumbler
Copy link

I prepared a slight modification of your script. Mostly I wanted to put it into one file, not two...

#!/bin/bash
# ========================================================================
#       Converts VimWiki formatted markdown to Github markdown
# ========================================================================
# adapted from this script:
#   https://gist.github.com/hotoo/9866720
# must use gnused because of here-document
# watch out for the spaces and tabs with here-documents
#   https://unix.stackexchange.com/questions/76481/
#
# usage:
#
# vw2md.sh  [fileglob, e.g., wikidir/subdir/*.txt]
#
#   input can have any extension, .txt, .wiki, .markdown
#   output will be given .md extension (can be changed: $oext)

oext='md'
for infile in $@; do
  [ -e "$infile" ] || continue
  ext="${infile##*.}"
  base=$(basename "$infile" "$ext")
  dir=$(dirname "$infile")
  outfile="$dir"/"$base""$oext"
  #echo  infile "$infile"
  #echo base "$base"
  #echo dir "$dir"
  #echo outfile "$outfile"
  if [ ! -f "$outfile" ]
  then
    gsed -f - "$infile" > "$outfile" <<- '      SED_SCRIPT'
      s/# \(.*\)$/* \1/g
      s/^= \(.*\) =$/# \1/g
      s/^== \(.*\) ==$/## \1/g
      s/^=== \(.*\) ===$/### \1/g
      s/^==== \(.*\) ====$/#### \1/g
      s/^===== \(.*\) =====$/##### \1/g
      s/^====== \(.*\) ======$/###### \1/g
      s/{{{class="brush: *\([^"]*\)"/\`\`\`\1/g
      s/{{{class="\([^"]*\)"/\`\`\`\1/g
      s/{{{/\`\`\`/g
      s/}}}/\`\`\`/g
      s/\[\([^] ]\{1,\}\)\]\([^](]\)/![pic](\1)\2/g
      s/\[\([^] ]\{1,\}\)\]$/![pic](\1)/g
      s/\[\[\(\([^|]\{1,\}\)\|\)\([^]]\{1,\}\)\]\]/[\3](\2.md)/g
      s/\[\[\([^]]\{1,\}\)\]\]/[\1](\1.md)/g
      s/\[\(https\{0,1\}:\/\/[^ ]*\) \([^]]*\)\]/[\2](\1)/g
      s/%% \(.*\)/<!-- \1 -->/g
      /%toc.*/d
      s/%title \(.*\)/# \1/g
      s/%nohtml/- status: draft/g
      SED_SCRIPT
    echo "CONVERTED:  $outfile"
  else
    echo "skipped:    $outfile"
  fi
done

@reneatzi
Copy link

Thank you.

One improvement: You should double quote the variables to support whitespaces in filenames.

for x
do
  filename=$(echo "$x"|sed -e "s/\.wiki$/.md/")

  sed -f ex "$x" > "$filename"
done

And you can also rename the file with shell parameter expansion

filename=${x/.wiki/.md/}

@VimWei
Copy link

VimWei commented Dec 31, 2019

" vim 8.2取消了对'|'需要escape的要求,这个算是对其它类似命令一致性的改进/修复,但是也造成之前习惯\code的不兼容
" 更新如下:
%s/\[\[\(\([^|]\{1,\}\)|\)\([^]]\{1,\}\)\]\]/[\3](\2)/ge

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