批量改文章为草稿

This commit is contained in:
Vaica committed 2026-01-20 23:57:39 +08:00
1 parent 1243526162
commit d9259d0eb2
109 files changed
+243

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
<#
.SYNOPSIS
将所有包含 'status: hidden' 的文章标记为草稿 (draft: true)。
.DESCRIPTION
此脚本用于批量处理 Hugo 文章。它会遍历 content/post 目录下的所有 .md 文件,
解析 Front Matter。如果发现 'status: hidden' 且没有 'draft: true',
则会在 'status: hidden' 下一行添加 'draft: true',以确保 Hugo 不会生成这些页面。
脚本包含安全检查:
1. 确保读取和写入使用 UTF-8 编码。
2. 确保新内容不为空。
3. 确保文件大小没有异常减小(防止数据丢失)。
.EXAMPLE
.\scripts\add_draft_to_hidden.ps1
#>
$postsPath = "content/post"
$files = Get-ChildItem -Path $postsPath -Recurse -Filter "*.md"
$count = 0
Write-Host "Starting scan for hidden posts..." -ForegroundColor Cyan
foreach ($file in $files) {
# Read with UTF8
$content = Get-Content $file.FullName -Raw -Encoding UTF8
if ([string]::IsNullOrEmpty($content)) {
Write-Warning "Skipping empty file: $($file.Name)"
continue
}
# Check for 'status: hidden'
if ($content -match "(?m)^status:\s*hidden") {
# Check if 'draft: true' is already present
if ($content -notmatch "(?m)^draft:\s*true") {
Write-Host "Processing: $($file.Name)"
# Use regex replace
# Capture the status: hidden line and append draft: true
# Note: `n is newline in PowerShell
$newContent = $content -replace "(?m)(^status:\s*hidden.*$)", "`$1`ndraft: true"
# SAFETY CHECK: Ensure new content is not empty and size is reasonable
if ([string]::IsNullOrEmpty($newContent) -or $newContent.Length -eq 0) {
Write-Error "CRITICAL: New content is empty for $($file.Name). Skipping save."
continue
}
# SAFETY CHECK: Ensure we haven't lost significant data (e.g. > 10% size drop)
if ($newContent.Length -lt ($content.Length * 0.9)) {
Write-Error "CRITICAL: Suspicious size drop for $($file.Name). Skipping save."
continue
}
# Save file using .NET method to ensure UTF8 no BOM (or compatible UTF8)
[System.IO.File]::WriteAllText($file.FullName, $newContent, [System.Text.Encoding]::UTF8)
Write-Host " -> Updated." -ForegroundColor Green
$count++
}
}
}
Write-Host "Completed. Updated $count files." -ForegroundColor Cyan