fix: 修复博客图片加载失败(掘金防盗链)

- 将《Git使用教学》中的掘金外链图片下载至 source/images/ 并替换为站内路径
- _config.yml 注入 no-referrer meta 作为兜底方案
- 新增 tools/download-juejin-images.js 批量下载/替换脚本
This commit is contained in:
2026-07-08 21:52:22 +08:00
parent 1d8ce5a16c
commit b3f44d235a
57 changed files with 1389 additions and 5 deletions
+61
View File
@@ -0,0 +1,61 @@
# 下载 Markdown 中的掘金外链图片到 source/images/,并替换链接为站内路径
param(
[string]$MdPath = "c:/Users/Administrator/Desktop/新建文件夹/blog/source/_posts/Git使用教学.md",
[string]$OutDir = "c:/Users/Administrator/Desktop/新建文件夹/blog/source/images"
)
$ErrorActionPreference = "Stop"
New-Item -ItemType Directory -Force -Path $OutDir | Out-Null
$content = Get-Content -Path $MdPath -Raw -Encoding UTF8
# 匹配 ![alt](url) 中的掘金图片地址
$pattern = '!\[[^\]]*\]\((https://p3-juejin\.byteimg\.com/[^)\s]+)\)'
$imgMatches = [regex]::Matches($content, $pattern)
$map = @{} # url -> /images/xxx.webp
$count = 0
$failed = @()
foreach ($m in $imgMatches) {
$url = $m.Groups[1].Value
if ($map.ContainsKey($url)) { continue }
# 用 URL 路径最后一段中 "~" 之前的部分作为文件名(即图片 hash)
$seg = ($url -split '/')[-1]
$hash = ($seg -split '~')[0]
if ([string]::IsNullOrEmpty($hash)) {
$failed += "无法解析文件名: $url"
continue
}
$file = $hash + '.webp'
$dest = Join-Path $OutDir $file
try {
curl.exe -s -L --max-time 30 -o $dest $url
if ((Test-Path $dest) -and ((Get-Item $dest).Length -gt 0)) {
$map[$url] = '/images/' + $file
$count++
Write-Host "[OK] $file <- $url"
} else {
$failed += "下载为空: $url"
}
} catch {
$failed += "下载异常: $url -> $_"
}
}
# 替换 Markdown 中的外链
foreach ($kv in $map.GetEnumerator()) {
$content = $content.Replace($kv.Key, $kv.Value)
}
# 写回(UTF-8 无 BOM
[System.IO.File]::WriteAllText($MdPath, $content, [System.Text.UTF8Encoding]::new($false))
Write-Host ""
Write-Host "完成:成功下载 $count 张,失败 $($failed.Count)"
if ($failed.Count -gt 0) {
Write-Host "失败列表:"
$failed | ForEach-Object { Write-Host " - $_" }
}