为博客创建时兼得hexo n
的便捷和cp
的快速,改进[[使用 Git 管理 Hexo 博客目录]]和[[Hexo 博客和 Obsidian 的协作]]中的脚本如下,将变量$front
写入 Markdown 文件,并且创建硬链接
$count = [System.IO.Directory::GetFiles("D:\Blog\source\_posts").Count
$date = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$front = "---" + "`ntitle: $count" + "`ndate: '$date'" + "`nnum: '$count'" + "`nupdated: '$date'" + "`ncomments: true" + "`npin: false" + "`ncategories: []" + "`ntags: [blog]" + "`nlink:" + "`nheadimg:" + "`nexcerpt:" +"`n`n---"
Write-Output $front | Out-File D:\Blog\source\_posts\$count.md
New-Item D:\Note\$count.md -Type HardLink -Target D:\Blog\source\_posts\$count.md
期望将指定 Yaml 头写入文件,然而在 VSCode、Typora、Obsidian 打开生成文件却发现完全乱码,不同程序表现不同,但均不能识别 Yaml 头。
修改 Powershell 编码
查看 VSCode 报告生成 Markdown 文件为UTF16??
码,而程序需要 UTF-8 编码,且部分程序英文显示正常,推测为编码不支持中文问题。在 Powershell 中输入chcp
得到
活动代码页: 936
得知 Powershell 默认为 GBK 编码,意图修改 Powershell 编码。有几种方式:
- 全局修改:在控制面板中勾选「使用 UTF-8 提供全球语言支持」。全局修改编码,重启后生效。但听闻有中文软件乱码问题,未求证。
- Powershell 修改:将
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
字段写入 $Profile 文件。仅修改 Powershell 配置。 - Out-File 参数:在 Out-File 后面指定编码,参数为
-Encoding
,例如Out-File file.md -Encoding utf8
。支持全部编码参阅帮助文档。
然而即使将编码修改为 UTF 8,输出的文件编码并非 UTF 8。可以在 Typora 中识别,但无法在 Obsidian 中识别,且 VSCode 显示并非 UTF-8 编码。
修改执行终端
推测可能为 Powershell 5.0 Bug,尝试修改脚本执行程序为 Powershell.core,成功。Bug 并未确证,具体原因不明。