[TOC]

node和git

1
2
3
在下载Hexo之前,我们需要确保电脑上已经安装好以下软件:
Node.js (Node.js 版本需不低于 10.13,建议使用 Node.js 12.0 及以上版本) node.js 是一种javascript的运行环境,能够使得javascript脱离浏览器运行。
Git

npm(Node Package Manager)换源

1
npm config set registry http://registry.npm.taobao.org

安装hexo:

1
2
3
4
5
6
7
8
9
10
 npm install -g hexo-cli
以上命令安装的是hexo的脚手架,如果你要安装Hexo的瘦身版,自己定制Hexo,官方推荐的下载方式:

对于熟悉 npm 的进阶用户,可以仅局部安装 hexo 包。
$ npm install hexo
安装以后,可以使用以下两种方式执行 Hexo:
npx hexo <command>
Linux 用户可以将 Hexo 所在的目录下的 node_modules 添加到环境变量之中即可直接使用 hexo <command>:
echo 'PATH="$PATH:./node_modules/.bin"' >> ~/.profile

在当前目录创建一个blog:Blog

1
2
3
4
5
6
7
8
9
10
11
12
13
14
hexo init Blog

目录分析
1.2.1 source
资源文件夹。 是存放用户资源的地方。
这个文件夹非常重要,我们博客展现出来的文章最初就是存放在这里。Hexo默认创建的md文件存放在该文件夹下的 _posts 文件夹下,Hexo规定开头命名为 _ (下划线)的文件 / 文件夹和隐藏的文件将会被忽略。 Markdown 和 HTML 文件会被解析并放到 public 文件夹,而其他文件会被拷贝过去。

1.2.2 scaffolds
Hexo的模板文件。

我们在创建md文件的时候,官方默认是非常空白的,如果我们有自己的编写方式可以后续在这里自定义自己的模板。后续我会教大家来自定义自己的模板。

1.2.3 themes
我们要建造自己的博客,肯定会带有自己的特色,Hexo提供支持自定义自己的主题,什么是主题?看下图:

image-20241127015452739

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
1.2.5 package.json
这个文件不用过多介绍,很少用得到。

1.3 _config.yml 配置详解
我们打开 _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: en #语言
timezone: '' #时区

# URL
## Set your site url here. For example, if you use GitHub Page, set url as 'https://username.github.io/project'
url: http://example.com #网址, 必须以 http:// 或 https:// 开头
permalink: :year/:month/:day/:title/ #文章的 永久链接 格式
permalink_defaults: #永久链接中各部分的默认值
pretty_urls: #改写 permalink 的值来美化 URL
trailing_index: true # Set to false to remove trailing 'index.html' from permalinks #是否在永久链接中保留尾部的 index.html,设置为 false 时去除
trailing_html: true # Set to false to remove trailing '.html' from permalinks #是否在永久链接中保留尾部的 .html, 设置为 false 时去除 (对尾部的 index.html无效)

# Directory
source_dir: source #源文件夹。您的内容存储在哪里
public_dir: public #公用文件夹。静态站点将在哪里生成
tag_dir: tags #标签文件夹
archive_dir: archives #归档文件夹
category_dir: categories #分类文件夹
code_dir: downloads/code #Include code 文件夹,source_dir 下的子目录
i18n_dir: :lang #国际化(i18n)文件夹
skip_render: #匹配到的文件将会被不做改动地复制到 public 目录中。 您可使用 glob 表达式来匹配路径。

# Writing
new_post_name: :title.md # File name of new posts 新文章的文件名称
default_layout: post #预设布局
titlecase: false # Transform title into titlecase #把标题转换为 title case
external_link: #在新标签中打开链接
enable: true # Open external links in new tab #在新标签中打开链接
field: site # Apply to the whole site #对整个网站(site)生效或仅对文章(post)生效
exclude: '' #需要排除的域名。 主域名和子域名如 www 需分别配置
filename_case: 0 #将文件名转换为小写1;2上壳体
render_drafts: false #显示草稿
post_asset_folder: false #启用 资源文件夹
relative_link: false #把链接改为与根目录的相对位址
future: true #显示未来的文章
syntax_highlighter: highlight.js #代码块的设置, 请参考 代码高亮 进行设置
highlight: # 代码块的设置, 请参考 Highlight.js 进行设置
line_number: true
auto_detect: false
tab_replace: ''
wrap: true
hljs: false
prismjs: #代码块的设置, 请参考 PrismJS 进行设置
preprocess: true
line_number: true
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: #使用hexo生成器索引生成帖子存档
path: '' #博客索引页的根路径
per_page: 10 #每页显示的帖子。
order_by: -date #发布订单。默认情况下按日期降序排列(从新到旧)。

# Category & Tag
default_category: uncategorized #默认分类
category_map: #分类别名
tag_map: #标签别名

# Metadata elements
## https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta
meta_generator: true #Meta generator 标签。 值为 false 时 Hexo 不会在头部插入该标签

# 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' #当 Front Matter 中没有指定 updated 时 updated 的取值

# Pagination
## Set per_page to 0 to disable pagination
per_page: 10
pagination_dir: page

# Include / Exclude file(s)使用以下选项可明确处理或忽略某些文件/文件夹。 可以使用 glob 表达式 进行路径匹配。include 和 exclude 选项只会应用到 source/ ,而 ignore 选项会应用到所有文件夹.
## include:/exclude: options only apply to the 'source/' folder
include: #包含隐藏文件(包括名称以下划线开头的文件/文件夹,* 除外)
exclude: #排除文件或文件夹
ignore: #忽略文件/文件夹

# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: landscape #当前主题名称。 值为false时禁用主题
theme_config: #主题的配置文件。 在这里放置的配置会覆盖主题目录下的 _config.yml 中的配置

# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:
type: '' #部署部分的设置

Hexo指令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1.4 Hexo的指令
$ hexo init [folder] 新建一个网站。 如果没有设置 folder ,Hexo 默认在目前的文件夹建立网站。
本命令相当于执行了以下几步:
Git clone hexo-starter 和 hexo-theme-landscape 主题到当前目录或指定目录。
使用 Yarn 1、pnpm 或 npm 包管理器下载依赖(如有已安装多个,则列在前面的优先)。 npm 默认随 Node.js 安装。


1.5 hexo new [layout] <title> hexo new page --path about/me "About me"
-p, --path 文章的路径。 自定义文章的路径。
-r, --replace 如果存在的话,替换当前的文章。
-s, --slug 文章别名。 自定义文章的 URL。

1.6 hexo generate 生成静态文件
1.7 hexo server 在本地运行server服务器,可以访问
1.8 hexo deploy 部署你的网站。
1.9 hexo clean 清除缓存文件 (db.json) 和已生成的静态文件 (public)。
2.0 hexo migrate <type> 从其他博客系统 迁移内容。

hexo升级

1
2
3
4
5
6
7
8
9
10
11
12
13
Hexo版本升级指南按以下顺序逐步执行完成后,Hexo 版本及系统插件均会升级到最新。
//以下指令均在Hexo目录下操作,先定位到Hexo目录
//查看当前版本,判断是否需要升级> hexo version
//全局升级hexo-cli> npm i hexo-cli -g
//再次查看版本,看hexo-cli是否升级成功> hexo version
//安装npm-check,若已安装可以跳过> npm install -g npm-check
//检查系统插件是否需要升级> npm-check
//安装npm-upgrade,若已安装可以跳过> npm install -g npm-upgrade
//更新package.json> npm-upgrade
//更新全局插件> npm update -g
//更新系统插件> npm update --save
//再次查看版本,判断是否升级成功> hexo version