Hugo博客添加GitHub风格的Alert块引用
让 Hugo 支持 GitHub 风格的 Alert 块引用。
前言
在一篇博客 中看到 Hugo
新版本支持通过标记(Markups)的 Hook 模板来渲染 Alert
风格的块引用,因此参考那位博主的代码和 ChatGPT
直接在博客上尝试。
配置
重要
需要
Hugo
版本在0.132
以上
新建 ~/layouts/_default/_markup/render-blockquote-alert.html
,写入模板内容:
1{{ $alertTypes := dict
2 "note" "<path d=\"M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8Zm8-6.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13ZM6.5 7.75A.75.75 0 0 1 7.25 7h1a.75.75 0 0 1 .75.75v2.75h.25a.75.75 0 0 1 0 1.5h-2a.75.75 0 0 1 0-1.5h.25v-2h-.25a.75.75 0 0 1-.75-.75ZM8 6a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z\"></path>"
3 "tip" "<path d=\"M8 1.5c-2.363 0-4 1.69-4 3.75 0 .984.424 1.625.984 2.304l.214.253c.223.264.47.556.673.848.284.411.537.896.621 1.49a.75.75 0 0 1-1.484.211c-.04-.282-.163-.547-.37-.847a8.456 8.456 0 0 0-.542-.68c-.084-.1-.173-.205-.268-.32C3.201 7.75 2.5 6.766 2.5 5.25 2.5 2.31 4.863 0 8 0s5.5 2.31 5.5 5.25c0 1.516-.701 2.5-1.328 3.259-.095.115-.184.22-.268.319-.207.245-.383.453-.541.681-.208.3-.33.565-.37.847a.751.751 0 0 1-1.485-.212c.084-.593.337-1.078.621-1.489.203-.292.45-.584.673-.848.075-.088.147-.173.213-.253.561-.679.985-1.32.985-2.304 0-2.06-1.637-3.75-4-3.75ZM5.75 12h4.5a.75.75 0 0 1 0 1.5h-4.5a.75.75 0 0 1 0-1.5ZM6 15.25a.75.75 0 0 1 .75-.75h2.5a.75.75 0 0 1 0 1.5h-2.5a.75.75 0 0 1-.75-.75Z\"></path>"
4 "important" "<path d=\"M0 1.75C0 .784.784 0 1.75 0h12.5C15.216 0 16 .784 16 1.75v9.5A1.75 1.75 0 0 1 14.25 13H8.06l-2.573 2.573A1.458 1.458 0 0 1 3 14.543V13H1.75A1.75 1.75 0 0 1 0 11.25Zm1.75-.25a.25.25 0 0 0-.25.25v9.5c0 .138.112.25.25.25h2a.75.75 0 0 1 .75.75v2.19l2.72-2.72a.749.749 0 0 1 .53-.22h6.5a.25.25 0 0 0 .25-.25v-9.5a.25.25 0 0 0-.25-.25Zm7 2.25v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 9a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"></path>"
5 "warning" "<path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"></path>"
6 "caution" "<path d=\"M4.47.22A.749.749 0 0 1 5 0h6c.199 0 .389.079.53.22l4.25 4.25c.141.14.22.331.22.53v6a.749.749 0 0 1-.22.53l-4.25 4.25A.749.749 0 0 1 11 16H5a.749.749 0 0 1-.53-.22L.22 11.53A.749.749 0 0 1 0 11V5c0-.199.079-.389.22-.53Zm.84 1.28L1.5 5.31v5.38l3.81 3.81h5.38l3.81-3.81V5.31L10.69 1.5ZM8 4a.75.75 0 0 1 .75.75v3.5a.75.75 0 0 1-1.5 0v-3.5A.75.75 0 0 1 8 4Zm0 8a1 1 0 1 1 0-2 1 1 0 0 1 0 2Z\"></path>"
7}}
8
9
10{{ if eq .Type "alert" }}
11 <blockquote class="alert-blockquote alert-{{ .AlertType }}">
12 <p class="alert-heading">
13 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16">
14 {{ index $alertTypes .AlertType | safeHTML }}
15 </svg>
16 <span>{{ or (i18n .AlertType) (title .AlertType) }}</span>
17 </p>
18 {{ .Text | safeHTML }}
19 </blockquote>
20{{ else }}
21 <blockquote>
22 {{ .Text | safeHTML }}
23 </blockquote>
24{{ end }}
然后可以在 custom.scss
中添加样式代码,可以直接写到模板中用 <style>
和 </style>
包裹:
1<style>
2.alert-blockquote {
3 --title-color: #fff;
4 --content-color: inherit;
5 padding: 18px;
6 line-height: 24px;
7 margin: 1rem 0;
8 border-radius: 4px;
9 color: var(--content-color);
10 border-left: none !important;
11}
12
13.alert-blockquote * {
14 color: var(--content-color) !important;
15}
16
17.alert-blockquote .alert-heading {
18 margin: -18px -18px 12px;
19 padding: 4px 18px;
20 border-radius: 4px 4px 0 0;
21 font-weight: 600;
22 color: var(--title-color) !important;
23 display: flex;
24 align-items: center;
25}
26
27.alert-blockquote .alert-heading svg {
28 width: 1em !important;
29 height: 1em !important;
30 margin-right: 0.5rem !important;
31 fill: currentColor !important;
32}
33
34.alert-blockquote p:last-child {
35 margin-bottom: 0;
36}
37
38/* Light theme */
39.alert-blockquote.alert-note {
40 --title-background-color: #166dd0;
41 --content-background-color: #e7f2fa;
42}
43
44.alert-blockquote.alert-tip {
45 --title-background-color: #1a7f37;
46 --content-background-color: #efe;
47}
48
49.alert-blockquote.alert-important {
50 --title-background-color: #8250df;
51 --content-background-color: #f5f0ff;
52}
53
54.alert-blockquote.alert-warning {
55 --title-background-color: #9a6700;
56 --content-background-color: #fff8c5;
57}
58
59.alert-blockquote.alert-caution {
60 --title-background-color: #cf222e;
61 --content-background-color: #ffebe9;
62}
63
64/* Dark theme */
65body.night .alert-blockquote {
66 --content-color: #d0d7dd;
67}
68
69body.night .alert-blockquote.alert-note {
70 --title-background-color: #58a6ff;
71 --content-background-color: #0d1d30;
72}
73
74body.night .alert-blockquote.alert-tip {
75 --title-background-color: #3fb950;
76 --content-background-color: #0f2a1b;
77}
78
79body.night .alert-blockquote.alert-important {
80 --title-background-color: #a371f7;
81 --content-background-color: #2a1d3f;
82}
83
84body.night .alert-blockquote.alert-warning {
85 --title-background-color: #d29922;
86 --content-background-color: #3b2300;
87}
88
89body.night .alert-blockquote.alert-caution {
90 --title-background-color: #f85149;
91 --content-background-color: #3d0c0c;
92}
93
94.alert-blockquote .alert-heading {
95 background: var(--title-background-color);
96}
97
98.alert-blockquote {
99 background: var(--content-background-color);
100}
101</style>
我的样式是根据 Hugo-notice
的样式而来,如果想使用 GitHub
风格,可以替换样式代码:
1.alert-blockquote {
2 all: revert !important;
3 border-left-width: 4px !important;
4 border-left-style: solid !important;
5 padding: 0.25rem 1rem !important;
6 margin: 1rem 0 !important;
7 color: inherit !important;
8}
9.alert-blockquote * {
10 color: inherit !important;
11}
12.alert-blockquote .alert-heading {
13 display: flex !important;
14 align-items: center !important;
15 font-weight: 600 !important;
16 margin-bottom: 0.5rem !important;
17}
18.alert-blockquote.alert-note {
19 border-left-color: #0969da !important;
20}
21.alert-blockquote.alert-note .alert-heading {
22 color: #0969da !important;
23}
24.alert-blockquote.alert-tip {
25 border-left-color: #1a7f37 !important;
26}
27.alert-blockquote.alert-tip .alert-heading {
28 color: #1a7f37 !important;
29}
30.alert-blockquote.alert-important {
31 border-left-color: #8250df !important;
32}
33.alert-blockquote.alert-important .alert-heading {
34 color: #8250df !important;
35}
36.alert-blockquote.alert-warning {
37 border-left-color: #9a6700 !important;
38}
39.alert-blockquote.alert-warning .alert-heading {
40 color: #9a6700 !important;
41}
42.alert-blockquote.alert-caution {
43 border-left-color: #cf222e !important;
44}
45.alert-blockquote.alert-caution .alert-heading {
46 color: #cf222e !important;
47}
48.alert-blockquote .alert-heading svg {
49 width: 1rem !important;
50 height: 1rem !important;
51 margin-right: 0.5rem !important;
52 fill: currentColor !important;
53}
54/* 暗黑模式适配 */
55body.night .alert-blockquote {
56 color: #c9d1d9 !important;
57}
58body.night .alert-blockquote * {
59 color: #c9d1d9 !important;
60}
61body.night .alert-blockquote.alert-note .alert-heading {
62 color: #58a6ff !important;
63}
64body.night .alert-blockquote.alert-tip .alert-heading {
65 color: #3fb950 !important;
66}
67body.night .alert-blockquote.alert-important .alert-heading {
68 color: #a371f7 !important;
69}
70body.night .alert-blockquote.alert-warning .alert-heading {
71 color: #d29922 !important;
72}
73body.night .alert-blockquote.alert-caution .alert-heading {
74 color: #f85149 !important;
75}
76body.night .alert-blockquote .alert-heading svg {
77 fill: currentColor !important;
78}
注释
由于我的主题引入了
Bootstrap
的CSS
,导致配置样式被覆盖,因此我都使用了!important
来确保样式生效。
模板中使用了 i18n
来生成标题,所以需要在 ~/i18n/
下的文件中配置名称,我的中文文件参考如下:
1- id: caution
2 translation: '危险'
3- id: important
4 translation: '重要'
5- id: note
6 translation: '注释'
7- id: tip
8 translation: '提示'
9- id: warning
10 translation: '警告'
这个相比短代码的好处是 obsidian
很多主题都带有这种 Alert
引用,可以在 obsidian
直接显示样式,而短代码不行。
参考
最后修改于 2024-08-25