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
@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