Last active
April 15, 2023 01:15
-
-
Save hotoo/9866720 to your computer and use it in GitHub Desktop.
convert vimwiki to markdown: `sed -f ex -i *.md`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
for x | |
do | |
filename=$(echo $x|sed -e "s/\.wiki$/.md/") | |
sed -f ex $x > $filename | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
I suggest to quote the filename variable to avoid the issue when there is space in the file name:
sed -f ex "$x" > "$filename"
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
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/}
" 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
这个脚本可以将 vimwiki 语法的项目,自动转换成 markdown 语法。
转换的内容包括:
转换方法
假设上面的脚本保存结构如下:
在 convert.sh 同级目录执行:
然后自动或手动微调之后,删除
.wiki
文件。