适配Hugo新版本(0.58)的三个改动(.Hugo、.RSSLink与.RegularPages)

开发迭代,版本更新,接口替换,这是常见的事。 [Hugo]在[v0.58.0]及之前的v0.55.0,做出了较大调整。 本站的模板多年未动,也仍然受到影响,甚至一度发生首页空白的事件。

如果不愿意适配,完全可以停留在旧版本。 但孤当然要跟随潮流,亦步亦趋,至少徐徐前进。 程序员这职业,容不得你原地踏步。

这里记录一下相关的三个改动。

适配废弃的.Hugo

v0.55.0后,多出以下警告。

Page's .Hugo is deprecated and will be removed in a future release. Use the global hugo function.

受影响的主要是.Hugo.Generator.Hugo.Version。 参考[[#132]],做出以下修改。

 <head>
     <link href="http://gmpg.org/xfn/11" rel="profile">
     <meta http-equiv="content-type" content="text/html; charset=utf-8">
-    {{ .Hugo.Generator }}
+    <meta name="generator" content="Hugo {{ hugo.Version }}">

     <!-- Enable responsiveness on mobile devices-->
     <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
             style="margin-left: auto; margin-right:auto; display:block; border-width:0"
             src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" />
         </a>
-        <p>Powered by <a href="https://gohugo.io/">Hugo</a> {{ .Hugo.Version }}</p>
+        <p>Powered by <a href="https://gohugo.io/">Hugo</a> {{ hugo.Version }}</p>
     </div>
 </div>

v0.55.0后,多出以下警告。

Page's .RSSLink is deprecated and will be removed in a future release

.RSSLink将不再能用,而需要使用更通用的形式{{ with .OutputFormats.Get "RSS" }}{{ .RelPermalink }}{{ end }}。 参考#4427,做出以下修改。

     <link rel="bookmark"href="/note.svg" />

     <!-- RSS -->
-    <link href="{{ .RSSLink }}" rel="alternate" type="application/rss+xml" title="{{ .Site.Title }}" />
+    <link href="{{ with .OutputFormats.Get "RSS" }}{{ .RelPermalink }}{{ end }}" rel="alternate" type="application/rss+xml" title="{{ .Site.Title }}" />

     {{ if not .Site.IsServer }}
     {{ partial "baidu.html" . }}

适配修改的.Pages

v0.57.0版本开始,.Pages发生变化,尤其是首页的home.Pages深受影响。

home.Pages now only returns pages in the top level section. Before this release, it included all regular pages in the site. This made it easy to list all the pages on home page, but it also meant that you needed to take special care if you wanted to navigate the page tree from top to bottom. If you need all regular pages, use .Site.RegularPages. Also see #6153.

.Pages now include sections. We have added .RegularPages as a convenience method if you want the old behaviour. See #6154 for details.

如果不愿意使用新.Pages的功能,可以用.RegularPages,全面替换旧的.Pages。 此外,如果仍然希望列出所有页面,可以使用.Site.RegularPages

         {{ partial "sidebar.html" . }}
         <div class="content container">
             <div class="posts">
-                {{ range .Data.Pages }}
+                {{ range .Site.RegularPages }}
                 {{ if .Params.tags }}
                 {{ partial "page-description" . }}
                 {{ end }}

相关笔记