叶寻的博客

Hugo:给文章添加 lastmod(上次修改时间)

叶寻

背景:本站文章的 lastmod(上次修改时间)就是 Git 提交的 author date(作者日期)。我需要批量修改文章(Hugo:补全文章的 ISO 8601 日期),这会导致所有文章的 lastmod 都变成今天。文章内容没变就不应该改 lastmod,所以我打算给每篇文章都加上 lastmod,后面改动文章就 lastmod 就不会变。


先把 lastmod 的优先级调至最高,不然 Hugo 会继续使用 Git 提交日期。

1
2
3
# config.toml
[frontmatter]
  lastmod = ['lastmod', ':default']

在根目录新建 add-lastmod.sh,添加执行权限

1
2
touch add-lastmod.sh
chmod +x add-lastmod.sh

add-lastmod.sh 填入脚本内容。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/usr/bin/env bash
# usage: ./add-lastmod.sh directory-name
directory="$1"
files=$(find "$directory" -type f)
for file in $files; do
  echo "${file}"
  lastmod_date=$(git log --no-show-signature -1 --format=%aI "$file") # example: 2024-05-16T14:23:53+08:00
  echo "$lastmod_date"
  # Use awk to insert the lastmod line above the second ---
  awk -v lastmod="lastmod: $lastmod_date # remove this line if the content is actually changed" '
  BEGIN { frontmatter = 0 }
  /^---$/ { frontmatter++ }
  frontmatter == 2 && !printed { print lastmod; printed = 1 }
  { print }
  ' "$file" >tmpfile && mv tmpfile "$file"
done

修改前先构建网站,后面用于对比。

1
2
3
rm --recursive public/
hugo
mv public public-original

添加 lastmod。

1
./add-lastmod.sh content

再次构建网站。

1
2
hugo
mv public public-changed

使用文件对比工具(我用的是 Kompare)对比 public-originalpublic-changed,结果完全相同。

预览,稍微和 https://cyrusyip.org/ 对比一下,看起来一样。

1
hugo server

删除构建的网站。

1
rm --recursive public public-original public-changed

查看差异后提交改动。

1
2
3
git diff
git add --update
git commit --message 'chore: add lastmod before changing date'

删除脚本。

1
rm add-lastmod.sh
标签:
comments powered by Disqus