如何利用Hexo,在Github上免費架設部落格

Hexo 是一個快速、簡單且強大的網誌框架。Hexo 使用 Markdown(或其他渲染引擎)解析您的文章,並在幾秒鐘內,透過漂亮的主題產生靜態檔案。

安裝

安裝需求

準備好上述必備需求後就可以直接安裝

安裝hexo

1
npm install -g hexo-cli

建立

安裝完成後,執行以下指令建立專案

1
2
3
hexo init <專案名>
cd <專案名>
npm install

建立完成後,專案資料夾會有下列檔案:

1
2
3
4
5
6
7
8
.
├── _config.yml
├── package.json
├── scaffolds
├── source
| ├── _drafts
| └── _posts
└── themes

配置

在_config.yml可以修改網站的設定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Site
title: 網站標題
subtitle: 網站副標題
description: 網站描述
author: 您的名字
language: zh-tw #參考[2-lettter ISO-639-1 code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
timezone: Asia/Taipei

# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: 網站的網址
root: 網站的根目錄
permalink: 文章 永久連結 的格式 (:year/:month/:day/:title/)
permalink_defaults: permalink 中各區段的預設值

指令

建立文章

1
hexo new [layout] <title>

產生靜態檔案

1
hexo generate

啟動伺服器

1
hexo server

啟動伺服器,預設是 http://localhost:4000/

部署網站

1
hexo deploy

在部署到github之前
必須先安裝 hexo-deployer-git

1
npm install hexo-deployer-git --save

修改_config.yml 設定github專案路徑

1
2
3
4
deploy:
type: github
repository: git@github.com:yourname/yourRepo.git
branch: gh-pages

清除檔案

1
hexo clean

清除快取檔案 (db.json) 和已產生的靜態檔案 (public)

自訂

主題

官方的樣式不喜歡的話沒關係,可以自己修改樣式,或是下載其他人提供的主題
並且利用sub-module進行版本管理

1
git submodule add https://github.com/iissnan/hexo-theme-next themes/next

如果主題有更新,只要執行以下指令

1
git submodule update

參考資料

Hexo
使用 submodule 管理 Hexo 主题
iissnan/hexo-theme-next: Elegant theme for Hexo.