Overview
Welcome to my blog! In this my very first post, I will record my experience of building this blog with the help of Github Pages and the amazing Hexo! This not only keeps a record of what I did, but also may help those who want to build a personal blog site.
Static sites can be hosted anywhere and are fast to serve and easy to cache. Github Pages offers a very fast way to host your site and Hexo is a static blogging framework built on Node.js. Using Hexo, you can publish Markdown documents in the form of blog posts. Blog posts and content are processed and converted into HTML/CSS, which is sourced from the default or custom template theme files. All of the software in Hexo is modular, so you can install and set up exactly what you need.
Ok, let’s get down to the business.
Install
Installing Hexo is quite easy. However, you do need to have a couple of other things installed first:
Git
Node.js
If your computer already has these, congratulations! Just install Hexo with npm:
$ npm install -g hexo-cli
If not, please follow the following instructions to install all the requirements.
Git
Windows: Download & install Git.
Mac: Install it with Homebrew, MacPorts or installer.
Linux (Ubuntu, Debian): sudo apt-get install git-core
Linux (Fedora, Red Hat, CentOS): sudo yum install git-core
Node.js
The best way to install Node.js is with Node Version Manager.
To install or update nvm, you can use the install script using cURL:
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
or Wget:
$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
Once nvm is installed, restart the terminal and run the following command to install Node.js:
$ nvm install stable
Alternatively, download and run the installer.
Hexo
Once all the requirements are installed, you can install Hexo with npm:
$ npm install -g hexo-cli
Strongly recommend you to read Hexo’s Docs. before setting up your own blog, since the official documentation is written in detial that you can follow it step-by-step. Here I will just write down some essential setting.
Initialize
Once Hexo is installed, run the following commands to initialise Hexo in the target <folder>
.
1 | $ hexo init <folder> |
If <folder>
is not specified, Hexo will create the project in the current working directory.
Writing
$ hexo new post [your-post-title]
Will find a new post is created in ../source/_posts
. All posts created by hexo is written in Markdown.
Generating
$ hexo generate
This will create a /public folder in the root of your project, and it contains all static html, css, js, fonts, images, etc.
Server
$ hexo server
Your website will run at http://localhost:4000 by default. When the server is running, Hexo will watch for file changes and update automatically so it’s not necessary to manually restart the server.
Deployment
Hexo provides many ways to deploy the website to your real server. You only need one single command to deploy your site to your servers.
$ npm install hexo-deployer-git --save
$ hexo deploy
Before your first deployment, you will have to modify some settings in _config.yml
. A valid deployment setting must have a type
field. For example:
1 | deploy: |
You can use multiple deployers. Hexo will execute each deployer in order.
1 | deploy: |
Git
See Git Pages for full guideline.
Create a repository
Head over to GitHub and create a new repository named username.github.io
, where username
is your username (or organization name) on GitHub.
If the first part of the repository doesn’t exactly match your username, it won’t work, so make sure to get it right.
Next, deploy a SSH key to this repository.
Git deploy strategy for Hexo
Install hexo-deployer-git.
$ npm install hexo-deployer-git --save
After that, edit deploy config in your _config.yml (located in project root).
1 | deploy: |
Then, deploy website to git repository
$ hexo deploy
Finally! Browse https://[your-github-username].github.io/ and enjoy your flight with Hexo!
Netlify
Netlify provides continuous deployment (Git-triggered builds), an intelligent global CDN, full DNS (including custom domains), automated HTTPS, asset acceleration, and a lot more. It is a unified platform that automates your code to create high-performant, easily maintainable sites and web apps.
There are two different ways to deploy your sites on Netlify. The most common way is to use the web UI. Go to the create a new site page, select your project repo from GitHub, GitLab, or Bitbucket, and follow the prompts.
Alternatively, you can use Netlify’s Node based CLI tool to manage and deploy sites on Netlify without leaving your terminal.
You can also add a Deploy to Netlify Button in your README.file to allow others to create a copy of your repository and be deployed to Netlify via one click.
Theme Settings
Hexo lets you define your site information, customize date/time format, pagination behaviour, language settings, etc. Most configs are not necessary, but it will make your site look more like yours if you specify them properly. You can modify most site settings in :_config.yml
or in an alternate config file.
NexT is the theme now I using, one of the most popular themes for Hexo, so it has much more customization features and plugins. And the following setting will be based on NexT. Certainly, we should read NexT Docs first.
Documentation Variables
There are two main configuration files using by Hexo and both called _config.yml:
The first one is under site root directory, which contains Hexo’s config.
The second one is under theme root directory, which is provided by NexT and contains theme’s config.
Let’s call the first one – site config file, and the second one – theme config file.
Colorfy
Body Background
Add code to theme/next/source/css/_custom/custom.styl
to modify the background.
1 | body{ |
Text Color
To make sure the text colormatch well with the background color, we may need to modify it. Just open theme/next/source/css/_common/scaffolding/base.styl
, find out body{}
, then change the color:$text-color
as you want.
1 | body { |
Code Block Style
Inline Code
1 | + #posts code {color: <color code>;background-color: <color code>;} |
Header & Footer
Background
1 | .header { |
You can also add background:<url>;
to set a specific background picture.
Text Color
Site-title
Open theme/next/source/css/_common/components/header/site-meta.styl
, find out .brand{}
, and add code:
1 | + color: <color code>; |
Menu
Open menu.styl
at the same folder, find out a{}
, add code:
1 | + color: <color code>; |
Footer Text
1 | + .footer-inner {color: <color code>;} |
Sidebar Color
1 | #sidebar { |
Layout
Page Width
1 | .main-inner {width: <page width>;} |
###Center Layout
Title
1 | - .post-title, |
Meta
1 | - $site-meta-text-align = left; |
Content
1 | .posts-expand .post-body { |
Read-More
1 | .post-button { |
Pagination
1 | //Center page number |
Tags
1 | .posts-expand .post-body { |
Site Footer
1 | //Center footer |
Customization
Sidebar
Siderbar avatat animation
-
Open
themes\next\layout\_partials\head.swig
, add code:themes\next\layout\_partials\head.swig 1
+ <link href="//cdn.bootcss.com/animate.css/3.5.0/animate.min.css" rel="stylesheet">
-
Add code in
themes\next\source\css\_common\components\sidebar\sidebar-author.styl
1
2
3
4.site-author-image:hover {
-webkit-animation: pulse 1s;
animation: pulse 1s;
};Here take
pulse
for example, you can find more animations at Animate.css as well.
Siderbar avatat Hyperlink
1 | // Click the sidebar avatar to return to homepage. |
Sidebar hover to display
-
Add following new
<div>
attheme/next/layout/_macro/sideebar.swig
before section<div class="links-of-author motion-element">
:1
2
3
4
5
6
7
8
9
10
11
12<!--my custom code begin-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/velocity/1.5.0/velocity.min.js"></script>
<script type="text/javascript">
$("#sidebar").hover(function(){
$("#mydivshow").velocity('stop').velocity({opacity: 1});
},function(){
$("#mydivshow").velocity('stop').velocity({opacity: 0});
});
</script>
<div id="mydivshow" class="mydivshow">
<!--my custom code end--> -
End
<div>
before section<div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
.1
2
3
4<!--my custom code begin-->
</div>
<!--my custom code end-->
{% if display_toc and toc(page.content).length > 1 %}Form
<div id="mydivshow" class="mydivshow">
to</div>
is the triggered area, hence you can insert them at any section as you want. -
Initialize setting
Add following code attheme/next/source/css/_custom/custom.styl
:1
#mydivshow{opacity: 0;}
Hyperlink Style
Add code in themes\next\source\css\_common\components\post\post.styl
1 | .post-body p a{ |
404 Page
Dos 404
Custom simple 404
Create new file 404.md
in Hexo/source
1 | <center> |
Hide and Encrypt Post
hexo-hide-posts
https://github.com/prinsss/hexo-hide-posts
- Install
npm install hexo-hide-posts --save
- Config
1 | # hexo-hide-posts |
e.g. Set filter to secret, so you can use secret: true in front-matter instead.
Note: although most of generator plugins respect a naming convention that they register generator with the name in their package names, the generator name could be arbitrary. For example, hexo-generator-searchdb does not register generators with name searchdb, but xml and json. For accurate generator name, you should check their source code.
hexo-blog-encrypt
https://github.com/D0n9X1n/hexo-blog-encrypt
-
Install
npm install --save hexo-blog-encrypt
-
Encrypt TOC
If you has a post with TOC, you should change the code of your template. Take the default theme ‘NexT’ as an example, editthemes/next/layout/_macro/sidebar.swig
with the following code :1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<!--noindex-->
<div class="post-toc-wrap sidebar-panel">
{%- if (page.encrypt) %}
<div id="toc-div" style="display:none">
{%- else %}
<div id="toc-div">
{%- endif %}
{%- if display_toc %}
<div class="post-toc motion-element">{{ toc }}</div>
{%- endif %}
</div>
</div>
<!--/noindex-->Reference
Outdated
-
Add code:
1
…
... //Setup passowrd for the post. +1 | 2. Setting scaffolds: |
1 | // Add post_meta |
1 | // hide powered by valine |
Dynamic Title
Cratedytitle.js
inthemes/source/js
1 | /* 离开当前页面时修改网页标题,回到当前页面时恢复原来标题 */ |
Add code in layout/_layout.swig
1 | <script src="https://cdn.jsdelivr.net/gh/weilain/cdn-photo/js/jquery.min.js"></script> |
Post Pages Label
Replace the tags label #
with at the bottom of the post pages.
- Open
../themes/next/layout/_macro/post.swig
- Search
rel="tag">#
- Replace
#
with<i class="fa fa-tag"></i>
Third Party Services
##Local Search
Local search no need any external 3rd-party services and can be extra indexed by search engines. That search method recommended for most users.
Install hexo-generator-searchdb by run following command in site root dir:
1 | $ npm install hexo-generator-searchdb --save |
Edit site config file and add following content:
1 | search: |
Edit theme config file to enable Local Search:
1 |
|
Sitemap
The Sitemaps protocol allows a webmaster to inform search engines about URLs on a website that are available for crawling. A Sitemap is an XML file that lists the URLs for a site. It allows webmasters to include additional information about each URL: when it was last updated, how often it changes, and how important it is in relation to other URLs in the site. This allows search engines to crawl the site more efficiently and to find URLs that may be isolated from rest of the site’s content.
Google Sitemap:
$ npm install hexo-generator-sitemap --save
Baidu Sitemap:
$ npm install hexo-generator-baidu-sitemap --save
Edit Site _config.yml:
1 | sitemap: |
Tag Plugins
Go to Hexo Theme Next Test for more detial.
Centered Quote
1 | {% centerquote %}Something{% endcenterquote %} |
Something
Something
Include Raw
This tag include any raw content into your posts. Path is relative to your site source directory.
Usage
1 | {% include_raw '_data/path/to/file.html' %} |
Example
Let’s create include-raw.html file in _data directory under site root directory with following content:
1 | Any <strong>raw content</strong> may be included with this . |
Then in any post we can use this content with include_raw tag:
1 | {% include_raw '_data/path/to/include-raw.html' %} |
Any raw content may be included with this tag.
Note Tag
Edit Themes config file to change Note tag style
1 | note: |
For emample:
1 | {% note %} |
Header
(without define class style)
Default Header
Default Text
Primary Header
Primary Text
Info Header
Info Text
Success Header
Success Text
Warning Header
Warning Text
Danger Header
Danger Text
Label Tag
1 | {% label default@default %} |
Button Tag
Tab TagNexT Supported Tags - Button
1 | {% button url, text, icon [class], [title] %} |
Tab Tag
Button TagNexT Supported Tags - Tab
1 | {% tabs Unique name, [index] %} |
Full Image (Featured Image)
1 | {% fullimage /url [@lazy], [alt], [title], [size] %} |
Auto Center Image
1 | // Auto Center Image |
Reference:
NexT User Docs
Hexo