Skip to content

Commit 8c92cde

Browse files
committed
📝 Add neodb shortcode update post
1 parent 00c88ee commit 8c92cde

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
draft: false
3+
title: NeoDB shortcode 更新
4+
date: 2025-01-08
5+
categories: Tech
6+
comments: true
7+
isCJKLanguage: true
8+
---
9+
10+
突然的,更新了一下本地的 hugo,从 0.12x 一下子更新到了 0.140。
11+
然后本地启动博客 `hugo server` 出现了一堆报错。
12+
13+
Don't Panic❗️
14+
15+
按照报错一条一条解决一下就好啦。主要就是一些 function deprecated,一般会提示用什么替代。
16+
然后发现最大需要改动的是 NeoDB shortcode。
17+
18+
原来广为流传的版本是这个
19+
20+
```html
21+
{{ $dbUrl := .Get 0 }}
22+
{{ $dbApiUrl := "https://neodb.social/api/" }}
23+
{{ $dbType := "" }}
24+
25+
{{ if ( findRE `^.*neodb\.social\/.*` $dbUrl ) }}
26+
{{ $dbType = replaceRE `.*neodb.social\/(.*\/.*)` "$1" $dbUrl }}
27+
{{ else }}
28+
{{ $dbType = $dbUrl }}
29+
{{ $dbApiUrl = "https://neodb.social/api/catalog/fetch?url=" }}
30+
{{ end }}
31+
32+
{{ $dbFetch := getJSON $dbApiUrl $dbType }}
33+
34+
...后面省略
35+
```
36+
37+
其中很重要的用来获取数据的 [`getJSON` Deprecated in v0.123.0](https://gohugo.io/functions/data/getjson/),不能用了。按照文档里写的需要用 `resources.GetRemote`
38+
39+
按照文档里的例子,我改成了这个,亲测可用。
40+
41+
```html
42+
{{ $dbUrl := .Get 0 }}
43+
{{ $dbApiUrl := "https://neodb.social/api/" }}
44+
{{ $dbType := "" }}
45+
46+
{{ if ( findRE `^.*neodb\.social\/.*` $dbUrl ) }}
47+
{{ $dbType = replaceRE `.*neodb.social\/(.*\/.*)` "$1" $dbUrl }}
48+
{{ else }}
49+
{{ $dbType = $dbUrl }}
50+
{{ $dbApiUrl = "https://neodb.social/api/catalog/fetch?url=" }}
51+
{{ end }}
52+
53+
{{ $url := printf "%s%s" $dbApiUrl $dbType }}
54+
{{ $data := dict }}
55+
56+
{{ with resources.GetRemote $url }}
57+
{{ with .Err }}
58+
{{ errorf "%s" . }}
59+
{{ else }}
60+
{{ $data = . | transform.Unmarshal }}
61+
{{ if $data }}
62+
{{ $itemRating := 0 }}{{ with $data.rating }}{{ $itemRating = . }}{{ end }}
63+
<div class="db-card">
64+
<div class="db-card-subject">
65+
<div class="db-card-post"><img loading="lazy" decoding="async" referrerpolicy="no-referrer" src="{{ $data.cover_image_url }}"></div>
66+
<div class="db-card-content">
67+
<div class="db-card-title"><a href="{{ $dbUrl }}" class="cute" target="_blank" rel="noreferrer">{{ $data.title }}</a></div>
68+
<div class="rating"><span class="allstardark"><span class="allstarlight" style="width: {{ mul 10 $itemRating }}%"></span></span><span class="rating_nums">{{ $itemRating }}</span></div>
69+
<div class="db-card-abstract">{{ $data.brief }}</div>
70+
</div>
71+
<div class="db-card-cate">{{ $data.category }}</div>
72+
</div>
73+
</div>
74+
{{ end }}
75+
{{ end }}
76+
{{ else }}
77+
<p style="text-align: center;"><small>远程获取内容失败,请检查 API 有效性。</small></p>
78+
{{ errorf "Unable to get remote resource %q" $url }}
79+
{{ end }}
80+
81+
```
82+
83+
84+
效果和之前一样
85+
86+
{{< neodb "https://neodb.social/tv/season/5XNZfs5lya1GSt5zdRjhzx" >}}

0 commit comments

Comments
 (0)