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