From f6742ab9158051a9d5d0cf282447a9b91581aa55 Mon Sep 17 00:00:00 2001 From: Vaica <94612053+zqlit@users.noreply.github.com> Date: Fri, 23 Jan 2026 13:19:05 +0800 Subject: [PATCH] =?UTF-8?q?fixed=20=20=E6=96=87=E7=AB=A0=E9=A1=B5=20?= =?UTF-8?q?=E8=AF=84=E8=AE=BA=E5=8C=BAbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...new-a-ikun.md => 2024-08-07-new-a-ikun.md} | 0 ...-hugo.md => 2024-08-11-typecho-to-hugo.md} | 0 ...-cdn.md => 2024-09-16-auto-refresh-cdn.md} | 0 .../post/{junxun.md => 2024-09-21-junxun.md} | 0 ...ss.md => 2024-10-06-hugo-backup-alioss.md} | 0 ...ed-443.md => 2024-10-18-git-failed-443.md} | 0 ...md => 2024-10-18-some-useful-win-tools.md} | 0 ...rat-redis.md => 2024-11-10-strat-redis.md} | 0 ...bstorm.md => 2024-11-13-hello-webstorm.md} | 0 .../{除草日记.md => 2025-11-21-除草日记.md} | 0 ...通知勿扰.md => 2025-12-02-米环通知勿扰.md} | 0 ...hone16e.md => 2025-12-21-放弃iPhone16e.md} | 0 ...烦恼.md => 2025-12-24-脂溢性皮炎的烦恼.md} | 0 hugo.toml | 3 + new_post.ps1 | 149 +++++++++++------- themes/Ying/layouts/_default/single.html | 2 +- themes/Ying/layouts/partials/artalk.html | 2 +- 17 files changed, 94 insertions(+), 62 deletions(-) rename content/post/{new-a-ikun.md => 2024-08-07-new-a-ikun.md} (100%) rename content/post/{typecho-to-hugo.md => 2024-08-11-typecho-to-hugo.md} (100%) rename content/post/{auto-refresh-cdn.md => 2024-09-16-auto-refresh-cdn.md} (100%) rename content/post/{junxun.md => 2024-09-21-junxun.md} (100%) rename content/post/{hugo-backup-alioss.md => 2024-10-06-hugo-backup-alioss.md} (100%) rename content/post/{git-failed-443.md => 2024-10-18-git-failed-443.md} (100%) rename content/post/{some-useful-win-tools.md => 2024-10-18-some-useful-win-tools.md} (100%) rename content/post/{strat-redis.md => 2024-11-10-strat-redis.md} (100%) rename content/post/{hello-webstorm.md => 2024-11-13-hello-webstorm.md} (100%) rename content/post/{除草日记.md => 2025-11-21-除草日记.md} (100%) rename content/post/{米环通知勿扰.md => 2025-12-02-米环通知勿扰.md} (100%) rename content/post/{放弃iPhone16e.md => 2025-12-21-放弃iPhone16e.md} (100%) rename content/post/{脂溢性皮炎的烦恼.md => 2025-12-24-脂溢性皮炎的烦恼.md} (100%) diff --git a/content/post/new-a-ikun.md b/content/post/2024-08-07-new-a-ikun.md similarity index 100% rename from content/post/new-a-ikun.md rename to content/post/2024-08-07-new-a-ikun.md diff --git a/content/post/typecho-to-hugo.md b/content/post/2024-08-11-typecho-to-hugo.md similarity index 100% rename from content/post/typecho-to-hugo.md rename to content/post/2024-08-11-typecho-to-hugo.md diff --git a/content/post/auto-refresh-cdn.md b/content/post/2024-09-16-auto-refresh-cdn.md similarity index 100% rename from content/post/auto-refresh-cdn.md rename to content/post/2024-09-16-auto-refresh-cdn.md diff --git a/content/post/junxun.md b/content/post/2024-09-21-junxun.md similarity index 100% rename from content/post/junxun.md rename to content/post/2024-09-21-junxun.md diff --git a/content/post/hugo-backup-alioss.md b/content/post/2024-10-06-hugo-backup-alioss.md similarity index 100% rename from content/post/hugo-backup-alioss.md rename to content/post/2024-10-06-hugo-backup-alioss.md diff --git a/content/post/git-failed-443.md b/content/post/2024-10-18-git-failed-443.md similarity index 100% rename from content/post/git-failed-443.md rename to content/post/2024-10-18-git-failed-443.md diff --git a/content/post/some-useful-win-tools.md b/content/post/2024-10-18-some-useful-win-tools.md similarity index 100% rename from content/post/some-useful-win-tools.md rename to content/post/2024-10-18-some-useful-win-tools.md diff --git a/content/post/strat-redis.md b/content/post/2024-11-10-strat-redis.md similarity index 100% rename from content/post/strat-redis.md rename to content/post/2024-11-10-strat-redis.md diff --git a/content/post/hello-webstorm.md b/content/post/2024-11-13-hello-webstorm.md similarity index 100% rename from content/post/hello-webstorm.md rename to content/post/2024-11-13-hello-webstorm.md diff --git a/content/post/除草日记.md b/content/post/2025-11-21-除草日记.md similarity index 100% rename from content/post/除草日记.md rename to content/post/2025-11-21-除草日记.md diff --git a/content/post/米环通知勿扰.md b/content/post/2025-12-02-米环通知勿扰.md similarity index 100% rename from content/post/米环通知勿扰.md rename to content/post/2025-12-02-米环通知勿扰.md diff --git a/content/post/放弃iPhone16e.md b/content/post/2025-12-21-放弃iPhone16e.md similarity index 100% rename from content/post/放弃iPhone16e.md rename to content/post/2025-12-21-放弃iPhone16e.md diff --git a/content/post/脂溢性皮炎的烦恼.md b/content/post/2025-12-24-脂溢性皮炎的烦恼.md similarity index 100% rename from content/post/脂溢性皮炎的烦恼.md rename to content/post/2025-12-24-脂溢性皮炎的烦恼.md diff --git a/hugo.toml b/hugo.toml index d80004f6..24700d37 100644 --- a/hugo.toml +++ b/hugo.toml @@ -36,6 +36,9 @@ HasCJKLanguage = true post = "/:year:month:day" [params] + # 默认作者 + author = "小赵" + [params.style] # 自定义背景图开关 enableBackgroundImage = true diff --git a/new_post.ps1 b/new_post.ps1 index bd3c83a3..544e38c3 100644 --- a/new_post.ps1 +++ b/new_post.ps1 @@ -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") diff --git a/themes/Ying/layouts/_default/single.html b/themes/Ying/layouts/_default/single.html index 1a6d6a5a..49eeb3b9 100644 --- a/themes/Ying/layouts/_default/single.html +++ b/themes/Ying/layouts/_default/single.html @@ -5,7 +5,7 @@

{{ .Title }}

- 作者: 小赵同学 浏览: 字数: {{ .WordCount }} 阅读时间: {{ .ReadingTime }} 分钟 分类: + 作者: {{ .Params.author | default .Site.Params.author }} 浏览: 字数: {{ .WordCount }} 阅读: 约 {{ .ReadingTime }} 分钟 分类: {{ range .Params.categories }} {{ . }} {{ end }} diff --git a/themes/Ying/layouts/partials/artalk.html b/themes/Ying/layouts/partials/artalk.html index ab28744a..8e2e636a 100644 --- a/themes/Ying/layouts/partials/artalk.html +++ b/themes/Ying/layouts/partials/artalk.html @@ -17,7 +17,7 @@ } -
+