原创博客地址:脚本_vnote同步到hexo步骤[博]
1,批量跑md2hexo命令
1
2
3
4
5
6
7
| cd /home/john/文档/vnote_notebooks/vnote
conda activate py35
python md2hexo.py $(ls -I _v_recycle_bin)
变更部分比对
gitk:验证只有文件头部被修改
或者:meld .
|
2,关于[博]文章迁移到_post文件夹
1
2
3
4
5
6
7
8
9
10
11
12
13
| 删除原内容
rm ~/my_hexo/source/_posts/*
rm ~/my_hexo/source/images/*
切换到临时目录
cd ~/my_tmp
rm md_file_loc.txt tmp_png/* tmp_png_choose.txt tmp_png.txt
所有含有[博]的md文件绝对路径写到md_file_loc.txt
find ~/文档/vnote_notebooks/vnote/ -path ~/文档/vnote_notebooks/vnote/_v_recycle_bin -prune -o -name "*\[博\].md" -print > md_file_loc.txt
md文件名含有扩号的需要转义
sed -i 's/(/\\(/g;s/)/\\)/g' md_file_loc.txt
将md文件copy到surce/_post文件夹
awk -v to="/home/john/my_hexo/source/_posts/" '{cmd="cp "$0" "to ; system(cmd)}' md_file_loc.txt
|
3,md相关图片复制(复制到source/images/)
1
2
3
4
5
6
7
8
9
| 所有图片文件绝对路径写到tmp_png.txt
find ~/文档/vnote_notebooks/vnote/ -path ~/文档/vnote_notebooks/vnote/_v_recycle_bin -prune -o -regex ".*\.jpg\|.*\.png" -print > tmp_png.txt
所有图片cp到my_tmp/tmp_png中
mkdir tmp_png
awk -v to="/home/john/my_tmp/tmp_png/" '{cmd="cp "$0" "to ; system(cmd)}' tmp_png.txt
从md中提取md涉及的文件名
awk -F / -v patten="'[0-9_]+\.png'" -v mdPath="/home/john/my_hexo/source/_posts/" '{cmd= "grep -o -E " patten " " mdPath $NF;system(cmd)}' md_file_loc.txt > tmp_png_choose.txt
将涉及到的文件copy到source/images中
awk -v from="./tmp_png/" -v to="/home/john/my_hexo/source/images/" '{cmd="mv "from $1 " " to ;system(cmd)}' tmp_png_choose.txt
|
4,修改各md文件中路径(_posts/下的md替换/images为/images)
1
2
3
4
| image的路径替换
awk -F / -v newPath="/home/john/my_hexo/source/_posts/" -v sedCmd="'s//images/\\\\/images/g'" '{cmd= "sed -i " sedCmd " " newPath $NF;system(cmd) }' md_file_loc.txt
image后尺寸控制的替换(hexo不支持尺寸控制)
awk -F / -v newPath="/home/john/my_hexo/source/_posts/" -v sedCmd="'s/ =[0-9]\+x)/)/g'" '{cmd= "sed -i " sedCmd " " newPath $NF;system(cmd) }' md_file_loc.txt
|
5,发布到服务器
1
| cd ~/my_hexo;hexo g && hexo deploy
|
6,补充password字段
需要加密的文档添加password字段
7,整合后脚本
从第二步开始(第一步和发布无关,属于vnote格式自整理)
脚本目的,将vnote格式的md文件,统一copy到hexo文件路径(md文件+图片文件)
$1:vnote笔记目录,比如:/文档/vnote_notebooks/vnote
$2:hexo的source文件夹位置,比如:/my_hexo/source(会删除此文件夹下的_posts/和images/下的内容)
$3:*[博客].md
命令:./xx.sh ~/文档/vnote_notebooks/vnote ~/my_hexo/source “*[博客].md”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| # 删除hexo已有的文章和附件
echo "删除hexo博客下的文件夹_posts,images"
rm ~/my_hexo/source/_posts/*
rm ~/my_hexo/source/images/*
# 创建临时目录
echo "创建临时目录"
mkdir -p ~/tmp_hexo/tmp_png/;cd ~/tmp_hexo
# 需发布到hexo文件路径写到md_file_loc.txt中,并且生成copy命令列表
echo "生成hexo的文件copy命令"
find ~/文档/vnote_notebooks/vnote/ -path ~/文档/vnote_notebooks/vnote/_v_recycle_bin -prune -o -name "*\[博\].md" -print | grep -v total > md_file_loc.txt
sed -i 's/(/\\(/g;s/)/\\)/g' md_file_loc.txt
awk -v to="/home/john/my_hexo/source/_posts/" '{cmd="cp "$0" "to ; system(cmd)}' md_file_loc.txt
# 将hexo文件中的图片地址,提取到md_file_loc.txt中
echo "hexo文件中的图片地址,提取到md_file_loc.txt中"
find ~/文档/vnote_notebooks/vnote/ -path ~/文档/vnote_notebooks/vnote/_v_recycle_bin -prune -o -regex ".*\.jpg\|.*\.png" -print | grep -v total > tmp_png.txt
awk -v to="/home/john/my_tmp/tmp_png/" '{cmd="cp "$0" "to ; system(cmd)}' tmp_png.txt
awk -F / -v patten="'[0-9_]+\.png'" -v mdPath="/home/john/my_hexo/source/_posts/" '{cmd= "grep -o -E " patten " " mdPath $NF;system(cmd)}' md_file_loc.txt | grep -v total > tmp_png_choose.txt
awk -v from="./tmp_png/" -v to="/home/john/my_hexo/source/images/" '{cmd="mv "from $1 " " to ;system(cmd)}' tmp_png_choose.txt
# 替换文件路径(vnote图片位于/images下,而hexo图片位置为images)
echo "替换文件路径"
awk -F / -v newPath="/home/john/my_hexo/source/_posts/" -v sedCmd="'s//images/\\\\/images/g'" '{cmd= "sed -i " sedCmd " " newPath $NF;system(cmd) }' md_file_loc.txt
awk -F / -v newPath="/home/john/my_hexo/source/_posts/" -v sedCmd="'s/ =[0-9]\+x)/)/g'" '{cmd= "sed -i " sedCmd " " newPath $NF;system(cmd) }' md_file_loc.txt
# 删除临时目录
echo "删除临时目录"
rm md_file_loc.txt tmp_png.txt tmp_png_choose.txt
rm -rf tmp_png/*
# 发布到hexo
echo "发布到hexo"
cd ~/my_hexo;hexo g && hexo deploy
echo "完成"
|
8,整理为参数的脚本形式(github)
脚本地址:github:https://github.com/yuanjh6/scripts
基于vnote的自动发布
将vnote中的符合条件的文章(.md文件),复制到hexo/source/_posts/,
符合条件的md文章里面涉及的图片,复制到hexo/source/images/
使用方法:
1
| ./vnote2hexo.sh ~/文档/vnote_notebooks/vnote ~/my_hexo/source "*发布*.md"
|
大概执行路径:
find
vnote笔记本路径:~/文档/vnote_notebooks/vnote
找出其中文明名符合:”发布.md”
的md文件,将其copy到
hexo的source文件夹(路径):~/my_hexo/source/_posts/(后面的_posts脚本写死的)
同时,符合条件的md文件里面的.png,.jpeg等文件会复制到
hexo的source文件夹(路径):~/my_hexo/source/images/(后面的images脚本写死的)
代码行数不多,但是较难看懂,主要是awk语法使用较多,其中很多涉及多次转义的字符
参考本博客博文:脚本_vnote同步到hexo步骤[博](自行搜索)