创建一个基于hexo和github的静态博客。

安装git

检查电脑是否已经安装git,如果没有的话,执行以下命令安装:

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
$ sudo apt install git
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
git-man liberror-perl
Suggested packages:
git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn
The following NEW packages will be installed:
git git-man liberror-perl
0 upgraded, 3 newly installed, 0 to remove and 27 not upgraded.
Need to get 4,738 kB of archives.
After this operation, 33.9 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://mirrors.aliyun.com/ubuntu bionic/main amd64 liberror-perl all 0.17025-1 [22.8 kB]
Get:2 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 git-man all 1:2.17.1-1ubuntu0.5 [803 kB]
Get:3 http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 git amd64 1:2.17.1-1ubuntu0.5 [3,912 kB]
Fetched 4,738 kB in 7s (633 kB/s)
Selecting previously unselected package liberror-perl.
(Reading database ... 292089 files and directories currently installed.)
Preparing to unpack .../liberror-perl_0.17025-1_all.deb ...
Unpacking liberror-perl (0.17025-1) ...
Selecting previously unselected package git-man.
Preparing to unpack .../git-man_1%3a2.17.1-1ubuntu0.5_all.deb ...
Unpacking git-man (1:2.17.1-1ubuntu0.5) ...
Selecting previously unselected package git.
Preparing to unpack .../git_1%3a2.17.1-1ubuntu0.5_amd64.deb ...
Unpacking git (1:2.17.1-1ubuntu0.5) ...
Setting up git-man (1:2.17.1-1ubuntu0.5) ...
Setting up liberror-perl (0.17025-1) ...
Setting up git (1:2.17.1-1ubuntu0.5) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

安装成功后,将你的Git与GitHub帐号绑定,设置user.name和user.email配置信息:

1
2
3
4
5
6
7
8
9
10
$ git config --global user.name "你的GitHub用户名"
$ git config --global user.email "你的GitHub注册邮箱"
$ ssh-keygen -t rsa -C "你的GitHub注册邮箱"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/yoda/.ssh/id_rsa):
Created directory '/home/yoda/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/yoda/.ssh/id_rsa.
Your public key has been saved in /home/yoda/.ssh/id_rsa.pub.

显示生成的.ssh的文件夹中的id_rsa.pub密钥,将内容全部复制

1
$ cat ~/.ssh/id_rsa.pub 

打开GitHub_Settings_keys 页面,新建new SSH Key,Title为标题,任意填即可,将刚刚复制的id_rsa.pub内容粘贴进去,最后点击Add SSH key。

在Git Bash中检测GitHub公钥设置是否成功,输入 ssh git@github.com

1
2
3
4
5
$ ssh git@github.com
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
PTY allocation request failed on channel 0
Hi hujiyi! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

安装 hexo

使用 yarn 命令安装Hexo,输入:

1
2
3
4
5
6
7
8
9
10
11
$ sudo yarn global add hexo-cli
yarn global v1.21.1
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@2.1.2: The platform "linux" is incompatible with this module.
info "fsevents@2.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "hexo-cli@3.1.0" with binaries:
- hexo
Done in 86.85s.

创建博客项目

创建博客文件夹,输入命令:

1
$ mkdir hujiyi.github.io

进入文件夹,并执行博客的初始化:

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
$ cd hujiyi.github.io/
~/hujiyi.github.io$ hexo init
INFO Cloning hexo-starter https://github.com/hexojs/hexo-starter.git
Cloning into '/home/yoda/hujiyi.github.io'...
remote: Enumerating objects: 30, done.
remote: Counting objects: 100% (30/30), done.
remote: Compressing objects: 100% (24/24), done.
remote: Total 161 (delta 12), reused 12 (delta 4), pack-reused 131
Receiving objects: 100% (161/161), 31.79 KiB | 174.00 KiB/s, done.
Resolving deltas: 100% (74/74), done.
Submodule 'themes/landscape' (https://github.com/hexojs/hexo-theme-landscape.git) registered for path 'themes/landscape'
Cloning into '/home/yoda/hujiyi.github.io/themes/landscape'...
remote: Enumerating objects: 1054, done.
remote: Total 1054 (delta 0), reused 0 (delta 0), pack-reused 1054
Receiving objects: 100% (1054/1054), 3.21 MiB | 107.00 KiB/s, done.
Resolving deltas: 100% (581/581), done.
Submodule path 'themes/landscape': checked out '73a23c51f8487cfcd7c6deec96ccc7543960d350'
INFO Install dependencies
yarn install v1.21.1
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@2.1.2: The platform "linux" is incompatible with this module.
info "fsevents@2.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
info fsevents@1.2.11: The platform "linux" is incompatible with this module.
info "fsevents@1.2.11" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
Done in 129.04s.
INFO Start blogging with Hexo!

添加 pure 主题

1
2
3
4
5
6
~/hujiyi.github.io$ git clone https://github.com/cofess/hexo-theme-pure.git themes/pure
Cloning into 'themes/pure'...
remote: Enumerating objects: 3385, done.
remote: Total 3385 (delta 0), reused 0 (delta 0), pack-reused 3385
Receiving objects: 100% (3385/3385), 4.59 MiB | 51.00 KiB/s, done.
Resolving deltas: 100% (1892/1892), done.

将 themes/pure/_source 文件夹下的所有文件夹复制到 站点的 source/ 文件夹下。

安装字数统计插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
~/hujiyi.github.io$ yarn add hexo-wordcount
yarn add v1.21.1
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@2.1.2: The platform "linux" is incompatible with this module.
info "fsevents@2.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
info fsevents@1.2.11: The platform "linux" is incompatible with this module.
info "fsevents@1.2.11" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ hexo-wordcount@6.0.1
info All dependencies
└─ hexo-wordcount@6.0.1
Done in 9.20s.

修改主题的配置文件 themes/pure/_config.yml,启用字数统计功能,如以下代码所示:

1
2
3
4
5
# wordcount
postCount:
enable: true
wordcount: true # 文章字数统计
min2read: true # 阅读时长预计

hexo-generator-json-content

简介:用于生成静态站点数据,提供搜索功能的数据源。

安装:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
~/hujiyi.github.io$ yarn add hexo-generator-json-content
yarn add v1.21.1
[1/4] Resolving packages...
[2/4] Fetching packages...
warning Pattern ["hexo-util@latest"] is trying to unpack in the same destination "/home/yoda/.cache/yarn/v6/npm-hexo-util-1.8.1-d8287561709e246a6a73f07fccaed2a82865a736-integrity/node_modules/hexo-util" as pattern ["hexo-util@^1.8.0","hexo-util@^1.4.0","hexo-util@^1.0.0"]. This could result in non-deterministic behavior, skipping.
warning Pattern ["moment@latest"] is trying to unpack in the same destination "/home/yoda/.cache/yarn/v6/npm-moment-2.24.0-0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b-integrity/node_modules/moment" as pattern ["moment@^2.22.2","moment@>= 2.9.0","moment@^2.10.6","moment@^2.18.0"]. This could result in non-deterministic behavior, skipping.
info fsevents@2.1.2: The platform "linux" is incompatible with this module.
info "fsevents@2.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
info fsevents@1.2.11: The platform "linux" is incompatible with this module.
info "fsevents@1.2.11" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 4 new dependencies.
info Direct dependencies
└─ hexo-generator-json-content@4.1.6
info All dependencies
├─ hexo-generator-json-content@4.1.6
├─ keyword-extractor@0.0.18
├─ underscore.string@3.3.5
└─ underscore@1.9.1
Done in 2.13s.

配置:

在博客配置文件_config.yml中添加

1
2
3
4
5
6
7
jsonContent:
ignore:
- path/to/a/page
- url/to/one/post
- an-entire-category
- specific.file
- .ext # a file extension

自动压缩代码插件 hexo-neat

简介:自动压缩html、css、js代码

安装:

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
34
35
36
37
38
39
40
41
42
43
44
~/hujiyi.github.io$ yarn add hexo-neat
yarn add v1.21.1
[1/4] Resolving packages...
[2/4] Fetching packages...
warning Pattern ["hexo-util@latest"] is trying to unpack in the same destination "/home/yoda/.cache/yarn/v6/npm-hexo-util-1.8.1-d8287561709e246a6a73f07fccaed2a82865a736-integrity/node_modules/hexo-util" as pattern ["hexo-util@^1.8.0","hexo-util@^1.4.0","hexo-util@^1.0.0"]. This could result in non-deterministic behavior, skipping.
warning Pattern ["moment@latest"] is trying to unpack in the same destination "/home/yoda/.cache/yarn/v6/npm-moment-2.24.0-0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b-integrity/node_modules/moment" as pattern ["moment@^2.22.2","moment@>= 2.9.0","moment@^2.10.6","moment@^2.18.0"]. This could result in non-deterministic behavior, skipping.
info fsevents@2.1.2: The platform "linux" is incompatible with this module.
info "fsevents@2.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
info fsevents@1.2.11: The platform "linux" is incompatible with this module.
info "fsevents@1.2.11" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 25 new dependencies.
info Direct dependencies
└─ hexo-neat@1.0.4
info All dependencies
├─ any-promise@1.3.0
├─ change-case@3.0.2
├─ clean-css@4.2.3
├─ commander@2.9.0
├─ constant-case@2.0.0
├─ dot-case@2.1.1
├─ he@1.1.1
├─ header-case@1.0.1
├─ hexo-neat@1.0.4
├─ html-minifier@2.1.7
├─ is-lower-case@1.1.3
├─ is-upper-case@1.1.2
├─ lower-case-first@1.0.2
├─ ncname@1.0.0
├─ object-assign@4.1.1
├─ param-case@2.1.1
├─ path-case@2.1.1
├─ relateurl@0.2.7
├─ sentence-case@2.1.1
├─ stream-to-array@2.3.0
├─ swap-case@1.1.2
├─ title-case@2.1.1
├─ uglify-js@2.6.4
├─ upper-case-first@1.1.2
└─ xml-char-classes@1.0.0
Done in 4.09s.

配置:

在博客配置文件_config.yml中添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
neat_enable: true

# 压缩html代码
neat_html:
enable: true
exclude:

# 压缩CSS
neat_css:
enable: true
exclude:
- '*.min.css'

#压缩JS
neat_js:
enable: true
mangle: true
output:
compress:
exclude:
- '*.min.js'

安装Git部署插件 hexo-deployer-git

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
~/hujiyi.github.io$ yarn add hexo-deployer-git
yarn add v1.21.1
[1/4] Resolving packages...
[2/4] Fetching packages...
info fsevents@2.1.2: The platform "linux" is incompatible with this module.
info "fsevents@2.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
info fsevents@1.2.11: The platform "linux" is incompatible with this module.
info "fsevents@1.2.11" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
success Saved 1 new dependency.
info Direct dependencies
└─ hexo-deployer-git@2.1.0
info All dependencies
└─ hexo-deployer-git@2.1.0
Done in 7.44s.

打开站点的配置文件_config.yml,翻到最后修改为:

1
2
3
4
deploy:
type: git
repository: git@github.com:用户名/用户名.github.io.git
branch: master

完成后运行 hexo d 将网站上传部署到 GitHub Pages。

完成!这时访问我们的 GitHub 域名 https://用户名.github.io 就可以看到 Hexo 网站了

添加博客文章

创建新文章

1
hexo new "your bolg title"

编辑 source/_posts/ 文件夹下的 同名 md 文件。

生成静态博客

1
hexo g

上传到github

1
hexo d

添加 gitalk 评论

创建OAuth Application

Gitalk 需要一个 Github Application,打开 https://github.com/settings/applications/new 当然,也可以在用户头像下的setting下的Developer settings中new一个Application,然后填写相应的参数。

Register a new OAuth application

Application name (名字随便填,例如:Hujy blog comments)

Something users will recognize and trust.

Homepage URL (博客地址:https://hujiyi.github.io/)

The full URL to your application homepage.

Application description (描述,随便填)

This is displayed to all users of your application.

Authorization callback URL (博客地址,回调地址:https://hujiyi.github.io/)

Your application’s callback URL. Read our OAuth documentation for more information.

完成后会生成相应的clientID 和 clientSecret。

1
2
3
4
5
6
7
8
9
10
11
12
13
Hujy blog comments
@hujiyi

hujiyi owns this application.

You can list your application in the GitHub Marketplace so that other users can discover it.

0 users

Client ID
66f51d5c8d75dac*****
Client Secret
abac11570202d779515d511e338cf3**********

新建一个名字为 blogcomments 的仓库用于存储评论内容

修改主题的配置文件 themes/pure/_config.yml,如以下代码所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Comment
# Gitment
# Introduction: https://imsun.net/posts/gitment-introduction/
comment:
type: gitalk # 启用哪种评论系统


gitalk: # gitalk. https://gitalk.github.io/
owner: hujiyi #必须. GitHub repository 所有者,可以是个人或者组织。
admin: hujiyi #必须. GitHub repository 的所有者和合作者 (对这个 repository 有写权限的用户)。
repo: blogcomments #必须. GitHub repository.
ClientID: 66f51d5c8d75dac***** #必须. GitHub Application Client ID.
ClientSecret: abac11570202d779515d511e338cf3********** #必须. GitHub Application Client Secret.

Hexo博客常用插件

Hexo博客常用插件及用法

===END===