Hexo是一款基于Node.js的靜態(tài)博客框架∑碚可以方便的生成靜態(tài)網(wǎng)頁托管在github上.
由于 Hexo 是基于 Node 历恐,安裝前要先安裝 Node,這個網(wǎng)上文章很多介紹榆浓。
本文詳細介紹如何使用Hexo在github上搭建博客于未。
Hexo 的安裝
- 安裝Hexo,要用全局安裝,加-g參數(shù)
D:\github>npm install hexo-cli -g
安裝hexo發(fā)布到github的指令
D:\github>npm install hexo-deployer-git -S - 創(chuàng)建一個blog的目錄并把Hexo的源代碼下載到blog目錄
D:\github>hexo init blog - 進入下載的hexo目錄
D:\github>cd blog - 安裝依賴運行包
D:\github\blog>npm install - 啟動hexo本地程序
D:\github\blog>hexo server - 啟動瀏覽器進入查看:http://127.0.0.1:4000
Hexo 的全局配置
修改全局配置時烘浦,注意縮進抖坪,同時注意冒號后面要有一個空格。
在根目錄 _config.yml 中修改
# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/
# Site 站點配置
title: 溪水博客 # 網(wǎng)站標題
subtitle: 日積月累 # 網(wǎng)站副標題
description: 整理消化日常的工作技術(shù)文檔和代碼 # 網(wǎng)站描述
author: FanChengSheng # 你的名字
language: zh-CN # 網(wǎng)站使用的語言
timezone: Asia/Shanghai # 網(wǎng)站時區(qū)
# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: http://www.xishui.red #網(wǎng)址闷叉,搜索時會在搜索引擎中顯示
root: / # 網(wǎng)站根目錄
permalink: :year/:month/:day/:title/ # 永久鏈接格式
permalink_defaults: # 永久鏈接中各部分的默認值
# Directory 目錄配置
source_dir: source # 資源文件夾擦俐,這個文件夾用來存放內(nèi)容
public_dir: public #公共文件夾,這個文件夾用于存放生成的靜態(tài)站點文件
tag_dir: tags # 標簽文件夾
archive_dir: archives # 歸檔文件夾
category_dir: categories # 分類文件夾
code_dir: downloads/code # Include code 文件夾
i18n_dir: :lang # 國際化文件夾
skip_render: # 跳過指定文件的渲染握侧,您可使用 glob 來配置路徑
# 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 # 把文件名稱轉(zhuǎn)換為 (1) 小寫或 (2) 大寫
render_drafts: false # 顯示草稿
post_asset_folder: false # 是否啟動資源文件夾
relative_link: false # 把鏈接改為與根目錄的相對位址
future: true
highlight: # 代碼塊的設(shè)置
enable: true
line_number: true
auto_detect: false
tab_replace:
# 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 #每頁顯示的文章量 (0 = 關(guān)閉分頁功能)
pagination_dir: page #分頁目錄
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: jacman #當前主題名稱
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy: #部署到github
type: git
repo: https://github.com/fanchengsheng/blog.git
branch: master
Hexo 的主題配置
主題的配置文件在 /themes/主題文件夾/_config.yml 蚯瞧,一般包括導航配置(menu),內(nèi)容配置(content)品擎,評論插件埋合,圖片效果(fancybox)和邊欄(sidebar)。
主題的文件目錄必須在 themes 目錄下萄传。
Hexo提高了大量的主題甚颂,可以在全局配置文件中更改主題:
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: jacman #當前主題名稱
Hexo 的基本使用方法
- 寫文章通過 new 命令新建一篇文章
hexo new [layout] <title>
其中l(wèi)ayout是可選參數(shù),默認值為 post 秀菱。
如果沒有設(shè)置 layout 的話振诬,默認使用 _config.yml 中的 default_layout 參數(shù)代替。如果標題包含空格的話答朋,需用引號括起來贷揽。
這個在全局配置里面如下:
# Writing 寫作配置
new_post_name: :title.md # File name of new posts
default_layout: post # 默認布局
Hexo提供的layout在 scaffolds 目錄下,也可以在此目錄下自建layout文件梦碗。
scaffolds\post 內(nèi)容如下禽绪,添加了一個分類
---
title: {{ title }}
date: {{ date }}
tags:
categories:
---
新建的文件則會保存到 source/_post 目錄下。
source/_post/使用Hexo在githu上搭建博客.md
然后啟動服務(wù)器(hexo s)洪规,便能看到剛剛發(fā)表的文章
發(fā)表的文章會全部顯示印屁,如果文章很長,就只要顯示文章的摘要就行了斩例。在需要顯示摘要的地方添加如下代碼即可:
以上是摘要
<!--more-->
以下是余下全文
- 在部署之前雄人,需要通過命令把所有的文章都做靜態(tài)化處理,就是生成對應(yīng)的html, javascript, css念赶,使得所有的文章都是由靜態(tài)文件組成的
hexo generate
在本地目錄下础钠,會生成一個public的目錄,里面包括了所有靜態(tài)化的文件叉谜。 - 生成靜態(tài)文件之后旗吁,如果要發(fā)布到github,還需要配置 deploy 指令停局。在全局的配置文件中找到 deploy
hexo deploy
如果部署的是個人頁很钓,新建的倉庫必須的 your-user-name.github.io 香府。
請參考https://github.com/fanchengsheng/fanchengsheng.github.io.git - 域名配置如下:
- 在CNAME里把地址指向
CNAME www 默認 fanchengsheng.github.io -- 10分鐘 - 在修改https://github.com/fanchengsheng/fanchengsheng.github.io/settings里修改
Custom domain為自己買的域名
命令總結(jié)
Hexo常用命令:
啟動hexo本地程序
hexo s == hexo server
創(chuàng)建新的博客文章
hexo n == hexo new "Hello World"
生成靜態(tài)網(wǎng)站
hexo g == hexo generate
把程序發(fā)布到github上面
hexo d == hexo deploy
相關(guān)網(wǎng)址
hexo地址:https://hexo.io/
github: https://github.com/fanchengsheng/fanchengsheng.github.io.git
訪問地址: https://fanchengsheng.github.io/
域名映射地址:http://www.xishui.red/
使用的主題: https://github.com/wuchong/jacman.git