Published on

Hugo 0.84.0 Now Support Deep Merge of Theme Params

Authors
  • avatar
    Name
    ttyS3
    Twitter

功能介绍

对于主题的 params 配置,之前版本的 Hugo 只支持浅合并(shallow merge),举例来说: 假设主题有一个配置

[params]
[params.colours]
blue="#337DFF"
green="#68FF33"
red="#FF3358"

如果是之前版本的 Hugo, 你要调整其中一个配置,比如 red 的颜色,你需要 copy 整个 colours 配置,然后把 red 修改成你想要的值。

现在你可以在站点配置里这样简单地进行覆盖:

[params]
[params.colours]
red="#fc0f03"

默认合并策略

参考文档 https://gohugo.io/getting-started/configuration/#merge-configuration-from-themes

现在 _merge 的值可以是以下三种之一:

  • none 不合并
  • shallow 浅copy合并 Only add values for new keys.
  • deep 深度合并 Add values for new keys, merge existing.

当前的默认配置是:

build:
  _merge: none
caches:
  _merge: none
frontmatter:
  _merge: none
imaging:
  _merge: none
languages:
  _merge: none
  en:
    _merge: none
    menus:
      _merge: shallow
    params:
      _merge: deep
markup:
  _merge: none
mediatypes:
  _merge: shallow
menus:
  _merge: shallow
minify:
  _merge: none
module:
  _merge: none
outputformats:
  _merge: shallow
params:
  _merge: deep
permalinks:
  _merge: none
related:
  _merge: none
sitemap:
  _merge: none
taxonomies:
  _merge: none

其它重要改变

Themes now support the config directory

Now both the project and themes/modules can store its configuration in both the top level config file (e.g. config.toml) or in the config directory. See Configuration Directory.

HTTP headers in getJSON/getCSV

getJSON now supports custom HTTP headers. This has been a big limitation in Hugo, especially considering the Authorization header.

We have updated the internal Instagram shortcode to pass the access token in a header:

{{ $hideCaption := cond (eq (.Get 1) "hidecaption") "1" "0" }}
{{ $headers := dict "Authorization" (printf "Bearer %s" $accessToken) }}
{{ with getJSON "https://graph.facebook.com/v8.0/instagram_oembed/?url=https://instagram.com/p/" $id "/&hidecaption=" $hideCaption $headers }}
    {{ .html | safeHTML }}
{{ end }}

Also see the discussion this issue about the access token above.

New erroridf template func

Sometime, especially when creating themes, it is useful to be able to let the user decide if an error situation is critical enough to fail the build. The new erroridf produces ERROR log statements that can be toggled off:

{{ erroridf "some-custom-id" "Some error message." }}

Will log:

ERROR: Some error message.
If you feel that this should not be logged as an ERROR, you can ignore it by adding this to your site config:
ignoreErrors = ["some-custom-id"]

Refs

https://gohugo.io/news/0.84.0-relnotes/#deep-merge-of-theme-params

https://github.com/gohugoio/hugo/releases/tag/v0.84.0