自定义Hugo Next主题

最近从 Hexo 切换到 Hugo,通过对 Hugo Next 主题的深入研究,我对其进行了若干修改,以更好地满足个人需求。本文将记录这些改动。

主题颜色的改动

修改基本配色

1
2
3
4
5
# themes/hugo-theme-next/assets/css/_variables/base.scss
// Color system
// --------------------------------------------------
// $black-deep   : #222;
$black-deep   : #9400D3; // 深紫色

修改配置文件

根目录hugo.yaml第775行,修改为

1
2
3
4
themeColor:
# Violet color
    light: "#9400D3"
    dark: "#9400D3"

标题的隐藏

复制 Hugo-Next 的 post.html文件到根目录中的 layouts:

1
cp themes/hugo-theme-next/layouts/_partials/post.html ./layouts/partials/post.html

将18,19,20行追加上hide_title的判断,如果不在Front Matter中设置hide_title = true, 则显示标题,否则隐藏标题. 于是修改为

1
2
3
4
5
{{ if not .ctx.Params.hide_title }}
<header class="post-header">
{{ partial "post/header.html" (dict "ctx" .ctx "IsHome" .IsHome) }}
</header>
{{ end }}

隐藏文章底部的虚线

原始模板在分享和赞赏之间有一条修饰的虚线,但是我感觉这不够简洁正式,于是决定去除这条线。于是修改文件static/css/custom_style.css , 在底部追加虚线隐藏判断命令:

1
2
3
4
/* 隐藏文章底部的虚线分割线 */
.post-footer hr {
    display: none !important;
}

同时在hugo.yaml中第328行,启用自定义文件,即

1
2
3
4
5
6
# 用户自定义文件配置
# Define custom file paths.
customFilePath:
sidebar: # custom_sidebar.html
footer:  # custom_footer.html
style:   /css/custom_style.css

隐藏语言切换按钮和暗黑切换按钮

修改文件static/css/custom_style.css , 在底部追加命令:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
/* 隐藏多语言翻译按钮(地球图标) */
#goto-il8n-translate {
    display: none !important;
}

/* 隐藏暗黑模式切换按钮(日月图标,推测ID/类名,若不准可补充检查) */
#theme-switch,
.dark-mode-toggle,
.theme-switcher,
.tool-buttons .button:not(#goto-il8n-translate) {
    display: none !important;
}

隐藏页面底部的语言切换开关

修改文件static/css/custom_style.css , 在底部追加命令:

1
2
3
4
/* 隐藏 ID 为 i18n-translate 的语言切换器 */
#i18n-translate {
  display: none !important;
}

隐藏表头和加黑表头控制

在导航、收藏、软件等部分,以Markdown表格组织博客,但是它们不需要表头,因此需要一个空表头,但是不渲染的效果。于是同时增加了两个功能:

  • 增加表头隐藏功能,在表格的最底部加上{.no-header}.
  • 增加表头不加黑功能,在表格的最底部加上{.plain-header}.

自定义模板

新建目录/layouts/_markup, 并建立文件/layouts/_markup/render-table.html, 其内容为:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
{{- $classes := split (index .Attributes "class" | default "") " " }}
{{- $noHeader := in $classes "no-header" }}
{{- $plainHeader := in $classes "plain-header" }}

<table
  {{- range $k, $v := .Attributes }}
    {{- if $v }}
      {{- printf " %s=%q" $k $v | safeHTMLAttr }}
    {{- end }}
  {{- end }}>

  {{- if not $noHeader }}
  <thead>
    {{- range .THead }}
    <tr>
      {{- range . }}
      <th
        {{- with .Alignment }}
          {{- printf " style=%q" (printf "text-align: %s" .) | safeHTMLAttr }}
        {{- end }}>
        {{- .Text -}}
      </th>
      {{- end }}
    </tr>
    {{- end }}
  </thead>
  {{- end }}

  <tbody>
    {{- range .TBody }}
    <tr>
      {{- range . }}
      <td
        {{- with .Alignment }}
          {{- printf " style=%q" (printf "text-align: %s" .) | safeHTMLAttr }}
        {{- end }}>
        {{- .Text -}}
      </td>
      {{- end }}
    </tr>
    {{- end }}
  </tbody>
</table>

修改配置文件

在/hugo.yaml中, markup字段增加配置如下:

1
2
3
4
5
6
7
markup:
  goldmark:
    renderer:
      unsafe: true
    parser:
      attribute:
        block: true

自定义css

文件 /static/css/custom_style.css 中追加

1
2
3
table.plain-header thead th {
  font-weight: normal !important;
}

完整的 custom_style.css 内容为:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/** Custom style defined file **/

.mydefined {
  font-size: 0.65em;
  color: #ed6c24;
}

.custom-footer {
  color: #ed6c24;
}

.custom-head5 {
  font-size: 1.2em;
  color: #ed6c24;
  font-weight: bold;
}

/* 隐藏文章底部的虚线分割线 */
.post-footer hr {
    display: none !important;
}

!important/* 隐藏多语言翻译按钮(地球图标) */
#goto-il8n-translate {
    display: none !important;
}

/* 隐藏暗黑模式切换按钮(日月图标,推测ID/类名,若不准可补充检查) */
#theme-switch,
.dark-mode-toggle,
.theme-switcher,
.tool-buttons .button:not(#goto-il8n-translate) {
    display: none !important;
}

/* 隐藏 ID 为 i18n-translate 的语言切换器 */
#i18n-translate {
  display: none !important;
}

/* 控制表头隐藏与加黑 */
table.plain-header thead th {
  font-weight: normal;
}

修复禁用favicon后网站标签问题

当在 hugo.yaml 中禁用 favicon 后,我发现首页标签页会出现 post- 前缀,修复这个问题需要做的是将hugo.yaml的favicon部分改为:

1
2
3
4
5
6
# Different size of favicon
favicon:
icon:  "" #/imgs/icons/favicon.ico
small:  "" #/imgs/icons/favicon_16x16_next.png
medium:  "" #/imgs/icons/favicon_32_32_next.png
appleTouchIcon: "" #/imgs/icons/apple_touch_icon_next.png

将 themes/hugo-theme-next/layouts/_partials/head/meta.html与themes/hugo-theme-next/layouts/_partials/head/favicon.html 中的代码:

1
2
3
4
5
<link rel="shortcut icon" type="image/x-icon" href="{{ .Site.Params.favicon.icon }}">
<link rel="icon" type="image/x-icon" href="{{ .Site.Params.favicon.icon }}">
<link rel="icon" type="image/png" sizes="16x16" href="{{ .Site.Params.favicon.small }}">
<link rel="icon" type="image/png" sizes="32x32" href="{{ .Site.Params.favicon.medium }}">
<link rel="apple-touch-icon" sizes="180x180" href="{{ .Site.Params.favicon.appleTouchIcon }}">

修改为

1
2
3
4
5
6
7
8
9
{{ with .Site.Params.favicon }}
{{ if or .icon .small .medium .appleTouchIcon }}
<link rel="shortcut icon" type="image/x-icon" href="{{ .icon }}">
<link rel="icon" type="image/x-icon" href="{{ .icon }}">
<link rel="icon" type="image/png" sizes="16x16" href="{{ .small }}">
<link rel="icon" type="image/png" sizes="32x32" href="{{ .medium }}">
<link rel="apple-touch-icon" sizes="180x180" href="{{ .appleTouchIcon }}">
{{ end }}
{{ end }}
警告

由于修改了hugo-theme-next主题的图标配置,因此在升级主题后需要重新进行配置。

其他修改参考