配置

Hexo 的配置文件 _config.yml 是一个非常重要的文件,用于自定义网站的各种设置。下面是一个基本的 _config.yml 文件示例,并附带一些常用的配置选项说明:

# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/

# Site
title: Your Blog Title          # 博客标题
subtitle: A simple blog       # 博客副标题
description: ""                 # 描述信息
author: Your Name             # 作者名字
language: zh-CN               # 设置语言,默认是zh-CN(简体中文)
timezone: Asia/Shanghai       # 时区

# URL
url: http://yourdomain.com    # 您的博客网址
root: /                       # 根目录
permalink: :year/:month/:day/:title/   # 文章永久链接格式
permalink_defaults:

# Directory
source_dir: source            # 资源文件夹,这个文件夹用来存放文章和其他资源
public_dir: public            # 公共文件夹,这个文件夹用于存放生成的静态页面
tag_dir: tags                 # 标签文件夹
archive_dir: archives         # 归档文件夹
category_dir: categories      # 分类文件夹
code_dir: downloads/code      # Include code 文件夹,保存 include 代码
i18n_dir: :lang               # 国际化文件夹
skip_render:

# Writing
new_post_name: :title.md      # 新建文章的默认文件名
default_layout: post          # 默认布局
titlecase: false              # 是否将标题转换为 title case
external_link:
  enable: true                # 是否开启外链跳转提示
  field: site                 # 对整个站点生效还是仅对文章生效
  exclude:                    #
  hostname_skip:              #
filename_case: 0              # 处理文件名大小写的方式
render_drafts: false          # 显示草稿
post_asset_folder: false      # 启用 Asset 文件夹
relative_link: false          # 将链接改为相对路径
future: true                  # 显示未来的文章
highlight:
  enable: true                # 开启代码高亮
  line_number: true           # 显示行号
  auto_detect: false          # 自动检测语言类型
  tab_replace: ""
  wrap: true
  hljs: false

# Home page setting
pagination_dir: page          # 分页目录
per_page: 10                  # 每页显示的文章数量
pagination_dir: page          # 分页目录

# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: landscape              # 主题名称

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type: git                   # 部署方式
  repo: https://github.com/user/repo.git # 远程仓库地址
  branch: master              # 分支名

常见配置项解释:

  • Site: 包含了网站的基本信息,如标题、描述等。
  • URL: 定义了网站的根URL和永久链接格式。
  • Directory: 指定了各个目录的位置,包括资源文件夹、公共文件夹等。
  • Writing: 提供了一些写作相关的配置,比如新建文章的命名规则、是否启用代码高亮等。
  • Home page setting: 控制首页分页的相关设置。
  • Extensions: 可以在这里添加插件和主题的信息。
  • Deployment: 定义了如何部署您的网站,比如通过Git推送至GitHub Pages。
    您可以根据自己的需求调整这些配置项,以定制出符合自己风格的博客。

英文版

You can modify site settings in _config.yml or in an alternate config file.

Site

SettingDescription
titleThe title of your website
subtitleThe subtitle of your website
descriptionThe description of your website
keywordsThe keywords of your website. Supports multiple values.
authorYour name
languageThe language of your website. Use a 2-letter ISO-639-1 code or optionally its variant. Default is en.
timezoneThe timezone of your website. Hexo uses the setting on your computer by default. You can find the list of available timezones here. Some examples are America/New_York, Japan, and UTC.

URL

SettingDescriptionDefault
urlThe URL of your website, must start with http:// or https://
rootThe root directory of your websiteurl's pathname
permalinkThe permalink format of articles:year/:month/:day/:title/
permalink_defaultsDefault values of each segment in permalink
pretty_urlsRewrite the permalink variables to pretty URLs
pretty_urls.trailing_indexTrailing index.html, set to false to remove ittrue
pretty_urls.trailing_htmlTrailing .html, set to false to remove it (_does not apply to trailing index.html_)true

{% note info Website in subdirectory %}
If your website is in a subdirectory (such as http://example.org/blog) set url to http://example.org/blog and set root to /blog/.
{% endnote %}

Examples:

# e.g. page.permalink is http://example.com/foo/bar/index.html
pretty_urls:
  trailing_index: false
# becomes http://example.com/foo/bar/

Directory

SettingDescriptionDefault
source_dirSource folder. Where your content is storedsource
public_dirPublic folder. Where the static site will be generatedpublic
tag_dirTag directorytags
archive_dirArchive directoryarchives
category_dirCategory directorycategories
code_dirInclude code directory (subdirectory of source_dir)downloads/code
i18n_diri18n directory:lang
skip_renderPaths that will be copied to public raw, without being rendered. You can use glob expressions for path matching.

Examples:

skip_render: "mypage/**/*"
# will output `source/mypage/index.html` and `source/mypage/code.js` without altering them.

## This also can be used to exclude posts,
skip_render: "_posts/test-post.md"
# will ignore the `source/_posts/test-post.md`.

Writing

SettingDescriptionDefault
new_post_nameThe filename format for new posts:title.md
default_layoutDefault layoutpost
titlecaseTransform titles into title case?false
external_linkOpen external links in a new tab?
external_link.enableOpen external links in a new tab?true
external_link.fieldApplies to the whole site or post onlysite
external_link.excludeExclude hostname. Specify subdomain when applicable, including www[]
filename_caseTransform filenames to 1 lower case; 2 upper case0
render_draftsDisplay drafts?false
post_asset_folderEnable the Asset Folder?false
relative_linkMake links relative to the root folder?false
futureDisplay future posts?true
syntax_highlighterCode block syntax highlight settings, see Syntax Highlight section for usage guidehighlight.js
highlightCode block syntax highlight settings, see Highlight.js section for usage guide
prismjsCode block syntax highlight settings, see PrismJS section for usage guide

Home page setting

SettingDescriptionDefault
index_generatorGenerate an archive of posts, powered by hexo-generator-index
index_generator.pathRoot path for your blog's index page''
index_generator.per_pagePosts displayed per page.10
index_generator.order_byPosts order. Order by descending date (new to old) by default.-date
index_generator.pagination_dirURL format, see Pagination setting belowpage

Category & Tag

SettingDescriptionDefault
default_categoryDefault categoryuncategorized
category_mapOverride category slugs
tag_mapOverride tag slugs

Examples:

category_map:
  "yesterday's thoughts": yesterdays-thoughts
  "C++": c-plus-plus

Date / Time format

Hexo uses Moment.js to process dates.

SettingDescriptionDefault
date_formatDate formatYYYY-MM-DD
time_formatTime formatHH:mm:ss
updated_optionThe updated value to used when not provided in the front-mattermtime

{% note info updated_option %}
updated_option controls the updated value when not provided in the front-matter:

  • mtime: Use file modification date as updated. It has been the default behaviour of Hexo since 3.0.0
  • date: Use date as updated. Typically used with Git workflow when file modification date could be different.
  • empty: Simply drop updated when not provided. May not be compatible with most themes and plugins.

use_date_for_updated is removed in v7.0.0+. Please use updated_option: 'date' instead.
{% endnote %}

Pagination

SettingDescriptionDefault
per_pageNumber of posts displayed on each page. 0 disables pagination10
pagination_dirURL formatpage

Examples:

pagination_dir: 'page'
# http://example.com/page/2

pagination_dir: 'awesome-page'
# http://example.com/awesome-page/2

Extensions

SettingDescription
themeTheme name. false disables theming
theme_configTheme configuration. Include any custom theme settings under this key to override theme defaults.
deployDeployment settings
meta_generatorMeta generator tag. false disables injection of the tag.

Include/Exclude Files or Folders

Use the following options to explicitly process or ignore certain files/folders. Support glob expressions for path matching.

include and exclude options only apply to the source/ folder, whereas ignore option applies to all folders.

SettingDescription
includeInclude hidden files (including files/folders with a name that starts with an underscore, with an exception*)
excludeExclude files/folders
ignoreIgnore files/folders

Examples:

# Include/Exclude Files/Folders
include:
  - ".nojekyll"
  # Include 'source/css/_typing.css'.
  - "css/_typing.css"
  # Include any file in 'source/_css/'.
  - "_css/*"
  # Include any file and subfolder in 'source/_css/'.
  - "_css/**/*"

exclude:
  # Exclude 'source/js/test.js'.
  - "js/test.js"
  # Exclude any file in 'source/js/'.
  - "js/*"
  # Exclude any file and subfolder in 'source/js/'.
  - "js/**/*"
  # Exclude any file with filename that starts with 'test' in 'source/js/'.
  - "js/test*"
  # Exclude any file with filename that starts with 'test' in 'source/js/' and its subfolders.
  - "js/**/test*"
  # Do not use this to exclude posts in the 'source/_posts/'.
  # Use skip_render for that. Or prepend an underscore to the filename.
  # - "_posts/hello-world.md" # Does not work.

ignore:
  # Ignore any folder named 'foo'.
  - "**/foo"
  # Ignore 'foo' folder in 'themes/' only.
  - "**/themes/*/foo"
  # Same as above, but applies to every subfolders of 'themes/'.
  - "**/themes/**/foo"

Each value in the list must be enclosed with single/double quotes.

include: and exclude: do not apply to the themes/ folder. Either use ignore: or alternatively, prepend an underscore to the file/folder name to exclude.

* Notable exception is the source/_posts folder, but any file or folder with a name that starts with an underscore under that folder would still be ignored. Using include: rule in that folder is not recommended.

Using an Alternate Config

A custom config file path can be specified by adding the --config flag to your hexo commands with a path to an alternate YAML or JSON config file, or a comma-separated list (no spaces) of multiple YAML or JSON files.

# use 'custom.yml' in place of '_config.yml'
$ hexo server --config custom.yml

# use 'custom.yml' & 'custom2.json', prioritizing 'custom2.json'
$ hexo server --config custom.yml,custom2.json

Using multiple files combines all the config files and saves the merged settings to _multiconfig.yml. The later values take precedence. It works with any number of JSON and YAML files with arbitrarily deep objects. Note that no spaces are allowed in the list.

For instance, in the above example if foo: bar is in custom.yml, but "foo": "dinosaur" is in custom2.json, _multiconfig.yml will contain foo: dinosaur.

Alternate Theme Config

Hexo themes are independent projects, with separate _config.yml files.

Instead of forking a theme, and maintaining a custom version with your settings, you can configure it from somewhere else:

from theme_config in site's primary configuration file

Supported since Hexo 2.8.2
# _config.yml
theme: "my-theme"
theme_config:
  bio: "My awesome bio"
  foo:
    bar: "a"
# themes/my-theme/_config.yml
bio: "Some generic bio"
logo: "a-cool-image.png"
  foo:
    baz: 'b'

Resulting in theme configuration:

{
  "bio": "My awesome bio",
  "logo": "a-cool-image.png",
  "foo": {
    "bar": "a",
    "baz": "b"
  }
}

from a dedicated _config.[theme].yml file

Supported since Hexo 5.0.0

The file should be placed in your site folder, both yml and json are supported. theme inside _config.yml must be configured for Hexo to read _config.[theme].yml

# _config.yml
theme: "my-theme"
# _config.my-theme.yml
bio: "My awesome bio"
foo:
  bar: "a"
# themes/my-theme/_config.yml
bio: "Some generic bio"
logo: "a-cool-image.png"
  foo:
    baz: 'b'

Resulting in theme configuration:

{
  "bio": "My awesome bio",
  "logo": "a-cool-image.png",
  "foo": {
    "bar": "a",
    "baz": "b"
  }
}

{% note %}
We strongly recommend that you store your theme configuration in one place. But in case you have to store your theme configuration separately, you need to know the priority of those configurations: The theme_config inside site's primary configuration file has the highest priority during merging, then the dedicated theme configuration file.
The _config.yml file under the theme directory has the lowest priority.
{% endnote %}

中文版

您可以在 _config.yml代替配置文件 中修改大部分的配置。

网站

设置描述
title网站标题
subtitle网站副标题
description网站描述
keywords网站的关键词。 支持多个关键词。
author您的名字
language网站使用的语言。 使用 2 个字母的 ISO-639-1 代码,或 它的变体。 默认为 en
timezone网站时区。 Hexo 默认使用您电脑的时区。 请参考 时区列表 进行设置,如 America/New_York, Japan, 和 UTC 。 一般的,对于中国大陆地区可以使用 Asia/Shanghai

网址

设置描述默认值
url网址, 必须以 http://https:// 开头
root网站根目录url's pathname
permalink文章的 永久链接 格式:year/:month/:day/:title/
permalink_defaults永久链接中各部分的默认值
pretty_urls改写 permalink 的值来美化 URL
pretty_urls.trailing_index是否在永久链接中保留尾部的 index.html,设置为 false 时去除true
pretty_urls.trailing_html是否在永久链接中保留尾部的 .html, 设置为 false 时去除 (_对尾部的 index.html无效_)true

{% note info 网站存放在子目录 %}
如果您的网站存放在子目录中,例如 http://example.com/blog,则请将您的 url 设为 http://example.com/blog 并把 root 设为 /blog/
{% endnote %}

例如:

# e.g. page.permalink is http://example.com/foo/bar/index.html
pretty_urls:
  trailing_index: false
# becomes http://example.com/foo/bar/

目录

设置描述默认值
source_dirSource 文件夹 存储内容的位置source
public_dirPublic 文件夹 生成静态站点的位置public
tag_dir标签文件夹tags
archive_dir归档文件夹archives
category_dir分类文件夹categories
code_dirInclude code 文件夹,source_dir 下的子目录downloads/code
i18n_dir国际化(i18n)文件夹:lang
skip_render匹配到的文件将会被不做改动地复制到 public 目录中。 您可使用 glob 表达式来匹配路径。

例如:

skip_render: "mypage/**/*"
# 将会直接将 `source/mypage/index.html` 和 `source/mypage/code.js` 不做改动地输出到 'public' 目录
# 你也可以用这种方法来跳过对指定文章文件的渲染
skip_render: "_posts/test-post.md"
# 这将会忽略对 'test-post.md' 的渲染

## This also can be used to exclude posts,
skip_render: "_posts/test-post.md"
# will ignore the `source/_posts/test-post.md`.

文章

设置描述默认值
new_post_name新文章的文件名称:title.md
default_layout预设布局post
titlecase把标题转换为 title casefalse
external_link在新标签中打开链接
external_link.enable在新标签中打开链接true
external_link.field对整个网站(site)生效或仅对文章(post)生效site
external_link.exclude需要排除的域名。 主域名和子域名如 www 需分别配置[]
filename_case设置为 1 ,将文件名转换为小写形式; 设置为 2 ,将文件名转换为大写形式。0
render_drafts显示草稿false
post_asset_folder启用 资源文件夹false
relative_link把链接改为与根目录的相对位址false
future显示未来的文章true
syntax_highlighter代码块的设置, 请参考 代码高亮 进行设置highlight.js
highlight代码块的设置, 请参考 Highlight.js 进行设置
prismjs代码块的设置, 请参考 PrismJS 进行设置

首页设置

设置描述默认值
index_generator生成帖子归档。由 hexo-generator-index 驱动。
index_generator.path博客索引页面的根路径''
index_generator.per_page每页显示帖子数10
index_generator.order_by帖子排列顺序。 默认情况下按日期降序(从新到旧)。-date
index_generator.pagination_dirURL 格式,请参阅下面的 分页 设置page

分类 & 标签

设置描述默认值
default_category默认分类uncategorized
category_map分类别名
tag_map标签别名

例如:

category_map:
  "yesterday's thoughts": yesterdays-thoughts
  "C++": c-plus-plus

日期 / 时间格式

Hexo 使用 Moment.js 来解析和显示时间。

设置描述默认值
date_format日期格式YYYY-MM-DD
time_format时间格式HH:mm:ss
updated_option当 Front Matter 中没有指定 updatedupdated 的取值mtime

{% note info updated_option %}
updated_option 控制了当 Front Matter 中没有指定 updated 时,updated 如何取值:

  • mtime: 使用文件的最后修改时间。 这是从 Hexo 3.0.0 开始的默认行为。
  • date: 使用 date 作为 updated 的值。 可被用于 Git 工作流之中,因为使用 Git 管理站点时,文件的最后修改日期常常会发生改变
  • empty: 直接删除 updated。 使用这一选项可能会导致大部分主题和插件无法正常工作。

use_date_for_updated 选项已经在 v7.0.0+ 中被移除。 请改为使用 updated_option: 'date'
{% endnote %}

分页

设置描述参数
per_page每页显示的帖子数。 0 关闭分页功能10
pagination_dirURL 格式page

例如:

pagination_dir: 'page'
# http://example.com/page/2

pagination_dir: 'awesome-page'
# http://example.com/awesome-page/2

扩展

设置描述
theme当前主题名称。 值为false时禁用主题
theme_config主题的配置文件。 在这里放置的配置会覆盖主题目录下的 _config.yml 中的配置
deploy部署部分的设置
meta_generatorMeta generator 标签。 值为 false 时 Hexo 不会在头部插入该标签

包括或不包括目录和文件

使用以下选项可明确处理或忽略某些文件/文件夹。 可以使用 glob 表达式 进行路径匹配。

includeexclude 选项只会应用到 source/ ,而 ignore 选项会应用到所有文件夹.

设置描述
include包含隐藏文件(包括名称以下划线开头的文件/文件夹,* 除外)
exclude排除文件或文件夹
ignore忽略文件/文件夹

例如:

# 处理或不处理目录或文件
include:
  - ".nojekyll"
  # 处理 'source/css/_typing.css'
  - "css/_typing.css"
  # 处理 'source/_css/' 中的任何文件,但不包括子目录及其其中的文件。
  - "_css/*"
  # 处理 'source/_css/' 中的任何文件和子目录下的任何文件。
  - "_css/**/*"

exclude:
  # 不处理 'source/js/test.js'。
  - "js/test.js"
  # 不处理 'source/js/' 中的文件、但包括子目录下的所有目录和文件。
  - "js/*"
  # 不处理 'source/js/' 中的文件和子目录下的任何文件。
  - "js/**/*"
  # 不处理 'source/js/' 目录下的所有文件名以 'test' 开头的文件,但包括其它文件和子目录下的单文件。
  - "js/test*"
  # 不处理 'source/js/' 及其子目录中任何以 'test' 开头的文件。
  - "js/**/test*"
  # 不要用 exclude 来忽略 'source/_posts/' 中的文件。
  # 你应该使用 'skip_render'。 或者在要忽略的文件的文件名之前加一个下划线 '_'。
  # - "_posts/hello-world.md" # 在这里配置是没有用的。

ignore:
  # 忽略任何一个名叫 'foo' 的文件夹。
  - "**/foo"
  # 只忽略 'themes/' 下的 'foo' 文件夹。
  - "**/themes/*/foo"
  # 对 'themes/' 目录下的每个文件夹中忽略名叫 'foo' 的子文件夹。
  - "**/themes/**/foo"

列表中的每一项都必须用单引号或双引号包裹起来。

includeexclude 并不适用于 themes/ 目录下的文件。 如果需要忽略 themes/ 目录下的部分文件或文件夹,可以使用 ignore 或在文件名之前添加下划线 _

source/_posts 文件夹是一个例外,但该文件夹下任何名称以 _ 开头的文件或文件夹仍会被忽略。 不建议在该文件夹中使用 include 规则。

使用代替配置文件

可以在 hexo-cli 中使用 --config 参数来指定自定义配置文件的路径。 你可以使用一个 YAML 或 JSON 文件的路径,也可以使用逗号分隔(无空格)的多个 YAML 或 JSON 文件的路径。

# use 'custom.yml' in place of '_config.yml'
$ hexo server --config custom.yml

# use 'custom.yml' & 'custom2.json', prioritizing 'custom2.json'
$ hexo server --config custom.yml,custom2.json

当你指定了多个配置文件以后,Hexo 会按顺序将这部分配置文件合并成一个 _multiconfig.yml。 后面的值优先。 这个原则适用于任意数量、任意深度的 YAML 和 JSON 文件。 请注意,列表中不允许有空格

如果 custom.yml 中指定了 foo: bar,在 custom2.json 中指定了 "foo": "dinosaur",那么在 _multiconfig.yml 中你会得到 foo: dinosaur

使用代替主题配置文件

通常情况下,Hexo 主题是一个独立的项目,并拥有一个独立的 _config.yml 配置文件。

除了自行维护独立的主题配置文件,你也可以在其它地方对主题进行配置。

配置文件中的 theme_config

该特性自 Hexo 2.8.2 起提供
# _config.yml
theme: "my-theme"
theme_config:
  bio: "My awesome bio"
  foo:
    bar: "a"
# themes/my-theme/_config.yml
bio: "Some generic bio"
logo: "a-cool-image.png"
  foo:
    baz: 'b'

最终主题配置的输出是:

{
  "bio": "My awesome bio",
  "logo": "a-cool-image.png",
  "foo": {
    "bar": "a",
    "baz": "b"
  }
}

独立的 _config.[theme].yml 文件

该特性自 Hexo 5.0.0 起提供

独立的主题配置文件应放置于站点根目录下,支持 ymljson 格式。 需要配置站点 _config.yml 文件中的 theme 以供 Hexo 寻找 _config.[theme].yml 文件。

# _config.yml
theme: "my-theme"
# _config.my-theme.yml
bio: "My awesome bio"
foo:
  bar: "a"
# themes/my-theme/_config.yml
bio: "Some generic bio"
logo: "a-cool-image.png"
  foo:
    baz: 'b'

最终主题配置的输出是:

{
  "bio": "My awesome bio",
  "logo": "a-cool-image.png",
  "foo": {
    "bar": "a",
    "baz": "b"
  }
}

{% note %}
我们强烈建议你将所有的主题配置集中在一处。 如果你不得不在多处配置你的主题,那么这些信息对你将会非常有用:Hexo 在合并主题配置时,Hexo 配置文件中的 theme_config 的优先级最高,其次是 _config.[theme].yml 文件。 最后是位于主题目录下的 _config.yml 文件。
{% endnote %}