2026-01-20 23:57:39 +08:00
|
|
|
|
|
|
|
|
|
|
<#
|
|
|
|
|
|
.SYNOPSIS
|
2026-01-21 12:34:55 +08:00
|
|
|
|
将所有包含 'status: hidden' 的文章标记为草稿 (draft: true),并清理冲突的设置。
|
2026-01-20 23:57:39 +08:00
|
|
|
|
|
|
|
|
|
|
.DESCRIPTION
|
2026-06-02 15:37:49 +08:00
|
|
|
|
此脚本用于批量处理 Hugo 文章。它会遍历 content/posts 目录下的所有 .md 文件。
|
2026-01-21 12:34:55 +08:00
|
|
|
|
如果发现 'status: hidden':
|
|
|
|
|
|
1. 将 'draft: false' 修改为 'draft: true'。
|
|
|
|
|
|
2. 注释掉冲突的 'status: public' 设置。
|
|
|
|
|
|
3. 如果没有 'draft: true',则添加它。
|
2026-01-20 23:57:39 +08:00
|
|
|
|
|
|
|
|
|
|
脚本包含安全检查:
|
|
|
|
|
|
1. 确保读取和写入使用 UTF-8 编码。
|
|
|
|
|
|
2. 确保新内容不为空。
|
2026-01-21 12:34:55 +08:00
|
|
|
|
3. 确保文件大小没有异常减小。
|
2026-01-20 23:57:39 +08:00
|
|
|
|
|
|
|
|
|
|
.EXAMPLE
|
|
|
|
|
|
.\scripts\add_draft_to_hidden.ps1
|
|
|
|
|
|
#>
|
|
|
|
|
|
|
2026-06-02 15:37:49 +08:00
|
|
|
|
$postsPath = "content/posts"
|
2026-01-21 12:34:55 +08:00
|
|
|
|
# Ensure path exists
|
|
|
|
|
|
if (-not (Test-Path $postsPath)) {
|
|
|
|
|
|
Write-Error "Path not found: $postsPath"
|
|
|
|
|
|
exit 1
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 23:57:39 +08:00
|
|
|
|
$files = Get-ChildItem -Path $postsPath -Recurse -Filter "*.md"
|
|
|
|
|
|
$count = 0
|
|
|
|
|
|
|
2026-01-21 12:34:55 +08:00
|
|
|
|
Write-Host "Starting scan for hidden posts cleanup..." -ForegroundColor Cyan
|
|
|
|
|
|
|
|
|
|
|
|
# Set console encoding to UTF8 to correctly display filenames
|
|
|
|
|
|
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
2026-01-20 23:57:39 +08:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 12:34:55 +08:00
|
|
|
|
$modified = $false
|
|
|
|
|
|
$newContent = $content
|
|
|
|
|
|
|
|
|
|
|
|
# Check for 'status: hidden' or 'status: "hidden"'
|
|
|
|
|
|
if ($content -match "(?m)^status:\s*(`"hidden`"|hidden)") {
|
2026-01-20 23:57:39 +08:00
|
|
|
|
|
2026-01-21 12:34:55 +08:00
|
|
|
|
# 1. Replace draft: false -> draft: true
|
|
|
|
|
|
if ($newContent -match "(?m)^draft:\s*false") {
|
|
|
|
|
|
$newContent = $newContent -replace "(?m)^draft:\s*false", "draft: true"
|
|
|
|
|
|
$modified = $true
|
|
|
|
|
|
Write-Host " Fixed 'draft: false' in $($file.Name)" -ForegroundColor Yellow
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# 2. Comment out status: public / status: "public"
|
|
|
|
|
|
if ($newContent -match "(?m)^status:\s*(`"public`"|public)") {
|
|
|
|
|
|
$newContent = $newContent -replace "(?m)(^status:\s*(`"public`"|public))", "# `$1 # Auto-hidden by script"
|
|
|
|
|
|
$modified = $true
|
|
|
|
|
|
Write-Host " Commented out 'status: public' in $($file.Name)" -ForegroundColor Yellow
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# 3. Ensure draft: true exists
|
|
|
|
|
|
if ($newContent -notmatch "(?m)^draft:\s*true") {
|
|
|
|
|
|
# Append after status: hidden
|
|
|
|
|
|
$newContent = $newContent -replace "(?m)(^status:\s*(`"hidden`"|hidden).*$)", "`$1`ndraft: true"
|
|
|
|
|
|
$modified = $true
|
|
|
|
|
|
Write-Host " Added 'draft: true' to $($file.Name)" -ForegroundColor Yellow
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($modified) {
|
|
|
|
|
|
# SAFETY CHECK: Ensure new content is not empty and size is reasonable
|
2026-01-20 23:57:39 +08:00
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 13:13:54 +08:00
|
|
|
|
# Save file using .NET method to ensure UTF8 without BOM
|
|
|
|
|
|
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
|
|
|
|
|
|
[System.IO.File]::WriteAllText($file.FullName, $newContent, $utf8NoBom)
|
2026-01-21 12:34:55 +08:00
|
|
|
|
Write-Host " -> Saved changes to $($file.Name)" -ForegroundColor Green
|
2026-01-20 23:57:39 +08:00
|
|
|
|
$count++
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Write-Host "Completed. Updated $count files." -ForegroundColor Cyan
|