目标
- Typora配合Git脚本,通过软件启动或
F12按键触发同步。
优点
- 利用Git的特点保存文件修改历史。
- 利用Git仓库的界面可以在手机端查看数据。
缺点
- 依赖Git。
- 有一定的提交冲突风险,会导致同步失败(手动解决即可),个人使用基本遇不到。
环境
- Typora 1.10.8
- Windwos 11
步骤
-
笔记文件夹初始为Git仓库,可以参考教程:blog.csdn.net/abtain/arti…
-
笔记文件夹下创建名为
GitSync.bat的自动提交脚本,内容如下@echo off cd /d "%~dp0" echo %cd% git pull origin main git add . git commit -m "%date% %time%" git push origin main注意:第一步切换为笔记文件所在目录根据实际情况调整。
-
将下面的脚本内容存放在应用resources目录下,文件名为
typora-git.js:const {exec} = reqnode('child_process'); const batFile = 'D:/Storage/Markdown/GitSync.bat'; let working = false; function gitSync(e) { if (working) { return; } if (e.type !== 'load' && e.key !== 'F12') { return } working = true; exec(batFile, (error, stdout, stderr) => { working = false; if (error) { message(error.message); return; } if (e.key !== 'F12') { return; } message(stdout); }); } function message(msg) { showDialog({message: msg}); working = false; } window.addEventListener('load', gitSync); window.addEventListener('keydown', gitSync);注意:脚本内的路径调整为第2步
GitSync.bat的具体路径,注意/的写法,否则路径不生效。 -
在
Typora安装目录\resources\window.html尾部添加脚本:<script src="./typora-git.js"></script> -
重启Typora软件,每次启动软件或者软件启动后按下
F12键即可同步修改到远程Git仓库。