通常的使用情况下,一个网站并不需要一个配置文件,因为它的目录结构和模板就提供了主要的配置。

Hugo 需要在源目录查找一个 config.toml 的配置文件。如果这个文件不存在,将会查找 config.yaml,然后是 config.json

这个配置文件是一个整站的配置。它给 Hugo 提供了如何构建站点的方式,比如全局的参数和菜单。

示例

下面是一个典型的 yaml 格式的配置文件的示例:

<span>---</span>
<span><span>baseurl</span>:<span> <span>"http://yoursite.example.com/"</span>
...
</span></span>

下面是一个 toml 格式的带了一些默认值的配置文件。 在 [params] 下面的值将会构成模板里的 .Site.Params 变量:

contentdir = <span>"content"</span>
layoutdir = <span>"layouts"</span>
publishdir = <span>"public"</span>
builddrafts = <span>false</span>
baseurl = <span>"http://yoursite.example.com/"</span>
canonifyurls = <span>true</span>

[taxonomies]
  category = <span>"categories"</span>
  tag = <span>"tags"</span>

[<span>params</span>]
  description = <span>"Tesla's Awesome Hugo Site"</span>
  author = <span>"Nikola Tesla"</span>

这是一个 yaml 格式的配置文件,设置了一些更多的选项:

---
<span>baseurl:</span> <span>"http://yoursite.example.com/"</span>
<span>title:</span> <span>"Yoyodyne Widget Blogging"</span>
<span>footnotereturnlinkcontents:</span> <span>"↩"</span>
<span>permalinks:</span>
  <span>post:</span> /<span>:year/</span><span>:month/</span><span>:title/</span>
<span>params:</span>
  <span>Subtitle</span><span>:</span> <span>"Spinning the cogs in the widgets"</span>
  <span>AuthorName</span><span>:</span> <span>"John Doe"</span>
  <span>GitHubUser</span><span>:</span> <span>"spf13"</span>
  <span>ListOfFoo</span><span>:</span>
    - <span>"foo1"</span>
    - <span>"foo2"</span>
  <span>SidebarRecentLimit</span><span>:</span> <span>5</span>
...

配置变量

下面是 Hugo 定义好的变量列表,以及他们的默认值,你可以设置他们:

---
archetypedir:               <span>"archetype"</span>
<span># hostname (and path) to the root, e.g. http://spf13.com/</span>
baseURL:                    <span>""</span>
<span># include content marked as draft</span>
buildDrafts:                <span>false</span>
<span># include content with publishdate in the future</span>
buildFuture:                <span>false</span>
<span># enable this to make all relative URLs relative to content root. Note that this does not affect absolute URLs.</span>
relativeURLs:               <span>false</span>
canonifyURLs:               <span>false</span>
<span># config file (default is path/config.yaml|json|toml)</span>
config:                     <span>"config.toml"</span>
contentdir:                 <span>"content"</span>
dataDir:                    <span>"data"</span>
defaultExtension:           <span>"html"</span>
defaultLayout:              <span>"post"</span>
<span>disable</span>LiveReload:          <span>false</span>
<span># Do not build RSS files</span>
<span>disable</span>RSS:                 <span>false</span>
<span># Do not build Sitemap file</span>
<span>disable</span>Sitemap:             <span>false</span>
<span># edit new content with this editor, if provided</span>
editor:                     <span>""</span>
footnoteAnchorPrefix:       <span>""</span>
footnoteReturnLinkContents: <span>""</span>
<span># google analytics tracking id</span>
googleAnalytics:            <span>""</span>
languageCode:               <span>""</span>
layoutdir:                  <span>"layouts"</span>
<span># Enable Logging</span>
<span>log</span>:                        <span>false</span>
<span># Log File path (if set, logging enabled automatically)</span>
<span>log</span>File:                    <span>""</span>
<span># "yaml", "toml", "json"</span>
metaDataFormat:             <span>"toml"</span>
newContentEditor:           <span>""</span>
<span># Don't sync modification time of files</span>
noTimes:                    <span>false</span>
paginate:                   <span>10</span>
paginatePath:               <span>"page"</span>
permalinks:
<span># Pluralize titles in lists using inflect</span>
pluralizeListTitles:        <span>true</span>
<span># Preserve special characters in taxonomy names ("Gérard Depardieu" vs "Gerard Depardieu")</span>
preserveTaxonomyNames:      <span>false</span>
<span># filesystem path to write files to</span>
publishdir:                 <span>"public"</span>
<span># color-codes for highlighting derived from this style</span>
pygmentsStyle:              <span>"monokai"</span>
<span># true: use pygments-css or false: color-codes directly</span>
pygmentsUseClasses:         <span>false</span>
<span># default sitemap configuration map</span>
sitemap:
<span># filesystem path to read files relative from</span>
<span>source</span>:                     <span>""</span>
staticdir:                  <span>"static"</span>
<span># display memory and timing of different steps of the program</span>
stepAnalysis:               <span>false</span>
<span># theme to use (located in /doc/themes/THEMENAME/)</span>
theme:                      <span>""</span>
title:                      <span>""</span>
<span># if true, use /filename.html instead of /filename/</span>
uglyURLs:                   <span>false</span>
<span># Do not make the url/path to lowercase</span>
<span>disable</span>PathToLower:         <span>false</span>
<span># if true, auto-detect Chinese/Japanese/Korean Languages in the content. (.Summary and .WordCount can work properly in CJKLanguage)</span>
hasCJKLanguage              <span>false</span>
<span># verbose output</span>
verbose:                    <span>false</span>
<span># verbose logging</span>
verboseLog:                 <span>false</span>
<span># watch filesystem for changes and recreate as needed</span>
watch:                      <span>true</span>
---

编译时忽略的文件

使用 hugo 编译时,将忽略 config.toml 文件中指定的内容,比如:以 .foo.boo 结尾的文件。

<span>ignoreFiles = <span>[ <span>"\\.foo$"</span>, <span>"\\.boo$"</span> ]</span></span>

上面使用了正则表达式,使用 \ 转义是因为在 TOML 里。

设置 Blackfriday 渲染

Blackfriday 是 Hugo 中使用的 Markdown 渲染引擎。 Hugo 中的 Blackfriday 的默认设置已经很健全,可以适用于大多数的情况。

但是同时 Hugo 还提供了一些选项 — 正如下面的表格里的所示的, 对应于 Blackfriday 源码里的选项。 (html.gomarkdown.go):

 旗帜  默认  黑色星期五旗帜
**smartypants** true HTML_USE_SMARTYPANTS
 目的: 启用/禁用智能标点符号替换,例如智能引号、智能破折号等。可以使用 angledQuotesfractionssmartDasheslatexDashes 标志。
**angledQuotes** false HTML_SMARTYPANTS_ANGLED_QUOTES
 目的: 启用/禁用智能角度双引号。
示例: “Hugo” 呈现为 «Hugo» 而不是“Hugo”。
**fractions** true HTML_SMARTYPANTS_FRACTIONS
 目的: 启用/禁用智能分数。
示例: 5/12 渲染为 512 ( <sup>5</sup>&frasl;<sub>12</sub> )警告:即使使用 fractions = false ,Blackfriday 仍会将 1/2、1/4 和 3/4 转换为 ½ ( &frac12; )、¼ ( &frac14; ) 和分别为 3/4 ( &frac34; ),但只有这三个。
**smartDashes** true HTML_SMARTYPANTS_DASHES
 目的: 启用/禁用智能破折号,即将连字符变成短破折号或长破折号。

可以使用下面列出的 latexDashes 标志修改其行为。 | | **latexDashes** | true | HTML_SMARTYPANTS_LATEX_DASHES | |  目的: | 在 LaTeX 风格的智能破折号和“传统”智能破折号之间进行选择。

如果 true-- 被翻译为“-”( &ndash; ),而 --- 被翻译为“—”( &mdash; )。

如果 false-- 被翻译为“—”( &mdash; ),而两个单词之间的间隔单个连字符将被转换为破折号,例如 12 June - 3 July 变为 12 June &ndash; 3 July 。 | | **hrefTargetBlank** | false | HTML_HREF_TARGET_BLANK | |  目的: | 在新窗口/选项卡中打开外部链接。 | | **plainIDAnchors** | false |   FootnoteAnchorPrefixHeaderIDSuffix | |  目的: | 如果 true ,则生成页眉和脚注 ID,而无需文档 ID。
示例: #my-header 而不是 #my-header:bec3ed8ba720b9073ab75abcf3ba5d97 |

| **extensions** | [] | EXTENSION_* | |  目的: | 使用非默认附加扩展。
示例:添加 “hardLineBreak” 以使用 EXTENSION_HARD_LINE_BREAK | | **extensionsmask** | [] | EXTENSION_* | |  目的: | 不会加载此选项中的扩展。
示例:添加 “autoHeaderIds” 以禁用 EXTENSION_AUTO_HEADER_IDS |

注意

  1. 这些 flags 是非常大小写敏感的!(Hugo v0.15版本)
  2. 这些选项必须在 blackfriday 项下面,可以 同时用在 site 和 page 级别 。如果在 page 级别设置了,将会覆盖 site 上的设置。比如:
TOML YAML
[blackfriday]
  angledQuotes = <span>true</span>
  fractions = <span>false</span>
  plainIDAnchors = <span>true</span>
  extensions = [<span>"hardLineBreak"</span>]

|

blackfriday:
  angledQuotes: <span>true</span>
  fractions: <span>false</span>
  plainIDAnchors: <span>true</span>
  extensions:
    - hardLineBreak

|