1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
param( [Parameter(Mandatory=$false, Position=0)] [string]$category = "道祖之道", [Parameter(Mandatory=$false, Position=1)] [string]$title,
[Parameter(Mandatory=$false)] [string]$writedate,
[Parameter(Mandatory=$false)] [switch]$ShowDetails )
$modulesDir = "./psm-modules" $modules = @( "$modulesDir\CategoryMap.psm1", "$modulesDir\FolderTree.psm1", "$modulesDir\PathConverter.psm1" ) foreach ($module in $modules) { if (Test-Path $module) { try { Import-Module $module -Force -ErrorAction Stop if ($ShowDetails) { Write-Host "✅ 导入模块: $(Split-Path $module -Leaf)" -ForegroundColor Green } } catch { Write-Host "❌ 无法导入模块 $module : $_" -ForegroundColor Red exit 1 } } else { Write-Host "❌ 模块文件不存在: $module" -ForegroundColor Red Write-Host "请确保以下模块文件存在:" -ForegroundColor Yellow $modules | ForEach-Object { Write-Host " • $_" -ForegroundColor Gray } exit 1 } }
$categoryMap = Get-CategoryMap -Silent:(!$ShowDetails) if ($categoryMap.Count -eq 0) { Write-Host "❌ 无法加载分类映射" -ForegroundColor Red exit 1 }
$tree = Get-FolderTree -RootPath "source/_posts" -Silent:(!$ShowDetails) if (-not $tree) { Write-Host "❌ 无法扫描目录结构" -ForegroundColor Red exit 1 }
$defcategory=$category -eq "道祖之道"
function Get-foundFolder { $foundFolders = $tree.Find($category) if ($foundFolders -and $foundFolders.Count -gt 0) { if ($foundFolders.Count -gt 1) { if ($ShowDetails) { Write-Host "⚠️ 找到多个匹配的文件夹,使用第一个" -ForegroundColor Yellow $foundFolders | ForEach-Object { Write-Host " • $_" -ForegroundColor Gray } } $foundFolder = $foundFolders[0] } else { $foundFolder = $foundFolders } return $foundFolder
} else{ Write-Host "❌ 未找到包含 '$category' 的分类文件夹" -ForegroundColor Red if ($categoryMap.Count -gt 0) { Write-Host "`n📋 可用的分类映射:" -ForegroundColor Yellow $groups = @{ "道经卷" = $categoryMap.Keys | Where-Object { $_ -match "^道经卷|法则篇|道演篇" } "道境卷" = $categoryMap.Keys | Where-Object { $_ -match "^道境卷|道境之门|境界论述|实修根本|实修经|实修理术" } "实践方向" = $categoryMap.Keys | Where-Object { $_ -match "^实践方向|哲学之道|科学之道|技术之道|道祖之道" } "其他" = $categoryMap.Keys | Where-Object { $_ -notmatch "^道经卷|道境卷|实践方向" } } foreach ($groupKey in $groups.Keys) { if ($groups[$groupKey].Count -gt 0) { Write-Host "`n ${groupKey}:" -ForegroundColor Cyan $groups[$groupKey] | Sort-Object | ForEach-Object { Write-Host " • $_ → $($categoryMap[$_])" -ForegroundColor DarkGray } } } } Write-Host "`n📁 可用的顶层文件夹:" -ForegroundColor Yellow if (-not ($tree | Get-Member -Name GetSubfolders -MemberType ScriptMethod)) { $tree | Add-Member -MemberType ScriptMethod -Name GetSubfolders -Value { param([string]$ParentPath) if (-not $ParentPath) { return $this.AllFolders | Where-Object { $_ -notmatch "/" } } $this.AllFolders | Where-Object { $_ -like "$ParentPath/*" -and $_ -ne $ParentPath } } } $topLevelFolders = $tree.GetSubfolders("") if ($topLevelFolders) { $topLevelFolders | ForEach-Object { Write-Host " • $_" -ForegroundColor Gray } } exit 0 }
}
function Get-WriteDate{ if (-not [string]::IsNullOrEmpty($writedate)) { if ($writedate.Length -ne 12 -or $writedate -notmatch '^\d+$') { Write-Error "参数格式错误:必须为12位数字 (yyyyMMddHHmm)" exit 1 } try { $date= [datetime]::ParseExact($writedate, "yyyyMMddHHmm", $null) $writingDate=$date.AddHours(-8).ToString("yyyy-MM-dd HH:mm:ss") return $date,$writingDate } catch { Write-Error "无效的日期时间值:$writedate" exit 1 } }else{ $writingDate="{{ date }}" $date=Get-Date return $date,$writingDate } }
function Get-PostInfo {
$now,$writingDate = Get-WriteDate
if ([string]::IsNullOrEmpty($title)) { $title = "$($now.Year)年$($now.Month)月$($now.Day)日$($now.Hour)时$($now.Minute)分" Write-Host "默认标题: $title" -ForegroundColor Yellow }
if($defcategory){Write-Host "默认分类: $category" -ForegroundColor Yellow} if ($category -eq $title) { $posturl = "index" } else { $timestamp = [DateTimeOffset]::Now.ToUnixTimeMilliseconds() $posturl = "$timestamp" } $PostTitle = $title
$foundFolder=Get-foundFolder
$categories=$foundFolder $categories=$categories.Replace($($tree.GetSubfolders("")+"/"),"[") $categories=$categories.Replace("/",",")+"]" $categories="categories: $categories"
if($defcategory){ $foundFolder="$foundFolder/$($now.Year)/$($now.Month)" Write-Host "默认路径: $foundFolder" -ForegroundColor Yellow }
$permalink = Convert-PathToEnglish -RelativePath $foundFolder -CategoryMap $categoryMap $permalinkPath = "permalink: /$permalink/$posturl/"
$hide = "hide: ture"
return $foundFolder,$PostTitle,$categories,$permalinkPath,$hide,$writingDate }
function Set-ProcScaffold { param( [string]$PostTitle, [string]$writingDate, [string]$categories, [string]$permalinkPath, [string]$hide )
$postTemplate = "post.md" $newTemplate = "newpost.md" $templatePath = Join-Path (Get-Location) "scaffolds\$postTemplate" $newTemplatePath = Join-Path (Get-Location) "scaffolds\$newTemplate" if (Test-Path $templatePath) { Copy-Item $templatePath $newTemplatePath -Force $templateContent = Get-Content $newTemplatePath
if ($templateContent -match 'title:\s*{{ title }}') { $templateContent = $templateContent -replace 'title:\s*{{ title }}', "title: $PostTitle" } if ($templateContent -match 'writing_date:\s*.*') { $templateContent = $templateContent -replace 'writing_date:\s*.*', "writing_date: $writingDate" } if ($templateContent -match 'categories:\s*.*') { $templateContent = $templateContent -replace 'categories:\s*.*', "$categories" } else { $templateContent = $templateContent -replace '---', "---`n$categories" } if ($templateContent -match 'permalink:\s*.*') { $templateContent = $templateContent -replace 'permalink:\s*.*', "$permalinkPath" } else { $templateContent = $templateContent -replace '---', "---`n$permalinkPath" } if ($templateContent -match 'hide:\s*.*') { $templateContent = $templateContent -replace 'hide:\s*.*', "$hide" } else { $templateContent = $templateContent -replace '---', "---`n$hide" }
Set-Content -Path $newTemplatePath -Value $templateContent -Encoding UTF8
if ($ShowDetails) { Write-Host "📄 更新模板内容:" -ForegroundColor Gray Write-Host " permalink: $permalinkPath" -ForegroundColor DarkGray Write-Host " categories: $categories" -ForegroundColor DarkGray }
return $newTemplatePath } else{ Write-Host "❌ 模板文件不存在: $templatePath" -ForegroundColor Red Write-Host "请确保存在 scaffolds\post.md 文件" -ForegroundColor Yellow exit 0 } }
function Set-NewPost{ param( [string]$foundFolder, [string]$PostTitle ) try { $fullPostPath = Join-Path (Get-Location) "source/_posts" $foundFolder $PostTitle $fullPostPath = $fullPostPath.Replace("/", "\") + ".md"
if (Test-Path $fullPostPath) { Write-Host "⚠️ 文件已存在: $fullPostPath" -ForegroundColor Yellow $overwrite = Read-Host "是否覆盖?(y/n)" if ($overwrite -ne 'y') { Write-Host "❌ 已取消创建" -ForegroundColor Red exit 0 } } hexo new newpost --path "$foundFolder/$PostTitle" $PostTitle code "source/_posts/$foundFolder/$PostTitle.md" Write-Host "✅ 成功创建标题为'$PostTitle'的文章" -ForegroundColor Green Write-Host "📁 路径: source/_posts/$foundFolder/$PostTitle.md" -ForegroundColor Cyan } catch { Write-Host "❌ 执行Hexo命令时出错: $_" -ForegroundColor Red Write-Host "手动创建文件..." -ForegroundColor Yellow } }
$foundFolder,$PostTitle,$categories,$permalinkPath,$hide,$writingDate=Get-PostInfo $newTemplatePath=Set-ProcScaffold -PostTitle:($PostTitle) -categories:($categories) -permalinkPath:($permalinkPath) -hide:($hide) -writingDate:($writingDate) Set-NewPost -foundFolder:($foundFolder) -PostTitle:($PostTitle)
if (Test-Path $newTemplatePath) { Remove-Item $newTemplatePath -Force }
Remove-Module CategoryMap, FolderTree, PathConverter -ErrorAction SilentlyContinue
Write-Host "`n✅ 脚本执行完成" -ForegroundColor Green
|