HEXO+Github搭建免费静态Blog

img

前言

想熟悉markdown和纪录一些东西,于是有了这个blog和这篇文章!


更新历史

  • 20160607: 修正错误和排版
  • 20160603: 完成初稿

准备工作

什么是 Hexo?

Hexo 是一个快速、简洁且高效的博客框架。Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页。

安装前提

安装 Hexo 相当简单。然而在安装前,您必须检查电脑中是否已安装下列应用程序:

如果您的电脑中已经安装上述必备程序,那么恭喜您!接下来只需要使用 npm 即可完成 Hexo 的安装。

1
$ npm install -g hexo-cli

Mac 用户
您在编译时可能会遇到问题,请先到 App Store 安装 Xcode,Xcode 完成后,启动并进入 Preferences -> Download -> Command Line Tools -> Install安装命令行工具。

安装 Git

  • Windows:下载并安装 git.
  • Mac:下载 安装程序 安装。
  • Linux (Ubuntu, Debian): sudo apt-get install git-core
  • Linux (Fedora, Red Hat, CentOS): sudo yum install git-core

安装 Node.js

Node.JS
下载 安装程序 来安装。

安装 Hexo

所有必备的应用程序安装完成后,即可使用 npm 安装 Hexo。

1
$ npm install -g hexo-cli

Hexo初始化配置

创建Hexo文件夹

安装完成后,根据自己喜好建立目录(如E:\blog),进入Git Shell切换到该路径下E:\blog执行以下指令:

  • 初始化hexo
    1
    2
    hexo init
    npm install hexo --save

生成静态页面

  • 生成静态页面至hexo\public\目录
    hexo g (或者 hexo generate)
    

    本地查看效果

  • 本地启动服务
    hexo server
    
    这样,我们就可以在浏览器中输入http://localhost:4000/ 访问我们的博客啦(响应式的网站)。

部署静态网页到GitHub

注册并设置GitHub

  1. 登录GitHub,注册自定义用户名如puhemo
  2. 在主页右下角创建New repository,name必须和用户名一致如puhemo.github.io

同步内容至GitHub

  1. 编辑_config.yml文件,建立与git的关联(在.yml文件的最底部)

    # Deployment
    ## Docs: http://hexo.io/docs/deployment.html
    deploy:
      type: git
      repository: https://github.com/puhemo/puhemo.github.io
      branch: master
    
  2. 然后运行

        npm install hexo-deployer-git --save  
        hexo g
        hexo d
    这样你就可以在你的 puhemo.github.io 上看到代码已经同步到git上了。
    在浏览器中输入你的**.github.io(例如:http://puhemo.github.io/)
    

    每次有新的修改需要部署同步,都可以按照下面的步骤来:

    hexo clean  
    hexo g
    hexo d     
    

域名和DNS

域名推荐

DNS推荐

致力于为您提供最稳定、最安全的域名解析服务
https://www.dnspod.cn/

设置CNAME

  1. 创建CNAME文件并添加到/source目录下
  2. 填写自己的域名如hellodog.com,保存结束
  3. 同步至Github: hexo g -d
    hexo会自动把CNAME复制到/puclic目录下然后将/public路径下的内容进行复制并push到远程master分支的根目录下。

    如果是直接在GitHub网页上添加文件的话,会遇到一个问题就是在通过hexo g -d之后hexo会把根目录下的CNAME文件删除。

添加DNS Service记录,下面以dnspod为例:

添加两个A,主机记录写@,后面记录值写上
192.30.252.153
192.30.252.154

顶级域名适用

设置域名的DNS

在相应域名的DNS Service中添加上图中间的两条记录:
f1g1ns1.dnspod.net和f1g1ns2.dnspod.net(以dnspod为例)

新修改域名DNS指向DNSPod的DNS域名,虽DNSPod服务器的生效时间是实时的,但因各地ISP服务商刷新域名DNS的时间不一致,所以导致解析在全球生效一般需要0–72小时哦,请您耐心的等待


Hexo配置修改

整站设置

# Hexo Configuration
## Docs: http://hexo.io/docs/configuration.html
## Source: https://github.com/tommy351/hexo/

# Site #整站的基本信息
title: 1000 words a Day #网站标题
subtitle: Writing 1000 Words a Day Changes My Life #网站副标题
description: 学习总结 思考感悟 知识管理 #网站描述
author:  puhemo #网站作者,在下方显示
language: zh-Hans #网站语言

# URL
## If your site is put in a subdirectory
url: puhemo.tk #你的域名
root: /
permalink: :year/:month/:day/:title/
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code

# Directory
source_dir: source
public_dir: public

# Writing
new_post_name: :title.md # File name of new posts
default_layout: post
auto_spacing: false # Add spaces between asian characters and western characters
titlecase: false # Transform title into titlecase
external_link: true # Open external links in new tab
max_open_file: 100
multi_thread: true
filename_case: 0
render_drafts: false
post_asset_folder: false
highlight:
  enable: true
  line_number: true
  tab_replace:

# Category & Tag
default_category: uncategorized
category_map:
tag_map:

# Archives
## 2: Enable pagination
## 1: Disable pagination
## 0: Fully Disable
archive: 2
category: 2
tag: 2

# Server
## Hexo uses Connect as a server
## You can customize the logger format as defined in
## http://www.senchalabs.org/connect/logger.html
port: 4000
server_ip: 0.0.0.0
logger: false
logger_format:

# 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: H:mm:ss

# Pagination
## Set per_page to 0 to disable pagination
per_page: 15 #每页15篇文章
pagination_dir: page

# Disqus #社会化评论disqus,我使用多说,在主题中配置
disqus_shortname:

# Extensions
## Plugins: https://github.com/tommy351/hexo/wiki/Plugins
## Themes: https://github.com/tommy351/hexo/wiki/Themes
theme: next #主题设置
exclude_generator:
Plugins:
- hexo-generator-feed
- hexo-generator-sitemap

#sitemap
sitemap:
  path: sitemap.xml

#Feed Atom
feed:
  type: atom
  path: atom.xml
  limit: 20

# Markdown
## https://github.com/chjj/marked
markdown:
  gfm: true
  pedantic: false
  sanitize: false
  tables: true
  breaks: true
  smartLists: true
  smartypants: true

# Stylus
stylus:
      compress: false

# Deployment
## Docs: http://hexo.io/docs/deployment.html
deploy:
  type: github
  repository: https://github.com/puhemo/puhemo.github.io.git
  branch: master

修改局部页面

页面展现的全部逻辑都在每个主题中控制,源代码在\blog\themes\jacman(以jacman主题为例)

├── languages  #多语言
|   ├── default.yml#默认语言
|   └── zh-CN.yml  #中文语言
├── layout #布局,根目录下的*.ejs文件是对主页,分页,存档等的控制
|   ├── _partial   #局部的布局,此目录下的*.ejs是对头尾等局部的控制
|   └── _widget#小挂件的布局,页面下方小挂件的控制
├── source #源码
|   ├── css#css源码 
|   |   ├── _base  #*.styl基础css
|   |   ├── _partial   #*.styl局部css
|   |   ├── fonts  #字体
|   |   ├── images #图片
|   |   └── style.styl #*.styl引入需要的css源码
|   ├── fancybox   #fancybox效果源码
|   └── js #javascript源代码
├── _config.yml#主题配置文件
└── README.md  #说明文档

如何设置「阅读全文」?

在文章中使用 <!-- more --> 手动进行截断,Hexo 提供的方式

设置 RSS

  1. 添加feed插件: $ npm install hexo-generator-feed --save
  2. 自定义feed插件:
    修改_config.yml,增加以下内容

    #Feed Atom
    feed:
      type: atom
      path: atom.xml
      limit: 20
    

    type - Feed type. (atom/rss2)
    path - Feed path. (Default: atom.xml/rss2.xml)
    limit - Maximum number of posts in the feed (Use 0 or false to show all posts)
    hub - URL of the PubSubHubbub hubs (Leave it empty if you don’t use it)

分类和标签

只有文章支持分类和标签,您可以在 Front-matter 中设置。在其他系统中,分类和标签听起来很接近,但是在 Hexo 中两者有着明显的差别:分类具有顺序性和层次性,也就是说 Foo, Bar 不等于 Bar, Foo;而标签没有顺序和层次。
categories:

- Diary
tags:
- PS3
- Games

更多内容参考官方Front-matter说明

添加「标签」页面

  1. 新建页面:
    $ cd your-hexo-site
    $ hexo new page tags
    
  2. 设置页面类型:
    title: 标签
    date: 2014-12-22 12:39:04
    type: "tags"
    ---
    
  3. 修改菜单:
    在菜单中添加链接。编辑主题配置文件,添加 tags 到 menu 中,如下:
    menu:
    home: /
    archives: /archives
    tags: /tags
    

注意:如果有启用 多说 或者 Disqus 评论,页面也会带有评论。 若需要关闭的话,请添加字段 comments 并将值设置为 false,如:

title: 标签
date: 2014-12-22 12:39:04
type: "tags"
comments: false
---

添加「分类」页面

  1. 新建页面:
    $ cd your-hexo-site
    $ hexo new page categories
    
  2. 设置页面类型:
    title: 分类
    date: 2014-12-22 12:39:04
    type: "categories"
    ---
    
  3. 修改菜单:
    在菜单中添加链接。编辑主题配置文件,添加 categories 到 menu 中,如下:
    menu:
    home: /
    archives: /archives
    categories : /categories    
    

注意:如果有启用 多说 或者 Disqus 评论,页面也会带有评论。 若需要关闭的话,请添加字段 comments 并将值设置为 false,如:

title: 分类
date: 2014-12-22 12:39:04
type: "categories"
comments: false
---

设置头像

编辑站点配置文件,新增字段avatar,值设置成头像的链接地址。其中,头像的链接地址可以是:

  1. 完整的互联网 URI: http://example.com/avtar.png
  2. 站点内的地址:
  • 将头像放置主题目录下的 source/uploads/ (新建uploads目录若不存在)
    配置为:avatar: /uploads/avatar.png
  • 或者 放置在 source/images/ 目录下
    配置为:avatar: /images/avatar.png

Google 分析

编辑站点配置文件,新增字段google_analytics,值设置成泥的Google跟踪ID。跟踪ID通常以UA-开头。
ID

Hexo主题修改

  • 在git上找到你想要的主题
    #示例:Nest
    https://github.com/iissnan/hexo-theme-next
    
  • 进入你的hexo目录(如E:\blog),执行命令,拷贝主题
    git clone https://github.com/iissnan/hexo-theme-next themes/next
    
  • 拷贝完成后,你会发现你的项目下的themes下多了一个next文件夹
  • 我们还需要修改_config.yml文件中的一处来应用新的主题
    # Extensions
    ## Plugins: http://hexo.io/plugins/
    ## Themes: http://hexo.io/themes/
    theme: next
    
  • 然后我们重启服务就可以在本地看到效果了
    hexo server
    
  • 同步到github
    hexo clean  
    hexo g
    hexo d     
    

Hexo主题推荐


Hexo命令

常用命令:

hexo new "postName" #生成文章,或者source\_posts手动编辑
hexo new page "pageName" #新建页面
hexo generate #生成静态页面至public目录
hexo server #开启预览访问端口(默认端口4000,'ctrl + c'关闭server)
hexo deploy #将.deploy目录部署到GitHub

常用复合命令:

hexo d -g #生成加部署
hexo s -g #预览加部署

简写命令:

hexo n #hexo new
hexo g #hexo generate
hexo s #hexo server
hexo d #hexo deploy

常见问题

  1. 修改配置文件时注意YAML语法,参数冒号:后一定要留空格
  2. 中文乱码请修改文件编码格式为UTF-8
  3. Hexo常见问题解决方案

图床

推荐使用七牛,还可以使用imgur(国内不稳定)


推荐阅读


参考链接

文章目录
  1. 1. 前言
  2. 2. 更新历史
  3. 3. 准备工作
    1. 3.1. 什么是 Hexo?
    2. 3.2. 安装前提
    3. 3.3. 安装 Git
    4. 3.4. 安装 Node.js
    5. 3.5. 安装 Hexo
  4. 4. Hexo初始化配置
    1. 4.1. 创建Hexo文件夹
    2. 4.2. 生成静态页面
    3. 4.3. 本地查看效果
  5. 5. 部署静态网页到GitHub
    1. 5.1. 注册并设置GitHub
    2. 5.2. 同步内容至GitHub
  6. 6. 域名和DNS
    1. 6.1. 域名推荐
    2. 6.2. DNS推荐
    3. 6.3. 设置CNAME
    4. 6.4. 添加DNS Service记录,下面以dnspod为例:
    5. 6.5. 设置域名的DNS
  7. 7. Hexo配置修改
    1. 7.1. 整站设置
    2. 7.2. 修改局部页面
    3. 7.3. 如何设置「阅读全文」?
    4. 7.4. 设置 RSS
    5. 7.5. 分类和标签
    6. 7.6. 添加「标签」页面
    7. 7.7. 添加「分类」页面
    8. 7.8. 设置头像
    9. 7.9. Google 分析
    10. 7.10. Hexo主题修改
    11. 7.11. Hexo主题推荐
  8. 8. Hexo命令
    1. 8.1. 常用命令:
    2. 8.2. 常用复合命令:
    3. 8.3. 简写命令:
  9. 9. 常见问题
  10. 10. 图床
  11. 11. 推荐阅读
  12. 12. 参考链接
,