本文介绍了hexo+github搭建博客,作者的工作环境为mac,linux、windows方法大体类似。大部分参考了该文

现在已使用更快更简洁的hugo,hexo搭建方法暂且保留。

准备工作

  • 下载安装node.js
  • 下载安装git
  • 下载安装hexo: npm install -g hexo(最好翻墙)

本文环境:

Mac OS v10.15.2
node v11.10.0
npm v6.7.0
Hexo v4.2.0
Next v6.0.0

本地搭建静态博客

  • 新建一个文件夹,如blog
  • 命令行进入该文件夹,输入:hexo init
  • 生成模版后,运行:npm install
  • 最后运行:hexo server(本地浏览器输入localhost:4000,能看到博客页面,说明搭建成功)

与github关联

  • 在Github上创建名字为XXX.github.io的项目,XXX为自己的github用户名。

  • 打开本地的MyBlog文件夹项目内的_config.yml配置文件,将其中的type设置为git,repository设置为项目的地址

    deploy:
      type: git
      repository: https://github.com/XXX/XXX.github.io.git
      branch: master
    
  • 运行:npm install hexo-deployer-git –save

  • 运行:hexo g(本地生成静态文件)

  • 运行:hexo d(将本地静态文件推送至Github)

  • 打开浏览器,访问http://XXX.github.io,应该能看到网页啦。

绑定域名

目前博客已经能通过github网址访问了,如果想将博客绑定到自己的专属域名(假定为YYY.com),需要进行如下设置:

  • 登陆github,打开XXX.github.io项目,进入项目设置settings
  • 进入Custom domain,设置自定义域名为YYY.com,点击Save保存
  • 进入自己域名的域名服务管理页面,添加一条CNAME记录:
    • CNAME—>XXX.github.io
  • 博客添加CNAME文件:
    • 配置完域名解析后,进入博客目录,在source目录下新建CNAME文件,写入域名,如:YYY.com(注意:如果没有这步操作,每次使用hexo d部署后,github中的Custom domain设置会被自动清空
  • 运行:hexo g
  • 运行:hexo d

更新文章

  • 在MyBlog目录下执行:hexo new “我的第一篇文章”,会在source->_posts文件夹内生成一个.md文件。

  • 编辑该文件(遵循Markdown规则)

  • 修改起始字段

    title 文章的标题
    date 创建日期 (文件的创建日期 )
    updated 修改日期 ( 文件的修改日期)
    comments 是否开启评论 true
    tags 标签
    categories 分类
    permalink url中的名字(文件名)
    
  • 编写正文内容(MakeDown)

  • hexo clean 删除本地静态文件(Public目录),可不执行。

  • hexo g 生成本地静态文件(Public目录)

  • hexo deploy 将本地静态文件推送至github(hexo d)

个性化

修改语言

  • 打开博客根目录,修改_config.yml

  • 修改language项为zh-CN

    language: zh-CN
    

主题

访问主题列表,获取主题代码

进入themes目录,进入以下操作:

  • 下载主题 (以next主题为例)

    git clone https://github.com/iissnan/hexo-theme-next.git next
    
  • 打开__config.yml文件,将themes修改为next(下载到的主题文件夹的名字)

  • hexo g

  • hexo d

  • 关于hexo-next主题下的一些个性化配置,参考:Next主题配置

设置主题的Scheme

在主题目录的_config.yml里找到scheme,修改为喜欢的scheme

# Schemes
#scheme: Muse
scheme: Mist
#scheme: Pisces
#scheme: Gemini

隐藏网页底部powered By Hexo / 强力驱动

打开themes/next/layout/_partials/footer.swig,使用””隐藏之间的代码即可,或者直接删除。

<!--
{%- if theme.footer.powered.enable %}

  <div class="powered-by">
    {{- __('footer.powered', next_url('https://hexo.io', 'Hexo', {class: 'theme-link'})) }}
    {%- if theme.footer.powered.version %} v{{ hexo_version }}{%- endif %}
  </div>

{%- endif %}

{%- if theme.footer.powered.enable and theme.footer.theme.enable %}
  <span class="post-meta-divider">|</span>
{%- endif %}

{%- if theme.footer.theme.enable %}

  <div class="theme-info">
    {%- set next_site = 'https://theme-next.org' %}
    {%- if theme.scheme !== 'Gemini' %}
      {%- set next_site = 'https://' + theme.scheme | lower + '.theme-next.org' %}
    {%- endif %}
    {{- __('footer.theme') }} – {{ next_url(next_site, 'NexT.' + theme.scheme, {class: 'theme-link'}) }}
    {%- if theme.footer.theme.version %} v{{ next_version }}{%- endif %}
  </div>

{%- endif %}
-->

搜索功能

安装 hexo-generator-searchdb,在站点的根目录下执行以下命令:

npm install hexo-generator-searchdb --save

编辑站点配置文件,新增以下内容到任意位置:

search:
  path: search.xml
  field: post
  format: html
  limit: 10000

编辑主题配置文件,打开local_search

# Local Search
# Dependencies: https://github.com/theme-next/hexo-generator-searchdb
local_search:
  enable: true

鼠标点击小红心的设置

love.js 文件添加到 \themes\next\source\js 文件目录下。

找到 \themes\next\layout\_layout.swig文件,添加以下代码:

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/love.js"></script>

修改文章底部的那个带#号的标签

编辑主题配置文件,设置tag_icon为true

tag_icon: true

显示数学公式

使用了hexo-filter-mathjax插件

博客项目目录下,输入命令:

npm install hexo-filter-mathjax

项目配置文件_config.yml添加如下配置:

mathjax:
  single_dollars: true
  cjk_char_width: 16
  svg: true

在使用公式的博客文章头添加mathjax: true

---
title: Title
categories: Physics
date: 1984-01-24 16:00:00
tags:
mathjax: true
---