目標
本文將詳細介紹 Hexo 站點配置文件中的各個配置項的含義與作用。
本文基於 hexo: 3.8.0
站點配置文件
Hexo 工程的配置文件是 _config.yml
# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/
# Site
title: Hexo
subtitle:
description:
keywords:
author: John Doe
language:
timezone:
# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: http://yoursite.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
i18n_dir: :lang
skip_render:
# Writing
new_post_name: :title.md # File name of new posts
default_layout: post
titlecase: false # Transform title into titlecase
external_link: true # Open external links in new tab
filename_case: 0
render_drafts: false
post_asset_folder: false
relative_link: false
future: true
highlight:
enable: true
line_number: true
auto_detect: false
tab_replace:
# Home page setting
# path: Root path for your blogs index page. (default = '')
# per_page: Posts displayed per page. (0 = disable pagination)
# order_by: Posts order. (Order by date descending by default)
index_generator:
path: ''
per_page: 10
order_by: -date
# Category & Tag
default_category: uncategorized
category_map:
tag_map:
# Date / Time format
## Hexo uses Moment.js to parse and display date
## You can customize the date format as defined in
## http://momentjs.com/docs/#/displaying/format/
date_format: YYYY-MM-DD
time_format: HH:mm:ss
# Pagination
## Set per_page to 0 to disable pagination
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:
Site
Setting | Description |
---|---|
title | 站點名稱 |
subtitle | 站點副標題 |
description | 站點描述 |
author | 作者名稱 |
language | 網站使用的語言 |
timezone | 網站時區。Hexo 默認使用本機的時區,可用時區列表,例如:America/New_York , Japan , 和 UTC |
其中,description 主要用於 SEO,告訴搜索引擎一個關於站點的簡單描述,通常建議在其中包含網站的關鍵詞,也可以是你喜歡的一句簽名。author 參數用於主題顯示文章的作者。
對應於 NexT 主題 Gemini scheme 的顯示效果下,不同主題顯示效果不同,但原理類似。
URL
Setting | Description | Default |
---|---|---|
url | 網站的URL | |
root | 網站根目錄 | |
permalink | 文章的永久鏈接格式 | :year/:month/:day/:title/ |
permalink_defaults | 永久鏈接中各部分的默認值 |
如果網站存放在子目錄中,例如 http://yoursite.com/blog
,則需要將 url 設爲 http://yoursite.com/blog
並把 root 設爲 /blog/
。
Directory
Setting | Description | Default |
---|---|---|
source_dir | 資源文件夾,用來存放源文件,如文章的markdown文件 | source |
public_dir | 公共文件夾,用來存放生成的站點文件 | public |
tag_dir | 標籤文件夾 | tags |
archive_dir | 歸檔文件夾 | archives |
category_dir | 分類文件夾 | categories |
code_dir | Include code 文件夾 | downloads/code |
i18n_dir | 國際化(i18n)文件夾 | :lang |
skip_render | 跳過指定文件的渲染,可以使用glob表達式來匹配路徑, 如跳過README.md的渲染 |
Writing
Setting | Description | Description |
---|---|---|
new_post_name | 新文章的文件名稱 | :title.md |
default_layout | 默認佈局 | post |
titlecase | 把標題轉換爲 title case | false |
external_link | 在新標籤中打開鏈接 | true |
filename_case | 把文件名稱轉換爲"1"小寫或"2"大寫 | 0 |
render_drafts | 顯示草稿 | false |
post_asset_folder | 同步創建資源文件夾 | false |
relative_link | 把鏈接改爲與根目錄的相對地址 | false |
future | 顯示未來的文章 | true |
highlight | 代碼塊的設置 |
filename_case
filename_case
默認值爲 0,表示不做任何處理,爲 1 則轉爲小寫,爲 2 則轉爲大寫。
post_asset_folder
設置 post_asset_folder
爲 true 後,每次新建文章都會自動創建一個同名的文件夾,用於存放一些圖片文件,方便管理文章的圖片資源,創建的文件夾與新建文章在目錄結構上是同級關係。
relative_link
相對地址
默認情況下,Hexo生成的超鏈接都是絕對地址。
例如,如果您的網站域名爲example.com,您有一篇文章名爲hello,那麼絕對鏈接可能像這樣:http://example.com/hello.html,它是絕對於域名的。
相對鏈接像這樣:/hello.html,也就是說,無論用什麼域名訪問該站點,都沒有關係,這在進行反向代理時可能用到。
通常情況下,建議使用絕對地址。
highlight
Setting | Description | Default |
---|---|---|
enable | 啓用代碼高亮 | true |
line_number | 顯示行號 | true |
auto_detect | 自動檢測語言 | false |
tab_replace | 使用指定字符串替換tab鍵 |
tab_replace
會將代碼塊中所有的tab鍵都換成指定的字符串,例如:
highlight:
enable: true
line_number: true
auto_detect: false
tab_replace: true
那麼所有的tab鍵都會被替換成字符串"true", 假設原代碼爲:
class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
替換後爲:
class Main {
truepublic static void main(String[] args) {
truetrueSystem.out.println("Hello World!");
true}
}
Home page setting
主頁顯示配置
Setting | Description | Default |
---|---|---|
path | 博客主頁根路徑 | ‘’ |
per_page | 每頁展示文章數量 (0 = 關閉分頁功能) | 10 |
order_by | 排序,默認按時間降序排列 | -date |
Category & Tag
Setting | Description | Default |
---|---|---|
default_category | 默認分類 | uncategorized |
category_map | 分類別名 | |
tag_map | 標籤別名 |
Date / Time format
Hexo 使用 Moment.js 來解析和顯示時間。
Setting | Description | Default |
---|---|---|
date_format | 日期格式 | YYYY-MM-DD |
time_format | 時間格式 | H:mm:ss |
Pagination
Setting | Description | Default |
---|---|---|
per_page | 每頁顯示的文章數量 (0 = 關閉分頁功能) | 10 |
pagination_dir | 分頁目錄 | page |
Extensions
Setting | Description | Default |
---|---|---|
theme | 當前主題名稱,值爲false時禁用主題 | landscape |
Deployment
Setting | Description |
---|---|
deploy | 部署部分的設置 |
例如:
deploy:
type: git
repo: <repository url> # https://bitbucket.org/JohnSmith/johnsmith.bitbucket.io
branch: [branch name] # published
message: [commit message]
Setting | Description |
---|---|
type | deployer |
repo | 倉庫庫(Repository)地址 |
branch | 分支名稱,如果使用的是 GitHub 或 GitCafe 的話,程序會嘗試自動檢測 |
message | 自定義提交信息 (默認爲 Site updated: {{ now(‘YYYY-MM-DD HH:mm:ss’) }}) |
一個正確的部署配置中至少要有 type 參數,例如:
deploy:
type: git
可以同時使用多個 deployer,Hexo 會依照順序執行每個 deployer,例如:
deploy:
- type: git
repo:
- type: heroku
repo:
添加 README.md 文件
Hexo 默認會將 source 文件夾下的 md 文件渲染成 html 文件,所以使用 hexo 部署到倉庫後,項目下是沒有 README.md 文件的。在 Hexo 目錄下的 source 根目錄下添加一個 README.md 文件,修改 站點配置文件
,skip_render 參數的值設置如下:
skip_render: README.md
表示在執行 hexo g
時跳過渲染 README.md 這個文件。