Mac安装Hexo

安装nvm

因为brew安装nvm会有问题,所以推荐用传统方法安装1

1
2
brew install curl
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash

删除

1
to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)

升级

1
cd "$NVM_DIR" && git pull origin master && git checkout `git describe --abbrev=0 --tags`

安装node.js

1
2
nvm install 4
nvm use 4.2.6

预设使用Node的版本,否则每次重新登入,都要nvm use一次2

1
nvm alias default 4.2.6

测试:

1
nvm -v

安装Hexo

1
proxychains4 npm install -g hexo-cli

-g–global表示全局安装模块,如果没有这个参数,会安装在当前目录的node_modules子目录下3

因为GFW的问题,会在安装的时候发生错误,此时可以改用4

1
npm install hexo --no-optional

测试:

1
hexo -v

有可能会出现以下错误:

1
2
3
{ [Error: Cannot find module './build/Release/DTraceProviderBindings'] code: 'MODULE_NOT_FOUND' }
{ [Error: Cannot find module './build/default/DTraceProviderBindings'] code: 'MODULE_NOT_FOUND' }
{ [Error: Cannot find module './build/Debug/DTraceProviderBindings'] code: 'MODULE_NOT_FOUND' }

这是由于dtrace-provider出现错误,可以通过重新安装解决:

1
2
3
4
cd .nvm/versions/node/v4.2.6/lib/node_modules/hexo-cli/node_modules/hexo-log/node_modules/bunyan

npm uninstall dtrace-provider
npm install dtrace-provider --save

初始化Hexo

1
2
3
hexo init <folder>
cd <folder>
npm install

启动服务(在博客所在的文件夹内)5:

1
hexo s

如果继续发生如下错误:

1
2
3
{ [Error: Cannot find module './build/Release/DTraceProviderBindings'] code: 'MODULE_NOT_FOUND' }
{ [Error: Cannot find module './build/default/DTraceProviderBindings'] code: 'MODULE_NOT_FOUND' }
{ [Error: Cannot find module './build/Debug/DTraceProviderBindings'] code: 'MODULE_NOT_FOUND' }

可以继续通过重新安装dtrace-provider解决:

1
2
3
4
cd node_modules/hexo/node_modules/hexo-log/node_modules/bunyan

npm uninstall dtrace-provider
npm install dtrace-provider --save

更换主题

安装next主题

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

然后修改\_config.yml文件,修改为:

1
2
3
4
5
6
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: next

language: zh-Hans

使用Mathjax

安装插件6

1
npm install hexo-math --save

在网站的\_config.yml文件中添加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
math:
engine: 'mathjax' # or 'katex'
mathjax:
src: http://cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-MML-AM_HTMLorMML
config:
{
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
processEscapes: true
},
TeX: {
equationNumbers: {
autoNumber: "AMS"
}
},
messageStyle: "none"
}
katex:
css: # not used
js: # not used
config:
# KaTeX config

修改heading里#后的空格

为了##后不用加空格,直接写标题,可以对markdowm渲染器进行修改。把/Blogs/node_modules/hexo-renderer-marked/node_modules/marked/lib/marked.js改为:

1
heading: /^ *(#{1,6})+([^\n]+?) *#* *(?:\n+|$)/

支持footnote

可以通过将markdown渲染器替换为hexo-renderer-markdown-it,使得支持footnote功能7

1
2
npm un hexo-renderer-marked --save
npm i hexo-renderer-markdown-it --save

必须要先卸载原先的渲染器。然后在网站\_config.yml文件中添加:

1
2
3
markdown:
plugins:
- markdown-it-footnote

然后发现每一条footnote间都有一行间隔。想要去掉的话,可以在'/Blogs/themes/next/source/css/_custom/custom.styl'中添加:

1
2
3
.footnote-item p {
margin-bottom: 0
}

hexo-renderer-markdown-it还支持很多其他的功能,具体可以到官网了解。

Doesn't escape HTML content

hexo-renderer-markdown-it is the default renderer of my Hexo blog. But I found that the HTML content inside the md will be escaped to the final result when I wanted to force a line-break by inserting a <br> tag.

To trigger this function, the configuration of the \_config.yml in the blog should be:

1
2
3
markdown:
render:
html: true

where8:

1
2
3
html: true # Doesn't escape HTML content
## OR
html: false # Escapes HTML content so the tags will appear as text.

支持插入pdf

1
npm install --save hexo-pdf

使用9

1
2
3
4
5
6
7
8
# Normal PDF
{% pdf http://7xov2f.com1.z0.glb.clouddn.com/bash_freshman.pdf %}

#Google drive
{% pdf http://drive.google.com/file/d/0B6qSwdwPxPRdTEliX0dhQ2JfUEU/preview %}

#Slideshare
{% pdf http://www.slideshare.net/slideshow/embed_code/key/8Jl0hUt2OKUOOE %}

  1. https://github.com/creationix/nvm↩︎

  2. http://mac-osx-for-newbie-book.kejyun.com/software/softwareWebDeveloperNodeJS.html↩︎

  3. https://www.haomwei.com/technology/how-to-install-hexo-on-mac.html↩︎

  4. http://www.jianshu.com/p/fd878edb95e7↩︎

  5. https://hexo.io/docs/↩︎

  6. https://github.com/akfish/hexo-math↩︎

  7. https://github.com/celsomiranda/hexo-renderer-markdown-it↩︎

  8. https://github.com/celsomiranda/hexo-renderer-markdown-it/wiki/Advanced-Configuration↩︎

  9. https://github.com/superalsrk/hexo-pdf↩︎