fixed 文章页 评论区bug
This commit is contained in:
1 parent
9620cbd257
commit
7fb936ba78
21 files changed
+94
-66
No files matched your search
+89
-60
@@ -1,75 +1,104 @@
|
||||
# 设置控制台编码为 UTF-8
|
||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
|
||||
# Set the path to your posts directory
|
||||
$postsPath = "content/post"
|
||||
Write-Host "==========================================" -ForegroundColor Cyan
|
||||
Write-Host " 📝 新建博客文章脚本 (Hugo) " -ForegroundColor Cyan
|
||||
Write-Host "==========================================" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
# Get all markdown files in the posts directory
|
||||
$files = Get-ChildItem -Path $postsPath -Filter "*.md"
|
||||
|
||||
# Initialize max ID
|
||||
$maxId = 0
|
||||
|
||||
# Loop through each file to find the maximum pid
|
||||
foreach ($file in $files) {
|
||||
$content = Get-Content $file.FullName -Raw -Encoding UTF8
|
||||
|
||||
# Check if the file has a pid field
|
||||
if ($content -match "(?m)^pid:\s*(\d+)") {
|
||||
$currentId = [int]$matches[1]
|
||||
if ($currentId -gt $maxId) {
|
||||
$maxId = $currentId
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Calculate the next ID
|
||||
$nextId = $maxId + 1
|
||||
Write-Host "Current max PID is $maxId. Next PID will be $nextId." -ForegroundColor Green
|
||||
|
||||
# Prompt user for the new post filename (slug)
|
||||
$slug = Read-Host "Enter the filename for the new post (e.g., my-new-post, without .md extension)"
|
||||
# 1. 获取文章标题
|
||||
do {
|
||||
$title = Read-Host "请输入文章标题 (必填)"
|
||||
} while ([string]::IsNullOrWhiteSpace($title))
|
||||
|
||||
# 2. 获取文件名 Slug (默认为标题)
|
||||
$slug = Read-Host "请输入文件名/Slug (选填,留空默认使用标题,建议使用英文)"
|
||||
if ([string]::IsNullOrWhiteSpace($slug)) {
|
||||
Write-Error "Filename cannot be empty."
|
||||
exit
|
||||
$slug = $title
|
||||
}
|
||||
|
||||
# Ensure the filename ends with .md (optional, hugo new handles it usually but let's be safe)
|
||||
if (-not $slug.EndsWith(".md")) {
|
||||
$filename = "$slug.md"
|
||||
# 3. 获取分类
|
||||
$categoriesInput = Read-Host "请输入分类 (选填,多个分类用空格或逗号分隔)"
|
||||
if (-not [string]::IsNullOrWhiteSpace($categoriesInput)) {
|
||||
# 处理分隔符:将中文逗号、英文逗号替换为空格,然后分割
|
||||
$categories = $categoriesInput -replace ",", " " -replace ",", " " -split "\s+"
|
||||
# 重新组合成 JSON 数组格式字符串 ["A", "B"]
|
||||
$categoriesStr = '["' + ($categories -join '", "') + '"]'
|
||||
} else {
|
||||
$filename = $slug
|
||||
$categoriesStr = '["生活"]' # 默认分类
|
||||
}
|
||||
|
||||
# Create the new post using Hugo
|
||||
Write-Host "Creating new post: content/post/$filename"
|
||||
hugo new "content/post/$filename"
|
||||
# 4. 获取标签
|
||||
$tagsInput = Read-Host "请输入标签 (选填,多个标签用空格或逗号分隔)"
|
||||
if (-not [string]::IsNullOrWhiteSpace($tagsInput)) {
|
||||
$tags = $tagsInput -replace ",", " " -replace ",", " " -split "\s+"
|
||||
$tagsStr = '["' + ($tags -join '", "') + '"]'
|
||||
} else {
|
||||
$tagsStr = '[]'
|
||||
}
|
||||
|
||||
# Path to the new file
|
||||
$newFilePath = Join-Path $postsPath $filename
|
||||
# 5. 获取作者
|
||||
$author = Read-Host "请输入作者 (选填,默认为 '小赵同学')"
|
||||
if ([string]::IsNullOrWhiteSpace($author)) {
|
||||
$author = "小赵同学"
|
||||
}
|
||||
|
||||
# Check if file was created successfully
|
||||
if (Test-Path $newFilePath) {
|
||||
# Read the new file content
|
||||
$newFileContent = Get-Content $newFilePath -Raw -Encoding UTF8
|
||||
|
||||
# Add the pid field to the front matter
|
||||
# Assuming front matter ends with --- or similar.
|
||||
# We'll just insert it after the date or title, or at the end of the front matter.
|
||||
|
||||
# Simple replacement to add pid after date
|
||||
if ($newFileContent -match "(?m)^date:.*$") {
|
||||
$newFileContent = $newFileContent -replace "(?m)(^date:.*$)", "`$1`npid: $nextId"
|
||||
} else {
|
||||
# Fallback: add to the end of the first YAML block if date not found
|
||||
if ($newFileContent -match "^---[\s\S]*?---") {
|
||||
$newFileContent = $newFileContent -replace "(?m)(^---[\s\S]*?)(\r?\n---)", "`$1`npid: $nextId`$2"
|
||||
}
|
||||
# 6. 生成文件名 (YYYY-MM-DD-Slug.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+', '-'
|
||||
$fileName = "$dateStr-$safeSlug.md"
|
||||
$filePath = Join-Path "content/post" $fileName
|
||||
|
||||
# 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
|
||||
}
|
||||
|
||||
# Save the updated content back to the file
|
||||
Set-Content -Path $newFilePath -Value $newFileContent -Encoding UTF8
|
||||
# 检查文件是否已存在
|
||||
if (Test-Path $filePath) {
|
||||
Write-Host "⚠️ 文件已存在: $fileName" -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 "Successfully added 'pid: $nextId' to $newFilePath" -ForegroundColor Cyan
|
||||
} else {
|
||||
Write-Error "Failed to create the new post file."
|
||||
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")
|
||||
Reference in new issue
Block a user