新建 Hugo文章时自动递增文章编号
前言 我现在使用 hugo 创建新的文章和瞬间用的是 hugo new posts/1/index.md 和 hugo new ramble/1/index.md 每次新建文章的时候还得去看一下我已经写了多少文章,我想的是,写一个脚本,自动判断我写了的文章数,然后每次自动加 1。 开始折腾 因为我是使用 Windows 系统,所以原生终端 powershell 是我常用的,所以也是决定选用 powershell 脚本 创建 PowerShell 脚本 在你的 hugo 项目的目录下,创建一个新文件,命名为 new.ps1 将以下内容复制进去: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 param ([string]$Type) if ($Type -ne "posts" -and $Type -ne "ramble") { Write-Host "用法: .\new_post.ps1 posts 或 .\new_post.ps1 ramble" -ForegroundColor Red exit } $path = "content/$Type" if (!(Test-Path $path)) { $nextNum = 1 } else { $maxNum = Get-ChildItem -Path $path | Where-Object { $_.PSIsContainer -and $_.Name -match '^\d+$' } | Select-Object -ExpandProperty Name | ForEach-Object { [int]$_ } | Sort-Object -Descending | Select-Object -First 1 if ($null -eq $maxNum) { $nextNum = 1 } else { $nextNum = $maxNum + 1 } } $targetPath = "$Type/$nextNum/index.md" hugo new $targetPath Write-Host "成功创建: content/$targetPath" -ForegroundColor Green 使用方式 在 Windows 资源管理器输入框中输入 powershell (如果是 CMD,请先输入 powershell 切换) 直接运行 ...