content/s1/p1.md
content/s1/p1.md【免费下载链接】hugoThe world’s fastest framework for building websites.项目地址: https://gitcode.com/gh_mirrors/hu/hugodate: 2024-03-01 lastmod: 2024-03-02content/s1/p2.mddate: 2024-04-03 lastmod: 2024-04-04content/s1/p3.mdlastmod: 2099-05-06并在 layouts/list.html 中渲染 section 与 taxonomy 列表页的日期 go-html-template Date: {{ .Date.Format 2006-01-02 }} PublishDate: {{ .PublishDate.Format 2006-01-02 }} Lastmod: {{ .Lastmod.Format 2006-01-02 }}断言结果证明section 页、tag 列表页的Lastmod都聚合为三页中最新值2024-04-04p3的2099-05-06属于未来日期仅作为将来可能失效的边界用例存在Date聚合为最新的2024-04-03。当把 front matter 中的lastmod全部替换为publishDate后聚合结果随之变化PublishDate: 2024-04-04、Lastmod: 2024-04-03进一步印证了四个日期字段是独立聚合的。四、页面 Lastmod 的取值来源与解析顺序要真正理解.Site.Lastmod的最终值必须先弄清楚每个页面级 Lastmod 是怎么来的。它由 front matter 处理框架FrontMatterHandler负责解析完整逻辑在 resources/page/pagemeta/page_frontmatter.go。1. 默认解析顺序newDefaultFrontmatterConfigresources/page/pagemeta/page_frontmatter.go#L755-L762定义了四个日期字段的默认取值来源链func newDefaultFrontmatterConfig() FrontmatterConfig { return FrontmatterConfig{ Date: []string{fmDate, fmPubDate, fmLastmod}, Lastmod: []string{fmGitAuthorDate, fmLastmod, fmDate, fmPubDate}, PublishDate: []string{fmPubDate, fmDate}, ExpiryDate: []string{fmExpiryDate}, } }即Lastmod 默认按以下优先级取第一个可用的值:git—— 如果启用了 Git 集成取该文件最后一次提交的 Git AuthorDate作者提交日期lastmod—— front matter 中的lastmod字段别名modifieddate—— front matter 中的date字段publishdate—— front matter 中的publishdate字段别名pubdate、published。各来源标识符定义在 resources/page/pagemeta/page_frontmatter.go#L738-L752fmDate date // front matter 中的 date 字段 fmPubDate publishdate // front matter 中的 publishdate 字段 fmLastmod lastmod // front matter 中的 lastmod 字段 fmExpiryDate expirydate // front matter 中的 expirydate 字段 fmFilename :filename // 从文件名前缀解析日期如 2018-02-22-mypage.md fmModTime :filemodtime // 取文件操作系统修改时间 fmGitAuthorDate :git // 取 Git 最后一次提交的作者日期字段别名映射定义在 resources/page/pagemeta/page_frontmatter.go#L618-L623lastmod的别名是modifiedpublishdate的别名是pubdate与publishedexpirydate的别名是unpublishdate。2. 解析器如何工作createDateHandler会根据配置的标识符列表依次创建子处理器resources/page/pagemeta/page_frontmatter.go#L991-L1009并通过newChainedFrontMatterFieldHandler串联按顺序执行第一个成功取到非零时间的处理器即为最终值。例如newDateGitAuthorDateHandler:L1075-L1083读取FrontMatterDescriptor.GitAuthorDate若为零则跳过newDateFieldHandler:L1013-L1045从 front matter 取字段并调用htime.ToTimeInDefaultLocationE解析为时间解析失败会报错the lastmod front matter field is not a parsable datenewDateFilenameHandler:L1047-L1063与newDateModTimeHandler:L1065-L1073分别从文件名前缀与文件系统修改时间取值。GitAuthorDate在页面元数据初始化阶段注入hugolib/page__meta.go#L610-L613来源是页面携带的 Git 信息ps.gitInfo.AuthorDate。3. 自定义解析顺序frontmatter配置如果默认顺序不满足需求可在站点配置如hugo.toml中用frontmatter键自定义每个日期字段的来源顺序对应源码DecodeFrontMatterConfigresources/page/pagemeta/page_frontmatter.go#L764-L797[frontmatter] date [date, :filemodtime, :filename] lastmod [:git, lastmod, modified] publishDate [pubdate, date] expiryDate [expirydate, unpublishdate]其中:default占位符会被展开为对应的默认值列表expandDefaultValues:L811-L821。例如[frontmatter] lastmod [lastmod, :default]等价于lastmod [lastmod, :git, lastmod, date, publishdate]去重后。注意date字段别名modified也会被addDateFieldAliases自动展开:L799-L809。五、Git 集成让 Lastmod 自动跟随提交历史默认配置下 Lastmod 的第一优先来源是:git前提是启用 Git 信息集成。在hugo.toml中开启enableGitInfo true启用后Hugo 会为每个内容文件关联其最后一次提交的 Git 元数据哈希、提交说明、作者、作者日期等GitAuthorDate即取自该提交的作者日期。这意味着只要内容文件有新的提交无需手动维护 front matter站点的 Lastmod 就会自动更新——这也是很多文档站自动更新体验的来源。集成行为在 hugolib/gitinfo_github_test.go 中有端到端验证例如TestGitInfoFromGitModuleWithVersionQuery:L20-L65通过enableGitInfo true挂载带版本号的 Git 模块断言渲染页面能拿到对应提交的Hash、Subject、AuthorName等字段。测试同时表明 Git 信息集成对 Hugo Modules 挂载的内容同样生效分别验证了v3.0.0与v3.0.1两个版本挂载点的提交元数据。两点使用提示仅在 Git 仓库内构建才有效enableGitInfo需要 Hugo 能访问到.git元数据对模块挂载内容需要模块本身是 Git 仓库。与构建缓存的关系.Site.Lastmod依赖构建期的日期聚合而聚合结果在重建rebuild场景下同样会被重新计算相关逻辑在applyAggregates的rebuild分支中处理hugolib/content_map_page_assembler.go#L958-L972确保增量构建时首页/章节页的日期仍能正确刷新。六、实战组合按站点更新时间驱动页面逻辑将本文内容整合到真实站点可以这样做{{- $lastmod : .Site.Lastmod -}} {{- $today : now | time.Format 2006-01-02 -}} {{- $lastmodDay : $lastmod | time.Format 2006-01-02 -}} {{ if eq $today $lastmodDay }} span classbadge今日已更新/span {{ end }} time datetime{{ $lastmod.Format 2006-01-02T15:04:05Z07:00 }} {{ $lastmod | time.Format :date_long }} /time配合 RSS / sitemap / JSON-LD 输出时可使用 RFC 3339 格式让时间更规范{{ .Site.Lastmod.Format 2006-01-02T15:04:05Z07:00 }}【免费下载链接】hugoThe world’s fastest framework for building websites.项目地址: https://gitcode.com/gh_mirrors/hu/hugo创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考