Hexo基本設定與使用

Hexo本體設定

Hexo本體的設定均在<folder>/_config.yml裡面,大多數的設定都可以直接沿用很方便只有一些部分需要修改調整

詳細的Hexo Configuration

#Site Section

1
2
3
4
5
6
7
8
# Site
title: Kiwi's Blog
subtitle: 中年大叔的自言自語
description: ''
keywords:
author: Kiwi
language: zh_TW
timezone:
設定描述
title網站標題
subtitle網站副標題
description網站描述
keywords網站的關鍵詞。支援多個關鍵詞。
author您的名字
language網站使用的語言,參考 2-lettter ISO-639-1 code,預設為 en
timezone網站時區,Hexo 預設使用您電腦的時區,您可以在 時區列表 尋找適當的時區,例如 America/New_YorkJapanUTC

#URL Section

如果您的網站存放在子目錄中,例如 http://example.org/blog,請將您的 url 設為 http://example.org/blog 並把 root 設為 /blog/

1
2
3
4
5
6
7
8
# URL
url: https://kiwi0093.github.io/
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:
pretty_urls:
trailing_index: true
trailing_html: true
設定描述預設值
url網站的網址,must starts with http:// or https://
root網站的根目錄
permalink文章 永久連結 的格式:year/:month/:day/:title/
permalink_defaultspermalink 中各區段的預設值
pretty_urls改寫 permalink 的值來美化 URL
pretty_urls.trailing_index是否在永久鏈接中保留尾部的 index.html,設置為 false 時去除true
pretty_urls.trailing_html是否在永久鏈接中保留尾部的 .html, 設置為 false 時去除 (對尾部的 index.html無效)true

#Directory Section

1
2
3
4
5
6
7
8
9
# 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:
設定描述預設值
source_dir原始檔案資料夾,這個資料夾用於存放您的內容source
public_dir靜態檔案資料夾,這個資料夾用於存放建立完畢的檔案public
tag_dir標籤資料夾tags
archive_dir彙整資料夾archives
category_dir分類資料夾categories
code_dirInclude code 資料夾downloads/code
i18n_dir國際化(i18n)資料夾:lang
skip_render跳過指定檔案的渲染,您可使用 glob 表達式 來配對路徑

# Writing Section

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Writing
new_post_name: :title.md
default_layout: post
titlecase: false
external_link:
enable: true
field: site
exclude: ''
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: ''
wrap: true
hljs: false
prismjs:
enable: false
preprocess: true
line_number: true
tab_replace: ''
設定描述預設值
new_post_name新文章的檔案名稱:title.md
default_layout預設佈局post
auto_spacing在西方文字與東方文字中加入空白false
titlecase把標題轉換為 title casefalse
external_link在新頁籤中開啟連結true
external_link.enable在新頁籤中開啟連結true
external_link.fieldApplies to the whole site or post onlysite
external_link.excludeExclude hostname. Specify subdomain when applicable, including www[]
filename_case把檔案名稱轉換為: 1 小寫或 2 大寫0
render_drafts顯示草稿false
post_asset_folder啟動 Asset 資料夾false
relative_link把連結改為與根目錄的相對位址false
future顯示未來的文章true
highlight程式碼區塊的設定, see Highlight.js section for usage guide
prismjs程式碼區塊的設定, see PrismJS section for usage guide

# Homepage Section

1
2
3
4
5
6
7
8
# 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 Section

1
2
3
4
# Category & Tag
default_category: uncategorized
category_map:
tag_map:
設定描述預設值
default_category預設分類uncategorized
category_map分類別名
tag_map標籤別名

# Date & Time Section

1
2
3
4
5
6
7
8
# 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
## updated_option supports 'mtime', 'date', 'empty'
updated_option: 'mtime'

# Pagination Section

1
2
3
# Pagination
per_page: 10
pagination_dir: page
設定描述預設值
per_page一頁顯示的文章量 (0 = 關閉分頁功能)10
pagination_dir分頁目錄page

# Include / Exclude Section

1
2
3
4
5
# Include / Exclude file(s)
## include:/exclude: options only apply to the 'source/' folder
include:
exclude:
ignore:
設定描述
includeHexo 預設會忽略隱藏檔與隱藏資料夾,但列在這個欄位中的檔案,Hexo 仍然會去處理
exclude列在這裡的檔案將會被 Hexo 忽略
ignoreIgnore files/folders

# Extension Section

1
2
3
4
5
6
7
8
9
10
11
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: next

# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: git
repo: https://github.com/Kiwi0093/Kiwi0093.github.io
branch: master
設定描述
theme使用主題名稱, 設為 false 表示關閉主題功能
deploy佈署設定
#Update 2021/8/16

因為Github修改了政策,所以無法直接在https模式下用帳號/密碼登入,所以deloy的設定得改成以下

1
2
3
4
5
6
# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: git
repo: https://<token>@github.com/Kiwi0093/Kiwi0093.github.io
branch: master

要注意這樣設定的話hexo d -g就不用在輸入username/password但是一旦這個token流出去也會很有問題,最簡單的解就是把整個repo改成private這樣可以多少減少問題

Hexo使用方式

1
2
3
4
5
6
7
8
#清除建立好的資料
hexo clean
#建立靜態網頁
hexo g
#啟動Local Server
hexo s
#Deploy到Server上
hexo d

Hexo剛建好的時候需要手動建立以下頁面

  • 新增一個分類主頁:hexo new page categories

  • 新增一個Tag主頁:hexo new page tags

    source/tags/index.md 编辑:

    1
    2
    3
    ---
    type: "tags"
    ---

Hexo使用其他config.yml的方法

1
2
3
4
5
# 使用自訂的 'custom.yml' 取代預設的 '_config.yml'
$ hexo server --config custom.yml

# 使用多個配置檔, 有衝突時優先使用 'custom2.json'
$ hexo server --config custom.yml,custom2.json

參考資料

OHLIA’s Wiki

Hexo的官方文件

同場加映

最近更新後常常會有

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
FATAL YAMLException: unknown tag !<tag:yaml.org,2002:js/regexp> (116:49)

113 | ... ttributes: true
114 | ... ce: true
115 | ... ore -->' https://hexo.io/docs/tag-plugins#Post-Excerpt
116 | ... ents: [ !!js/regexp /^\s*more/]
------------------------------------------^
117 | ... true
118 | ... butes: true
at generateError (/home/kiwi/GitHub/Wiki-site/node_modules/js-yaml/lib/loader.js:183:10)
at throwError (/home/kiwi/GitHub/Wiki-site/node_modules/js-yaml/lib/loader.js:187:9)
at composeNode (/home/kiwi/GitHub/Wiki-site/node_modules/js-yaml/lib/loader.js:1521:7)
at readFlowCollection (/home/kiwi/GitHub/Wiki-site/node_modules/js-yaml/lib/loader.js:780:5)
at composeNode (/home/kiwi/GitHub/Wiki-site/node_modules/js-yaml/lib/loader.js:1442:11)
at readBlockMapping (/home/kiwi/GitHub/Wiki-site/node_modules/js-yaml/lib/loader.js:1164:11)
at composeNode (/home/kiwi/GitHub/Wiki-site/node_modules/js-yaml/lib/loader.js:1441:12)
at readBlockMapping (/home/kiwi/GitHub/Wiki-site/node_modules/js-yaml/lib/loader.js:1164:11)
at composeNode (/home/kiwi/GitHub/Wiki-site/node_modules/js-yaml/lib/loader.js:1441:12)
at readDocument (/home/kiwi/GitHub/Wiki-site/node_modules/js-yaml/lib/loader.js:1625:3)
at loadDocuments (/home/kiwi/GitHub/Wiki-site/node_modules/js-yaml/lib/loader.js:1688:5)
at Object.load (/home/kiwi/GitHub/Wiki-site/node_modules/js-yaml/lib/loader.js:1714:19)
at Hexo.yamlHelper (/home/kiwi/GitHub/Wiki-site/node_modules/hexo/lib/plugins/renderer/yaml.js:7:15)
at Hexo.tryCatcher (/home/kiwi/GitHub/Wiki-site/node_modules/bluebird/js/release/util.js:16:23)
at Hexo.<anonymous> (/home/kiwi/GitHub/Wiki-site/node_modules/bluebird/js/release/method.js:15:34)
at /home/kiwi/GitHub/Wiki-site/node_modules/hexo/lib/hexo/render.js:75:22
at tryCatcher (/home/kiwi/GitHub/Wiki-site/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/kiwi/GitHub/Wiki-site/node_modules/bluebird/js/release/promise.js:547:31)
at Promise._settlePromise (/home/kiwi/GitHub/Wiki-site/node_modules/bluebird/js/release/promise.js:604:18)
at Promise._settlePromise0 (/home/kiwi/GitHub/Wiki-site/node_modules/bluebird/js/release/promise.js:649:10)
at Promise._settlePromises (/home/kiwi/GitHub/Wiki-site/node_modules/bluebird/js/release/promise.js:729:18)
at _drainQueueStep (/home/kiwi/GitHub/Wiki-site/node_modules/bluebird/js/release/async.js:93:12) {
reason: 'unknown tag !<tag:yaml.org,2002:js/regexp>',
.......

這樣的錯誤訊息跑出來,解決方案如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# /under/your/web/_config.yml
---------------------------------------------------------------------------------------------------------------------------------
....
html_minifier:
collapseBooleanAttributes: true
collapseWhitespace: true
# Ignore '<!-- more -->' https://hexo.io/docs/tag-plugins#Post-Excerpt
#ignoreCustomComments: [ !!js/regexp /^\s*more/] #把這行Mark掉就好了
removeComments: true
removeEmptyAttributes: true
removeScriptTypeAttributes: true
removeStyleLinkTypeAttributes: true
minifyJS: true
minifyCSS: true
...