0%

搭建 hexo 博客(一)

环境

ubuntu 20.04

node 10.21.0

下载和安装nvm

nvm是node.js的版本管理器。使用nvm可以避免出现EACCES 权限错误和方便切换node.js和npm版本。

  • 执行以下命令下载和安装nvm

注意可能由于网络问题这个命令执行没成功,请在代理环境下执行以下命令

1
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
  • 验证安装
1
2
3
command -v nvm
#或者
nvm
  • 出现nvm: command not found或者没有输出nvm

重启terminal或者参考#1404解决

仍有问题请google或者参考项目的README或者github issue解决

下载和安装Node.js和npm

  • 查看所有可供安装的node版本:
1
nvm ls-remote
  • 下载node.js
1
2
3
4
#下载指定版本的node.js
nvm install 10.21.0
#如果想下载最新版本的node.js
nvm install node
  • 查看本地已安装的node.js
1
nvm ls

安装hexo

1
npm install -g hexo-cli

搭建博客

  • 初始化hexo项目
1
2
#新建一个项目,如果没写projectname,当前目录名就是项目名
hexo init projectname
  • 选择主题

    我是参考这个知乎问答选择的next主题,不过next官方版本已经停止维护了,官方推荐使用next社区维护版,但这个仓库更新也比较慢,而且代码无法高亮的问题难以解决。所以我用的是这个项目的维护者开源的另外一个版本

  • 下载next主题
1
2
3
cd projectname
#下载next主题到themes/next目录
git clone https://github.com/next-theme/hexo-theme-next themes/next
  • 切换到hexo主题。修改hexo的_config.yml配置文件,vim _config.yml
1
2
3
4
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: next
  • 启动服务器
1
hexo server
  • 写一篇博客
1
2
3
4
5
hexo new "博客标题"
#找到文章
cd /source/_posts
#编辑文章
vim 博客标题.md
  • 开启代码高亮

编辑hexo的_config.yml文件

1
2
3
4
5
6
7
highlight:
enable: true
line_number: true
auto_detect: true
tab_replace: ''
wrap: true
hljs: true

cd themes/next

编辑next的_config.yml文件,自行选择代码高亮方式

如果高亮不生效,尝试hexo generate生成静态文件

1
2
3
4
5
6
codeblock:
# Code Highlight theme
# See: https://github.com/highlightjs/highlight.js/tree/master/src/styles
theme:
light: darcula
dark: tomorrow-night

你可以选择多种代码高亮样式

参考highlight demo页面选择你喜欢的代码高亮样式

部署

  • 新建github仓库或者gitee仓库

仓库名必须为 <github的账户名>.github.io

  • 配置github地址

修改配置文件,告诉hexo远程仓库的地址

1
2
3
4
deploy:
type: git
repo: https://github.com/johnson329/johnson.github.io
branch: master
  • 安装hexo-deployer-git

它可以帮助我们把项目推送到远程仓库

1
npm install hexo-deployer-git –save
  • 配置git
1
2
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
  • 部署
1
2
3
4
hexo clean 
hexo deploy #或者hexo d
#输入github的用户名和密码,大功告成,如果想不输入用户名和密码,把配置中的repo换成ssh地址就好了
#至此,静态文件(html,css,js以及markdown)都被同步到远程仓库的master分支了