借助.bat更方便地写博客

起来开机,总是要手动进入博客目录,为什么不能像使用hexo那样操作博客呢?

准备工作

首先我们把博客目录写入到环境变量里,我给它起名叫BlogPath,这样做可以让我们在powershell里直接使用我们写好的.bat文件。

定位和打开hexo

文件名:blog.bat

1
2
title Blog Terminal
git-bash --cd=%BlogPath%

打开hexo服务端

文件名:blogS.bat

1
2
title Local Server Running...
git-bash --cd=%BlogPath% hexo s

一键部署

文件名:blogD.bat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
title Deploy to server...
echo 请使用Powershell运行脚本。
set /P judge="确定要部署到服务器吗?(Y/N)"
if %judge%==Y (
git-bash --cd=%BlogPath% hexo g
git-bash --cd=%BlogPath% hexo d
) else (
if %judge%==y (
git-bash --cd=%BlogPath% hexo g
git-bash --cd=%BlogPath% hexo d
) else (
echo 已取消部署。
)
)

2023/1/8更新

现在是真的用Powershell运行的了
额外增加功能:可以通过指令blogd y直接部署博客。

文件名:blogD.bat

1
powershell %BlogPath%blogD.ps1 %1

但是由于Powershell的策略,直接跑脚本会报“无法加载文件 ,因为在此系统上禁止运行脚本”的错误,你需要先在管理员模式下运行Powershell,然后粘贴
1
set-executionpolicy remotesigned

就可以了。接着在博客目录下加入这个
文件名:blogD.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Write-Host "请使用Powershell运行脚本。"
if ($args.Length -gt 0){
if ($args[0] -match "Y"){
git-bash --cd=$env:BlogPath hexo d
exit
} else {
if ($args[0] -match "y") {
git-bash --cd=$env:BlogPath hexo d
exit
}
}
}
Write-Host "确定要部署到服务器吗?(Y/N)"
$key = $Host.UI.RawUI.ReadKey()
Write-Host "" # 换行
If ($key -match 'Y') {
git-bash --cd=$env:BlogPath hexo d
} else {
if ($key -match 'y') {
git-bash --cd=$env:BlogPath hexo d
} else {
Write-Host "已取消部署。"
}
}

由于Powershell并不会串行执行git-bash的运行,使得git会同时进行博客文件的生成和部署。这里就删掉了hexo s的命令
当然,也可以加个blogDy.bat的文件,然后在里面直接powershell %BlogPath%blogD.ps1 y…那样的话这里做的保护措施就意义不大了。

一键打开博客目录

文件名:blogE.bat

1
2
title Blog in explorer
explorer %BlogPath%

在VSCode中打开博客

文件名:blogC.bat

1
2
title VSCode
code %BlogPath%

生成博客

文件名:blogG.bat

1
2
title Summon Blog Posts
git-bash --cd=%BlogPath% hexo g

作者

勇敢梧桐树

发布于

2023-01-01

更新于

2023-01-08

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×