配置
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
| Setting | Description |
|---|---|
title | The title of your website |
subtitle | The subtitle of your website |
description | The description of your website |
keywords | The keywords of your website. Supports multiple values. |
author | Your name |
language | The language of your website. Use a 2-letter ISO-639-1 code or optionally its variant. Default is en. |
timezone | The 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
| Setting | Description | Default |
|---|---|---|
url | The URL of your website, must start with http:// or https:// | |
root | The root directory of your website | url's pathname |
permalink | The permalink format of articles | :year/:month/:day/:title/ |
permalink_defaults | Default values of each segment in permalink | |
pretty_urls | Rewrite the permalink variables to pretty URLs | |
pretty_urls.trailing_index | Trailing index.html, set to false to remove it | true |
pretty_urls.trailing_html | Trailing .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
| Setting | Description | Default |
|---|---|---|
source_dir | Source folder. Where your content is stored | source |
public_dir | Public folder. Where the static site will be generated | public |
tag_dir | Tag directory | tags |
archive_dir | Archive directory | archives |
category_dir | Category directory | categories |
code_dir | Include code directory (subdirectory of source_dir) | downloads/code |
i18n_dir | i18n directory | :lang |
skip_render | Paths 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
| Setting | Description | Default |
|---|---|---|
new_post_name | The filename format for new posts | :title.md |
default_layout | Default layout | post |
titlecase | Transform titles into title case? | false |
external_link | Open external links in a new tab? | |
external_link.enable | Open external links in a new tab? | true |
external_link.field | Applies to the whole site or post only | site |
external_link.exclude | Exclude hostname. Specify subdomain when applicable, including www | [] |
filename_case | Transform filenames to 1 lower case; 2 upper case | 0 |
render_drafts | Display drafts? | false |
post_asset_folder | Enable the Asset Folder? | false |
relative_link | Make links relative to the root folder? | false |
future | Display future posts? | true |
syntax_highlighter | Code block syntax highlight settings, see Syntax Highlight section for usage guide | highlight.js |
highlight | Code block syntax highlight settings, see Highlight.js section for usage guide | |
prismjs | Code block syntax highlight settings, see PrismJS section for usage guide |
Home page setting
| Setting | Description | Default |
|---|---|---|
index_generator | Generate an archive of posts, powered by hexo-generator-index | |
index_generator.path | Root path for your blog's index page | '' |
index_generator.per_page | Posts displayed per page. | 10 |
index_generator.order_by | Posts order. Order by descending date (new to old) by default. | -date |
index_generator.pagination_dir | URL format, see Pagination setting below | page |
Category & Tag
| Setting | Description | Default |
|---|---|---|
default_category | Default category | uncategorized |
category_map | Override category slugs | |
tag_map | Override tag slugs |
Examples:
category_map:
"yesterday's thoughts": yesterdays-thoughts
"C++": c-plus-plusDate / Time format
Hexo uses Moment.js to process dates.
| Setting | Description | Default |
|---|---|---|
date_format | Date format | YYYY-MM-DD |
time_format | Time format | HH:mm:ss |
updated_option | The updated value to used when not provided in the front-matter | mtime |
{% note info updated_option %}updated_option controls the updated value when not provided in the front-matter:
mtime: Use file modification date asupdated. It has been the default behaviour of Hexo since 3.0.0date: Usedateasupdated. Typically used with Git workflow when file modification date could be different.empty: Simply dropupdatedwhen 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
| Setting | Description | Default |
|---|---|---|
per_page | Number of posts displayed on each page. 0 disables pagination | 10 |
pagination_dir | URL format | page |
Examples:
pagination_dir: 'page'
# http://example.com/page/2
pagination_dir: 'awesome-page'
# http://example.com/awesome-page/2Extensions
| Setting | Description |
|---|---|
theme | Theme name. false disables theming |
theme_config | Theme configuration. Include any custom theme settings under this key to override theme defaults. |
deploy | Deployment settings |
meta_generator | Meta 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.
| Setting | Description |
|---|---|
include | Include hidden files (including files/folders with a name that starts with an underscore, with an exception*) |
exclude | Exclude files/folders |
ignore | Ignore 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.jsonUsing 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_dir | Source 文件夹 存储内容的位置 | source |
public_dir | Public 文件夹 生成静态站点的位置 | public |
tag_dir | 标签文件夹 | tags |
archive_dir | 归档文件夹 | archives |
category_dir | 分类文件夹 | categories |
code_dir | Include 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 case | false |
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_dir | URL 格式,请参阅下面的 分页 设置 | 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 中没有指定 updated 时 updated 的取值 | 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_dir | URL 格式 | 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_generator | Meta generator 标签。 值为 false 时 Hexo 不会在头部插入该标签 |
包括或不包括目录和文件
使用以下选项可明确处理或忽略某些文件/文件夹。 可以使用 glob 表达式 进行路径匹配。
include 和 exclude 选项只会应用到 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"列表中的每一项都必须用单引号或双引号包裹起来。
include 和 exclude 并不适用于 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 起提供
独立的主题配置文件应放置于站点根目录下,支持 yml 或 json 格式。 需要配置站点 _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 %}