1.先把markdown文章放在桌面,里面的图片正确引用
2.在桌面创建文档HugoImageTransfer.ps1
3.把以下代码粘贴在里面
<#
Hugo 图片批量迁移脚本:提取原Markdown图片→保存到static/images→替换引用路径
#>
# -------------------------- 你需要改的参数(只改这3行!)--------------------------
$originalMdPath = "C:\Users\你的用户名\Desktop\原文章.md" # 你的原Markdown文件路径(比如桌面的“原文章.md”)
$hugoPostTitle = "2025-10-25-my-new-post" # 新建的Hugo文章文件名(不用加.md,会自动加)
$hugoProjectPath = "D:\personnal_blog\paper-notes" # 你的Hugo项目根目录(之前的paper-notes)
# --------------------------------------------------------------------------------
# 1. 新建Hugo文章
$hugoPostPath = Join-Path $hugoProjectPath "content\posts\$hugoPostTitle.md"
hugo new "posts\$hugoPostTitle.md" --source $hugoProjectPath | Out-Null
Write-Host "✅ 已新建Hugo文章:$hugoPostPath"
# 2. 确保static/images文件夹存在
$imageSaveDir = Join-Path $hugoProjectPath "static\images"
if (-not (Test-Path $imageSaveDir)) {
New-Item -ItemType Directory -Path $imageSaveDir | Out-Null
Write-Host "✅ 已创建图片保存目录:$imageSaveDir"
}
# 3. 读取原Markdown内容
$mdContent = Get-Content -Path $originalMdPath -Raw
Write-Host "✅ 已读取原Markdown文件"
# 4. 处理Base64图片(剪切板截图常见形式)
$base64Pattern = '!\[(.*?)\]\(data:image/(png|jpg|jpeg);base64,(.*?)\)'
$mdContent = [regex]::Replace($mdContent, $base64Pattern, {
param($match)
$imgDesc = $match.Groups[1].Value
$imgExt = $match.Groups[2].Value
$base64Data = $match.Groups[3].Value
# 生成唯一图片名(时间戳+随机数,避免重复)
$imgName = "img_$(Get-Date -Format 'yyyyMMddHHmmss')_$(Get-Random -Max 1000).$imgExt"
$imgSavePath = Join-Path $imageSaveDir $imgName
# Base64解码并保存为图片文件
$base64Bytes = [Convert]::FromBase64String($base64Data)
[IO.File]::WriteAllBytes($imgSavePath, $base64Bytes)
Write-Host "✅ 已保存Base64图片:$imgName"
# 返回新的引用路径(Hugo绝对路径)
return ""
}, [System.Text.RegularExpressions.RegexOptions]::Singleline)
# 5. 处理本地临时路径图片(如C:\Temp\XXX.png)
$localImgPattern = '!\[(.*?)\]\((C:\\Users\\.*?\.(png|jpg|jpeg|gif))\)'
$mdContent = [regex]::Replace($mdContent, $localImgPattern, {
param($match)
$imgDesc = $match.Groups[1].Value
$localImgPath = $match.Groups[2].Value
$imgExt = $match.Groups[3].Value
# 生成唯一图片名
$imgName = "img_$(Get-Date -Format 'yyyyMMddHHmmss')_$(Get-Random -Max 1000).$imgExt"
$imgSavePath = Join-Path $imageSaveDir $imgName
# 复制本地图片到static/images
Copy-Item -Path $localImgPath -Destination $imgSavePath -Force
Write-Host "✅ 已复制本地图片:$imgName"
# 返回新的引用路径
return ""
}, [System.Text.RegularExpressions.RegexOptions]::Singleline)
# 6. 把处理后的内容写入Hugo新文章(覆盖默认的Front Matter之后的内容)
$frontMatter = Get-Content -Path $hugoPostPath -Raw | Select-String -Pattern '---[\s\S]*?---' | ForEach-Object { $_.Matches.Value }
$finalContent = "$frontMatter`n`n$mdContent"
Set-Content -Path $hugoPostPath -Value $finalContent -Encoding UTF8
Write-Host "`n🎉 全部完成!Hugo文章路径:$hugoPostPath"
可以看到只用改三行代码
$originalMdPath = "C:\Users\你的用户名\Desktop\原文章.md" # 你的原Markdown文件路径(比如桌面的“原文章.md”)
$hugoPostTitle = "2025-10-25-my-new-post" # 新建的Hugo文章文件名(不用加.md,会自动加)
$hugoProjectPath = "D:\personnal_blog\paper-notes" # 你的Hugo项目根目录(之前的paper-notes)
第一行代码直接选中要上传的markdown文章Ctrl+shift+C,再Ctrl+V
第二行填写文章标题,一般都和原来的文章题目一样
第三行正常情况下不用改
4.打开powershell
cd d:
cd 桌面
先进入桌面文件夹
再执行文件
.\HugoImageTransfer.ps1
看到类似输出就稳了
✅ 已新建Hugo文章:D:\personnal_blog\paper-notes\content\posts\2025-10-25-math-notes.md
✅ 已读取原Markdown文件
✅ 已保存Base64图片:img_20251025153000_123.png
✅ 已复制本地图片:img_20251025153005_456.png
🎉 全部完成!Hugo文章路径:D:\personnal_blog\paper-notes\content\posts\2025-10-25-math-notes.md