2.2 设置上传图像的路径(必须)
打开Typora,进入菜单––>偏好设置––>图像,将文档中出现的图片保存在以下目录,并优先使用相对路径:
注意!⚠️插入图片时可以选择“复制到指定路径”,然后定位到
xxx\assets\${filename}_imgs
目录下(xxx是你放wiki的目录),这样你笔记中的所有图片都会保存在assets\笔记文件名_imgs
目录下。一定要勾选为相对路径添加./选项,勾选该选项后,假设你在wiki/a/test.md目录下创建一个图片img.jpg,他对应的url就是
..\..\assets\test\img.jpg
这样能够保证文档同步到云端时文档中的图片URL不会发生错误。当然,不一定非得把图片上传到assets目录下,也可以Typora自带的PicGo工具建立自己的图床,直接将图片上传到你的服务器中,而后在笔记中引入图片在你的服务器URL即可。想这样试试的兄弟可以参考Typora如何配置gitee图床?超详细教程!-CSDN博客。 {.is-warning}
2.3 将文档上传至云端(必须)
目前的方案是使用git进行同步,使用git工具进行同步。
同步文档的方式与同步代码相同,设置好ssh key(Github如何设置ssh key)后,
-
使用
git pull origin main
拉取远端代码更新本地。 -
当本地新建/修改/删除笔记后,需要使用
git add .
、git commit -m "your commit message"
以及git push origin main
将代码提交到远程仓库
2.4 自动上传文档至云端(可选)
显然,上面的方法都要提交三个命令,有点太不优雅了😅😅,需要寻找一种本地改完文档,直接提交到远程仓库的方案。目前有两种方案:
-
当打开Typora程序后附带运行一个脚本,脚本中实现自动同步
-
编写脚本,每五分钟检查本地和远端的代码更新情况
-
# 当前进程 PID $thisPid = $PID # 锁文件和日志文件路径 $lockFile = "F:\DevelopmentApps\Typora\watch_typora_git.lock" $logFile = "F:\DevelopmentApps\Typora\watch_typora_log.txt" # 日志函数 function Log { param ($message) $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" # 清除多余空白字符(包括换行符、制表符、多余空格) $cleanMessage = ($message -replace '\s+', ' ').Trim() $logMessage = "$timestamp - $cleanMessage" # 用 UTF-8 无 BOM 编码追加日志 [System.IO.File]::AppendAllText($logFile, "$logMessage`n", [System.Text.Encoding]::UTF8) } # 锁文件检查逻辑 if (Test-Path $lockFile) { try { $existingPid = Get-Content $lockFile -ErrorAction Stop $stillRunning = Get-Process -Id $existingPid -ErrorAction SilentlyContinue if ($stillRunning) { Log "Script is already running with PID $existingPid. Exiting..." exit } else { Log "Stale lock detected from PID $existingPid. Replacing with current PID $thisPid." Set-Content -Path $lockFile -Value $thisPid -Encoding UTF8 } } catch { Log "Lock file unreadable. Overwriting with current PID $thisPid." Set-Content -Path $lockFile -Value $thisPid -Encoding UTF8 } } else { Set-Content -Path $lockFile -Value $thisPid -Encoding UTF8 Log "Lock file created with PID $thisPid." } # 设置输出编码 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 chcp 65001 # Git 仓库路径和 SSH 密钥路径(处理中文) $repoPath = [System.Text.Encoding]::UTF8.GetString([System.Text.Encoding]::Default.GetBytes("path\\to\\your\\git\\repo")) $sshKeyPath = [System.Text.Encoding]::UTF8.GetString([System.Text.Encoding]::Default.GetBytes("Path_to_your_sshkey\\.ssh\\wikijs_id_rsa")) # Typora 进程名 $processName = "Typora" # 提交间隔(秒) $interval = 300 # 切换工作目录 try { Set-Location -LiteralPath $repoPath Log "Changed directory to: $repoPath" # Git 设置与变更检测 git config core.sshCommand "ssh -i $sshKeyPath" git config core.quotepath false } catch { Log "Error: Cannot access path $repoPath" Remove-Item -Path $lockFile -Force exit } Log "Starting Git Auto-Commit Script for Typora..." Write-Host "Starting Git Auto-Commit Script for Typora..." # 主运行逻辑 try { while ($true) { # 检查 Typora 是否运行 $process = Get-Process -Name $processName -ErrorAction SilentlyContinue if (-not $process) { Log "Typora is not running. Exiting script..." Write-Host "Typora is not running. Exiting script..." break } # 更新本地仓库 git pull origin main $changes = git status --porcelain | Select-String "^( M| D|\?\?)" | Out-String if ($changes -match "\S") { Log "Changes detected. Committing..." Log $changes Write-Host "Changes detected. Committing..." Write-Host $changes git add . $commitMessage = "Auto-commit: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" git commit -m $commitMessage git push origin main Log "Committed and pushed: $commitMessage" Write-Host "Committed and pushed: $commitMessage" } else { Log "No changes detected." Write-Host "No changes detected." } Write-Host "Sleeping for $interval seconds..." Start-Sleep -Seconds $interval } } finally { # 清理锁文件 if (Test-Path $lockFile) { Remove-Item -Path $lockFile -Force Log "Lock file removed on exit." } }
启动Typora时,自动启动该脚本(与vbs方法二选一即可)
修改Typora快捷方式中的目标,改为:
C:\Windows\System32\cmd.exe /c "start F:\DevelopmentApps\Typora\Typora.exe & start powershell.exe -NoProfile -ExecutionPolicy Bypass -File F:\DevelopmentApps\Typora\watch_typora_git.ps1"
- 这样,我们启动Typora程序的时候自动会打开powershell窗口,自动同步
-
- 隐藏powershell窗口运行(与修改快捷方式方案二选一即可)
可能有人比较讨厌😤😤黑黑的powershell窗口,这时候可以写个.vbs脚本来隐藏这个脚本,代码如下。
' 任意位置创建a.vbs文件
Set objShell = CreateObject("WScript.Shell")
' 启动 Typora,注意修改路径
objShell.Run """F:\DevelopmentApps\Typora\Typora.exe""", 1, False
' 等待 2秒
WScript.Sleep 2000
' 运行 PowerShell 脚本(隐藏窗口), 注意修改路径
objShell.Run "powershell -ExecutionPolicy Bypass -File F:\DevelopmentApps\Typora\watch_typora_git.ps1", 0, False
运行这个脚本就能启动Typora和静默启动同步脚本了。
-
将图标改回Typora(可选)
可以发现改完快捷方式后,图标变成了丑丑的cmd.exe图标,我们在快捷方式–>更改图标里面定位到Typora.exe程序ok了。