2026-01-23 13:19:05 +08:00
|
|
|
|
# 设置控制台编码为 UTF-8
|
|
|
|
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
2026-01-20 21:13:24 +08:00
|
|
|
|
|
2026-01-23 13:19:05 +08:00
|
|
|
|
Write-Host "==========================================" -ForegroundColor Cyan
|
|
|
|
|
|
Write-Host " 📝 新建博客文章脚本 (Hugo) " -ForegroundColor Cyan
|
|
|
|
|
|
Write-Host "==========================================" -ForegroundColor Cyan
|
|
|
|
|
|
Write-Host ""
|
2026-01-20 21:13:24 +08:00
|
|
|
|
|
2026-01-23 13:19:05 +08:00
|
|
|
|
# 1. 获取文章标题
|
|
|
|
|
|
do {
|
|
|
|
|
|
$title = Read-Host "请输入文章标题 (必填)"
|
|
|
|
|
|
} while ([string]::IsNullOrWhiteSpace($title))
|
2026-01-20 21:13:24 +08:00
|
|
|
|
|
2026-01-23 13:19:05 +08:00
|
|
|
|
# 2. 获取文件名 Slug (默认为标题)
|
|
|
|
|
|
$slug = Read-Host "请输入文件名/Slug (选填,留空默认使用标题,建议使用英文)"
|
2026-01-20 21:13:24 +08:00
|
|
|
|
if ([string]::IsNullOrWhiteSpace($slug)) {
|
2026-01-23 13:19:05 +08:00
|
|
|
|
$slug = $title
|
2026-01-20 21:13:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-23 13:19:05 +08:00
|
|
|
|
# 3. 获取分类
|
|
|
|
|
|
$categoriesInput = Read-Host "请输入分类 (选填,多个分类用空格或逗号分隔)"
|
|
|
|
|
|
if (-not [string]::IsNullOrWhiteSpace($categoriesInput)) {
|
|
|
|
|
|
# 处理分隔符:将中文逗号、英文逗号替换为空格,然后分割
|
|
|
|
|
|
$categories = $categoriesInput -replace ",", " " -replace ",", " " -split "\s+"
|
|
|
|
|
|
# 重新组合成 JSON 数组格式字符串 ["A", "B"]
|
|
|
|
|
|
$categoriesStr = '["' + ($categories -join '", "') + '"]'
|
2026-01-20 21:13:24 +08:00
|
|
|
|
} else {
|
2026-01-23 13:19:05 +08:00
|
|
|
|
$categoriesStr = '["生活"]' # 默认分类
|
2026-01-20 21:13:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-23 13:19:05 +08:00
|
|
|
|
# 4. 获取标签
|
|
|
|
|
|
$tagsInput = Read-Host "请输入标签 (选填,多个标签用空格或逗号分隔)"
|
|
|
|
|
|
if (-not [string]::IsNullOrWhiteSpace($tagsInput)) {
|
|
|
|
|
|
$tags = $tagsInput -replace ",", " " -replace ",", " " -split "\s+"
|
|
|
|
|
|
$tagsStr = '["' + ($tags -join '", "') + '"]'
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$tagsStr = '[]'
|
|
|
|
|
|
}
|
2026-01-20 21:13:24 +08:00
|
|
|
|
|
2026-01-23 13:19:05 +08:00
|
|
|
|
# 5. 获取作者
|
|
|
|
|
|
$author = Read-Host "请输入作者 (选填,默认为 '小赵同学')"
|
|
|
|
|
|
if ([string]::IsNullOrWhiteSpace($author)) {
|
|
|
|
|
|
$author = "小赵同学"
|
|
|
|
|
|
}
|
2026-01-20 21:13:24 +08:00
|
|
|
|
|
2026-01-23 13:30:42 +08:00
|
|
|
|
# 6. 生成文件夹和文件名 (Page Bundles 模式: YYYY-MM-DD-Slug/index.md)
|
2026-01-23 13:19:05 +08:00
|
|
|
|
$date = Get-Date
|
|
|
|
|
|
$dateStr = $date.ToString("yyyy-MM-dd")
|
|
|
|
|
|
$dateTimeStr = $date.ToString("yyyy-MM-ddTHH:mm:ss+08:00")
|
|
|
|
|
|
# 移除文件名中的非法字符
|
|
|
|
|
|
$safeSlug = $slug -replace '[\\/:*?"<>|]', '' -replace '\s+', '-'
|
2026-01-23 13:30:42 +08:00
|
|
|
|
$folderName = "$dateStr-$safeSlug"
|
|
|
|
|
|
$folderPath = Join-Path "content/post" $folderName
|
|
|
|
|
|
$filePath = Join-Path $folderPath "index.md"
|
2026-01-23 13:19:05 +08:00
|
|
|
|
|
|
|
|
|
|
# 7. 生成 Front Matter 内容
|
|
|
|
|
|
$content = @"
|
|
|
|
|
|
---
|
|
|
|
|
|
title: "$title"
|
|
|
|
|
|
date: $dateTimeStr
|
|
|
|
|
|
slug: $safeSlug
|
|
|
|
|
|
draft: false
|
|
|
|
|
|
categories: $categoriesStr
|
|
|
|
|
|
tags: $tagsStr
|
|
|
|
|
|
author: "$author"
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
|
|
这里开始写你的精彩内容...
|
|
|
|
|
|
|
|
|
|
|
|
"@
|
|
|
|
|
|
|
|
|
|
|
|
# 8. 写入文件
|
|
|
|
|
|
try {
|
2026-01-23 13:30:42 +08:00
|
|
|
|
# 确保父目录存在
|
2026-01-23 13:19:05 +08:00
|
|
|
|
if (-not (Test-Path "content/post")) {
|
|
|
|
|
|
New-Item -ItemType Directory -Path "content/post" | Out-Null
|
2026-01-20 21:13:24 +08:00
|
|
|
|
}
|
2026-01-23 13:30:42 +08:00
|
|
|
|
|
|
|
|
|
|
# 创建文章专属目录
|
|
|
|
|
|
if (-not (Test-Path $folderPath)) {
|
|
|
|
|
|
New-Item -ItemType Directory -Path $folderPath | Out-Null
|
|
|
|
|
|
}
|
2026-01-20 21:13:24 +08:00
|
|
|
|
|
2026-01-23 13:19:05 +08:00
|
|
|
|
# 检查文件是否已存在
|
|
|
|
|
|
if (Test-Path $filePath) {
|
2026-01-23 13:30:42 +08:00
|
|
|
|
Write-Host "⚠️ 文件已存在: $filePath" -ForegroundColor Yellow
|
2026-01-23 13:19:05 +08:00
|
|
|
|
$overwrite = Read-Host "是否覆盖? (y/n)"
|
|
|
|
|
|
if ($overwrite -ne 'y') {
|
|
|
|
|
|
Write-Host "❌ 操作已取消" -ForegroundColor Red
|
|
|
|
|
|
exit
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# 写入文件 (使用 UTF8 编码)
|
|
|
|
|
|
$content | Set-Content -Path $filePath -Encoding UTF8
|
2026-01-20 21:13:24 +08:00
|
|
|
|
|
2026-01-23 13:19:05 +08:00
|
|
|
|
Write-Host ""
|
|
|
|
|
|
Write-Host "✅ 文章创建成功!" -ForegroundColor Green
|
|
|
|
|
|
Write-Host "📂 文件路径: $filePath" -ForegroundColor Gray
|
|
|
|
|
|
Write-Host "🔗 文章标题: $title" -ForegroundColor Gray
|
|
|
|
|
|
Write-Host "📅 创建时间: $dateTimeStr" -ForegroundColor Gray
|
|
|
|
|
|
Write-Host ""
|
|
|
|
|
|
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
Write-Host "❌ 创建失败: $_" -ForegroundColor Red
|
2026-01-20 21:13:24 +08:00
|
|
|
|
}
|
2026-01-23 13:19:05 +08:00
|
|
|
|
|
|
|
|
|
|
# 暂停以便用户查看输出
|
|
|
|
|
|
Write-Host "按任意键退出..."
|
|
|
|
|
|
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
|