Mid of Nowhere

Welcome to the middle of nowhere. That's right, absolute nowhere.

更新 Hugo 版本后所需修改

从 0.128.0 更新到了 0.144.0 和 0.152.0 后的紧急 debug!

我因为最近在电脑上装了新的操作系统,所以重新下载了一遍 Hugo,更新了 hugo 版本到 0.144.0,结果一跑就挂了。一搜索,就找到了这个版本更新通告: https://discourse.gohugo.io/t/more-about-the-v0-144-0-release/53584

通告里第一条就提到了这个 permalink token。

因为 token 规则改变了,所以现在当你想要新建一个文件,你输入 hugo new “posts/test-content.md" 就会收到

WARN  deprecated: the ":filename" permalink token was deprecated in Hugo 0.144.0 and will be removed in a future release. Use ":contentbasename" instead.

收到这条 warning 之后请走到你的网站文件夹的根目录下,找到 config 文件(我的是 config.toml),里面有一段:

[permalinks]
    posts = "/:year/:filename/"
    status = "/posts/:year/:filename/"

把这里的 ":filename" 都换成 ":contentbasename",现在就是:

    posts = "/:year/:contentbasename/"
    status = "/posts/:year/:contentbasename/"

更新通告说这样以后可以有更多很棒棒的用法,但是我还啥都没用上,反正先改了再说,不然以后跑不起来。

LastChange deprecated

第二点是从 v 0.123.0 就更新的 Lastmod 功能: https://gohugo.io/methods/site/lastmod/

如果你在试图运行 hugo 的时候收到了这个提示:

ERROR deprecated: .Site.LastChange was deprecated in Hugo v0.123.0 and subsequently removed. Use .Site.Lastmod instead.

意思就是你用了过时的说法 "LastChange"。这个通常在页脚版权声明里会用到,我的就在 '/layouts/partials/aboutme.html' 里面有这么一行,只要把 LastChange 改成 Lastmod 就行了。注意⚠️ "Lastmod" 这个中间的m是小写的 我也不知道为什么这次是小写的上次是大写的 随便他们吧。

改完之后是这样:

<p style="font-weight: bold;">All rights reserved {{ site.Params.Since }} - {{ site.Lastmod.Format "2006" }}</p>

NeoDB 短代码

如果你跟我一样抄了忘记是哪个好心人写的 neodb 短代码,那么里面有一行 {{ $dbFetch := getJSON $dbApiUrl $dbType }} 这个现在会报错,因为:

ERROR deprecated: data.GetJSON was deprecated in Hugo v0.123.0 and subsequently removed. use resources.Get or resources.GetRemote with transform.Unmarshal.

所以我们来抄另一位热心网友

现在的 neodb.html 是这样:

{{ $dbUrl := .Get 0 }}
{{ $dbApiUrl := "" }}

{{ if (findRE `^.*neodb\.social\/.*` $dbUrl) }}
  {{ $dbPath := replaceRE `.*neodb.social\/(.*\/.*)` "$1" $dbUrl }}
  {{ $dbApiUrl = print "https://neodb.social/api/" $dbPath }}
{{ else }}
  {{ $dbApiUrl = print "https://neodb.social/api/catalog/fetch?url=" $dbUrl }}
{{ end }}

{{ $result := try (resources.GetRemote $dbApiUrl) }}

{{ with $result.Err }}
  {{ if hugo.IsServer }}
    {{ warnf "Failed to fetch NeoDB content: %s (%s)" . $dbApiUrl }}
    <p style="text-align:center;"><small>远程获取内容失败,请检查 NeoDB 是否可达。</small></p>
  {{ else }}
    {{ errorf "Failed to fetch NeoDB content: %s (%s)" . $dbApiUrl }}
  {{ end }}
{{ else }}
  {{ with $result.Value }}
    {{ $dbFetch := transform.Unmarshal . }}
    {{ $itemRating := 0 }}
    <div class="db-card">
        <div class="db-card-subject">
            <div class="db-card-post"><img loading="lazy" decoding="async" referrerpolicy="no-referrer" src="{{ $dbFetch.cover_image_url }}"></div>
            <div class="db-card-content">
                <div class="db-card-title"><a href="{{ $dbUrl }}" class="cute" target="_blank" rel="noreferrer">{{ $dbFetch.title }}</a></div>
                <div class="db-card-abstract">{{ $dbFetch.brief }}</div>
            </div>
            <div class="db-card-cate">{{ $dbFetch.category }}</div>
        </div>
    </div>
{{ end }}
{{ end }}

网友的 neodb 卡片显示了评分,我没有,所以把评分那一段删掉了。然后保险起见,在 config.toml 里加一段白名单:

[security]
  [security.http]
    methods = ["GET"]
    urls = ["https://neodb.social/api/" , ]

好了现在 neodb 又好了。

修改部署服务的 hugo 版本

最后,为了让本地运行和最终部署的效果一样,需要修改部署服务商上面的环境变量。我用的是 vercel,在项目面板的设置里,搜索 environment variable, 修改或者添加 HUGO_VERSION ,然后 value 就写本地运行的版本就行了,我的是 0.144.0。点击保存。Vercel 会问你要不要立刻重新部署一次,选好的,然后等 vercel 下载指定版本的 hugo 再重新部署就好了。


Comments

This work is licensed under CC BY-NC-ND 4.0

Powered by hugo. Theme adapted from no style please.