Hexo搭建博客教程

現在越來越多的人喜歡利用Github搭建靜態網站,原因不外乎簡單省錢。本人也利用hexo+github搭建了本博客,用於分享一些心得。在此過程中,折騰博客的各種配置以及功能佔具了我一部分時間,在此詳細記錄下我是如何利用hexo+github搭建靜態博客以及一些配置相關問題,以免過後遺忘,且當備份之用。

 

準備工作

  • 下載node.js並安裝(官網下載安裝),默認會安裝npm。
  • 下載安裝git(官網下載安裝)
  • 下載安裝hexo。方法:打開cmd 運行npm install -g hexo(要翻牆)

本地搭建hexo靜態博客

  • 新建一個文件夾,如MyBlog
  • 進入該文件夾內,右擊運行git,輸入:hexo init(生成hexo模板,可能要翻牆)
  • 生成完模板,運行npm install(目前貌似不用運行這一步)
  • 最後運行:hexo server (運行程序,訪問本地localhost:4000可以看到博客已經搭建成功)

將博客與Github關聯

  • 在Github上創建名字爲XXX.github.io的項目,XXX爲自己的github用戶名。

  • 打開本地的MyBlog文件夾項目內的_config.yml配置文件,將其中的type設置爲git


 

1

2

3

4


 

deploy:

type: git

repository: https://github.com/tengzhangchao/tengzhangchao.github.io.git

branch: master

  • 運行:npm install hexo-deployer-git –save
  • 運行:hexo g(本地生成靜態文件)
  • 運行:hexo d(將本地靜態文件推送至Github)

此時,打開瀏覽器,訪問http://tengzhangchao.github.io

綁定域名

  博客已經搭建好,也能通過github的域名訪問,但總歸還是用自己的域名比較舒服。因爲我們需要設置將自己的域名綁定到github這個博客項目上。

  • 域名提供商設置

    添加2條A記錄:

    @—>192.30.252.154

    @—>192.30.252.153

    添加一條CNAME記錄:

    CNAME—>tengzhangchao.github.io

  • 博客添加CNAME文件

    配置完域名解析後,進入博客目錄,在source目錄下新建CNAME文件,寫入域名,如:thief.one

  • 運行: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)

添加菜單

進入theme目錄,編輯_config_yml文件,找到menu:字段,在該字段下添加一個字段。


 

1

2

3

4


 

menu:

home: /

about: /about

......

然後找到lanhuages目錄,編輯zh-Hans.yml文件:


 

1

2

3

4


 

menu:

home: 首頁

about: 關於作者

......

更新頁面顯示的中文字符,最後進入theme目錄下的Source目錄,新增一個about目錄,裏面寫一個index.html文件。

文章內插入圖片

在文章中寫入:


 

1


 

![](/upload_image/1.jpg)

  然後進入themes-主題名-source-upload_image目錄下(自己創建),將圖片放到這個目錄下,就可以了。

說明:當執行hexo g命令時,會自動把圖片複製到 public文件的upload_image目錄下。

個性化設置

基本信息

  在根目錄下的_config.yml文件中,可以修改標題,作者等信息。打開編輯該文件,注意:每一個值的冒號後面都有一個半角空格!

  • 未生效的寫法:title:nMask的博客
  • 能生效的寫法:title:[空格]nMask的博客

主題

訪問主題列表,獲取主題代碼。

進入themes目錄,進入以下操作:

  • 下載主題 (以next主題爲例)

    
     

    1

    
     

    git clone https://github.com/iissnan/hexo-theme-next.git(主題的地址)

  • 打開__config.yml文件,將themes修改爲next(下載到的主題文件夾的名字)

  • hexo g
  • hexo d

關於hexo-next主題下的一些個性化配置,參考:Next主題配置

主題美化

文章中添加居中模塊

文章Markdown中填寫如下:


 

1


 

<blockquote class="blockquote-center">優秀的人,不是不合羣,而是他們合羣的人裏面沒有你</blockquote>

 

在文章底部增加版權信息

在目錄 next/layout/_macro/下添加 my-copyright.swig:


 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32


 

{% if page.copyright %}

<div class="my_post_copyright">

<script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>

<!-- JS庫 sweetalert 可修改路徑 -->

<script type="text/javascript" src="http://jslibs.wuxubj.cn/sweetalert_mini/jquery-1.7.1.min.js"></script>

<script src="http://jslibs.wuxubj.cn/sweetalert_mini/sweetalert.min.js"></script>

<link rel="stylesheet" type="text/css" href="http://jslibs.wuxubj.cn/sweetalert_mini/sweetalert.mini.css">

<p><span>本文標題:</span><a href="{{ url_for(page.path) }}">{{ page.title }}</a></p>

<p><span>文章作者:</span><a href="/" title="訪問 {{ theme.author }} 的個人博客">{{ theme.author }}</a></p>

<p><span>發佈時間:</span>{{ page.date.format("YYYY年MM月DD日 - HH:MM") }}</p>

<p><span>最後更新:</span>{{ page.updated.format("YYYY年MM月DD日 - HH:MM") }}</p>

<p><span>原始鏈接:</span><a href="{{ url_for(page.path) }}" title="{{ page.title }}">{{ page.permalink }}</a>

<span class="copy-path" title="點擊複製文章鏈接"><i class="fa fa-clipboard" data-clipboard-text="{{ page.permalink }}" aria-label="複製成功!"></i></span>

</p>

<p><span>許可協議:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商業性使用-禁止演繹 4.0 國際</a> 轉載請保留原文鏈接及作者。</p>

</div>

<script>

var clipboard = new Clipboard('.fa-clipboard');

clipboard.on('success', $(function(){

$(".fa-clipboard").click(function(){

swal({

title: "",

text: '複製成功',

html: false,

timer: 500,

showConfirmButton: false

});

});

}));

</script>

{% endif %}

 

在目錄next/source/css/_common/components/post/下添加my-post-copyright.styl:


 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45


 

.my_post_copyright {

width: 85%;

max-width: 45em;

margin: 2.8em auto 0;

padding: 0.5em 1.0em;

border: 1px solid #d3d3d3;

font-size: 0.93rem;

line-height: 1.6em;

word-break: break-all;

background: rgba(255,255,255,0.4);

}

.my_post_copyright p{margin:0;}

.my_post_copyright span {

display: inline-block;

width: 5.2em;

color: #b5b5b5;

font-weight: bold;

}

.my_post_copyright .raw {

margin-left: 1em;

width: 5em;

}

.my_post_copyright a {

color: #808080;

border-bottom:0;

}

.my_post_copyright a:hover {

color: #a3d2a3;

text-decoration: underline;

}

.my_post_copyright:hover .fa-clipboard {

color: #000;

}

.my_post_copyright .post-url:hover {

font-weight: normal;

}

.my_post_copyright .copy-path {

margin-left: 1em;

width: 1em;

+mobile(){display:none;}

}

.my_post_copyright .copy-path:hover {

color: #808080;

cursor: pointer;

}

 

修改next/layout/_macro/post.swig,在代碼


 

1

2

3

4

5


 

<div>

{% if not is_index %}

{% include 'wechat-subscriber.swig' %}

{% endif %}

</div>

 

之前添加增加如下代碼:


 

1

2

3

4

5


 

<div>

{% if not is_index %}

{% include 'my-copyright.swig' %}

{% endif %}

</div>

 

修改next/source/css/_common/components/post/post.styl文件,在最後一行增加代碼:


 

1


 

@import "my-post-copyright"

 

如果要在該博文下面增加版權信息的顯示,需要在 Markdown 中增加copyright: true的設置,類似:


 

1

2

3

4

5

6

7


 

---

title:

date:

tags:

categories:

copyright: true

---

 

自定義hexo new生成md文件的選項

在/scaffolds/post.md文件中添加:


 

1

2

3

4

5

6

7

8

9

10


 

---

title: {{ title }}

date: {{ date }}

tags:

categories:

copyright: true

permalink: 01

top: 0

password:

---

 

隱藏網頁底部powered By Hexo / 強力驅動

打開themes/next/layout/_partials/footer.swig,使用””隱藏之間的代碼即可,或者直接刪除。


 

1

2

3

4

5

6

7

8

9

10

11

12


 

<!--

<div class="powered-by">

{{ __('footer.powered', '<a class="theme-link" rel="external nofollow" href="https://hexo.io">Hexo</a>') }}

</div>

<div class="theme-info">

{{ __('footer.theme') }} -

<a class="theme-link" rel="external nofollow" href="https://github.com/iissnan/hexo-theme-next">

NexT.{{ theme.scheme }}

</a>

</div>

-->

 

文章加密訪問

打開themes->next->layout->_partials->head.swig文件,在meta標籤後面插入這樣一段代碼:


 

1

2

3

4

5

6

7

8

9

10


 

<script>

(function(){

if('{{ page.password }}'){

if (prompt('請輸入文章密碼') !== '{{ page.password }}'){

alert('密碼錯誤!');

history.back();

}

}

})();

</script>

 

然後文章中添加:


 

1


 

password: nmask

 

如果password後面爲空,則表示不用密碼。

博文置頂

修改 hero-generator-index 插件,把文件:node_modules/hexo-generator-index/lib/generator.js 內的代碼替換爲:


 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28


 

'use strict';

var pagination = require('hexo-pagination');

module.exports = function(locals){

var config = this.config;

var posts = locals.posts;

posts.data = posts.data.sort(function(a, b) {

if(a.top && b.top) { // 兩篇文章top都有定義

if(a.top == b.top) return b.date - a.date; // 若top值一樣則按照文章日期降序排

else return b.top - a.top; // 否則按照top值降序排

}

else if(a.top && !b.top) { // 以下是隻有一篇文章top有定義,那麼將有top的排在前面(這裏用異或操作居然不行233)

return -1;

}

else if(!a.top && b.top) {

return 1;

}

else return b.date - a.date; // 都沒定義按照文章日期降序排

});

var paginationDir = config.pagination_dir || 'page';

return pagination('', posts, {

perPage: config.index_generator.per_page,

layout: ['index', 'archive'],

format: paginationDir + '/%d/',

data: {

__index: true

}

});

};

 

在文章中添加 top 值,數值越大文章越靠前,如:


 

1

2

3

4

5


 

---

......

copyright: true

top: 100

---

 

默認不設置則爲0,數值相同時按時間排序。

添加頂部加載條

打開/themes/next/layout/_partials/head.swig文件,在maximum-scale=1”/>後添加如下代碼:


 

1

2


 

<script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>

<link href="//cdn.bootcss.com/pace/1.0.2/themes/pink/pace-theme-flash.css" rel="stylesheet">

 

但是,默認的是粉色的,要改變顏色可以在/themes/next/layout/_partials/head.swig文件中添加如下代碼(接在剛纔link的後面)


 

1

2

3

4

5

6

7

8

9

10

11

12

13


 

<style>

.pace .pace-progress {

background: #1E92FB; /*進度條顏色*/

height: 3px;

}

.pace .pace-progress-inner {

box-shadow: 0 0 10px #1E92FB, 0 0 5px #1E92FB; /*陰影顏色*/

}

.pace .pace-activity {

border-top-color: #1E92FB; /*上邊框顏色*/

border-left-color: #1E92FB; /*左邊框顏色*/

}

</style>

 

添加熱度

next主題集成leanCloud,打開/themes/next/layout/_macro/post.swig
在”leancloud-visitors-count”>標籤後面添加℃。
然後打開,/themes/next/languages/zh-Hans.yml,將visitors內容改爲熱度即可。

主頁文章添加陰影效果

打開\themes\next\source\css_custom\custom.styl,向裏面加入:


 

1

2

3

4

5

6

7

8


 

// 主頁文章添加陰影效果

.post {

margin-top: 60px;

margin-bottom: 60px;

padding: 25px;

-webkit-box-shadow: 0 0 5px rgba(202, 203, 203, .5);

-moz-box-shadow: 0 0 5px rgba(202, 203, 204, .5);

}

 

修改文章底部的那個帶#號的標籤

修改模板/themes/next/layout/_macro/post.swig,搜索 rel=”tag”>#,將 # 換成

鼠標點擊小紅心的設置

將 love.js 文件添加到 \themes\next\source\js\src 文件目錄下。
找到 \themes\next\layout_layout.swing 文件, 在文件的後面, 標籤之前 添加以下代碼:


 

1

2


 

<!-- 頁面點擊小紅心 -->

<script type="text/javascript" src="/js/src/love.js"></script>

 

背景的設置

將 particle.js 文件添加到 \themes\next\source\js\src 文件目錄下。
找到 \themes\next\layout_layout.swing 文件, 在文件的後面,標籤之前 添加以下代碼:


 

1

2


 

<!-- 背景動畫 -->

<script type="text/javascript" src="/js/src/particle.js"></script>

 

修改文章內鏈接文本樣式

將鏈接文本設置爲藍色,鼠標劃過時文字顏色加深,並顯示下劃線。
找到文件 themes\next\source\css\_custom\custom.styl ,添加如下 css 樣式:


 

1

2

3

4

5

6

7

8


 

.post-body p a {

color: #0593d3;

border-bottom: none;

&:hover {

color: #0477ab;

text-decoration: underline;

}

}

 

博文壓縮

在站點的根目錄下執行以下命令:


 

1

2


 

$ npm install gulp -g

$ npm install gulp-minify-css gulp-uglify gulp-htmlmin gulp-htmlclean gulp --save

 

在博客根目錄下新建 gulpfile.js ,並填入以下內容:


 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33


 

var gulp = require('gulp');

var minifycss = require('gulp-minify-css');

var uglify = require('gulp-uglify');

var htmlmin = require('gulp-htmlmin');

var htmlclean = require('gulp-htmlclean');

// 壓縮 public 目錄 css

gulp.task('minify-css', function() {

return gulp.src('./public/**/*.css')

.pipe(minifycss())

.pipe(gulp.dest('./public'));

});

// 壓縮 public 目錄 html

gulp.task('minify-html', function() {

return gulp.src('./public/**/*.html')

.pipe(htmlclean())

.pipe(htmlmin({

removeComments: true,

minifyJS: true,

minifyCSS: true,

minifyURLs: true,

}))

.pipe(gulp.dest('./public'))

});

// 壓縮 public/js 目錄 js

gulp.task('minify-js', function() {

return gulp.src('./public/**/*.js')

.pipe(uglify())

.pipe(gulp.dest('./public'));

});

// 執行 gulp 命令時執行的任務

gulp.task('default', [

'minify-html','minify-css','minify-js'

]);

 

生成博文是執行 hexo g && gulp 就會根據 gulpfile.js 中的配置,對 public 目錄中的靜態資源文件進行壓縮。

搜索功能

安裝 hexo-generator-searchdb,在站點的根目錄下執行以下命令:


 

1


 

$ npm install hexo-generator-searchdb --save

 

編輯 站點配置文件,新增以下內容到任意位置:


 

1

2

3

4

5


 

search:

path: search.xml

field: post

format: html

limit: 10000

 

增加閱讀排行統計頁面

首先我們可以使用leancloud來統計頁面閱讀數量,以及儲存這些信息,然後通過leancloud提供的api編寫js腳本來獲取閱讀數量信息,並展示在頁面上。
首先新建一個page頁面,hexo new page “”,然後編輯此.md文件,寫下:


 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26


 

<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>

<script>AV.initialize("", "");</script> //需要寫上leancloud的key

<script type="text/javascript">

var time=0

var title=""

var url=""

var query = new AV.Query('Counter');//表名

query.notEqualTo('id',0); //id不爲0的結果

query.descending('time'); //結果按閱讀次數降序排序

query.limit(20); //最終只返回10條結果

query.find().then(function (todo) {

for (var i=0;i<10;i++){

var result=todo[i].attributes;

time=result.time; //閱讀次數

title=result.title; //文章標題

url=result.url; //文章url

var content="<p>"+"<font color='#0477ab'>"+"【閱讀次數:"+time+"】"+"<a href='"+"http://thief.one"+url+"'>"+title+"</font>"+"</a>"+"</p>";

// document.write("<a href='"+"http://thief.one/"+url+"'>"+title+"</a>"+" Readtimes:"+time+"<br>");

document.getElementById("heheda").innerHTML+=content

}

}, function (error) {

console.log("error");

});

</script>

 

最終的效果查看:http://thief.one/count

多說替換成來必力評論

更新於@2017年5月18日
多說已經宣佈下線了,因此我找了個來必力評論系統來替換,以下是替換的教程,教程內容來自:https://blog.smoker.cc/web/add-comments-livere-for-hexo-theme-next.html

來必力評價
優點:界面美觀
缺點:不支持數據導入,加載慢

首先在 _config.yml 文件中添加如下配置:


 

1


 

livere_uid: your uid

 

其中 livere_uid 即註冊來必力獲取到的 uid。
在 layout/_scripts/third-party/comments/ 目錄中添加 livere.swig,文件內容如下:


 

1

2

3

4

5

6

7

8

9

10

11

12

13

14


 

{% if not (theme.duoshuo and theme.duoshuo.shortname) and not theme.duoshuo_shortname and not theme.disqus_shortname and not theme.hypercomments_id and not theme.gentie_productKey %}

{% if theme.livere_uid %}

<script type="text/javascript">

(function(d, s) {

var j, e = d.getElementsByTagName(s)[0];

if (typeof LivereTower === 'function') { return; }

j = d.createElement(s);

j.src = 'https://cdn-city.livere.com/js/embed.dist.js';

j.async = true;

e.parentNode.insertBefore(j, e);

})(document, 'script');

</script>

{% endif %}

{% endif %}

 

優先使用其他評論插件,如果其他評論插件沒有開啓,且LiveRe評論插件配置開啓了,則使用LiveRe。其中腳本代碼爲上一步管理頁面中獲取到的。在layout/_scripts/third-party/comments.swig文件中追加:


 

1


 

{% include './comments/livere.swig' %}

 

引入 LiveRe 評論插件。
最後,在 layout/_partials/comments.swig 文件中條件最後追加LiveRe插件是否引用的判斷邏輯:


 

1

2

3


 

{% elseif theme.livere_uid %}

<div id="lv-container" data-id="city" data-uid="{{ theme.livere_uid }}"></div>

{% endif %}

 

最後打開博客瞧瞧吧!

多說替換成網易雲跟貼

最好的方法就是更新next主題,因爲最新版本的主題已經支持這幾種評論。
如果不想更新主題,則往下看:

網易雲跟貼評價:
性能穩定,功能中規中矩,支持數據導入

首先在 _config.yml 文件中添加如下配置:


 

1


 

gentie_productKey: #your-gentie-product-key

 

其中 gentie_productKey 即註冊網易雲跟貼獲取到的key。
在 layout/_scripts/third-party/comments/ 目錄中添加 gentie.swig,文件內容如下:


 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16


 

{% if not (theme.duoshuo and theme.duoshuo.shortname) and not theme.duoshuo_shortname and not theme.disqus_shortname and not theme.hypercomments_id %}

{% if theme.gentie_productKey %}

{% set gentie_productKey = theme.gentie_productKey %}

<script>

var cloudTieConfig = {

url: document.location.href,

sourceId: "",

productKey: "{{gentie_productKey}}",

target: "cloud-tie-wrapper"

};

</script>

<script src="https://img1.ws.126.net/f2e/tie/yun/sdk/loader.js"></script>

{% endif %}

{% endif %}

 

在layout/_scripts/third-party/comments.swig文件中追加:


 

1


 

{% include './comments/gentie.swig' %}

 

最後,在 layout/_partials/comments.swig 文件中條件最後追加網易雲跟帖插件引用的判斷邏輯:


 

1

2

3


 

{% elseif theme.gentie_productKey %}

<div id="cloud-tie-wrapper" class="cloud-tie-wrapper">

</div>

 

報錯解決

(一)Deployer not found: git

當編輯__config.yml文件,將type: git設置完成後,運行hexo g 報錯:git not found
解決方案:可以在MyBlog目錄下運行: npm install hexo-deployer-git –save

(二)permission denied

當執行: hexo deploy 報錯時,把__config.yml中的github連接形式從ssh改成http。

(三)當在themes目錄下載主題時,報錯。

將該目錄只讀屬性取消。

(四)genrnate 報錯

檢查_config.yml配置中,鍵值對冒號後面是否已經預留了一個半角空格。

(五)ERROR Plugin load failed: hexo-generator-feed


 

1

2


 

npm install hexo-generator-feed

npm install hexo-generator-feed --save

(六)fatal: The remote end hung up unexpectedly


 

1

2

3


 

$ git config https.postBuffer 524288000

$ git config http.postBuffer 524288000

$ git config ssh.postBuffer 524288000

(七)hero d推送的內容有問題

  首先檢查下.deploy_git文件夾下的.git文件是否存在,此.git文件指定了hexo d時推送public文件夾,而不是所有的內容。如果此.git文件不存在,則會出現推送內容錯誤。
  用npm install hexo-deployer-git –save生成的.deploy_git不包含.git文件,因此正確的做法是.deploy_git文件夾也需要備份,然後再用npm install hexo-deployer-git –save更新一下其內容即可。
  如果已經出現這個錯誤,則刪除.deploy_git,重新hexo d。

(八)hexo s報錯

在新版本的mac上,安裝運行hexo會報此錯誤,但不影響使用。


 

1


 

{ Error: Cannot find module

 

解決方案:


 

1


 

npm install hexo --no-optional

 

Local Search錯誤

  最近發現Local Search搜索出來的連接有錯誤,到不是說連接不對,而是當我在/aaa/目錄下搜索一個頁面時,跳轉到了/aaa/正確的連接/,這樣明顯是正確的,應該是跟目錄+跳轉的目錄。
  網上搜索了下,沒有類似的案例,那麼自己動手修改吧,打開node_modules/hexo-generator-searchdb/templates下的xml.ejs文件:


 

1


 

<url><%- ("../../../../../../../../"+post.path) %></url>

 

說明:將這個文件的兩處url都改成這樣就可以了。

異地同步博客內容

  現在電腦已經很普及了,因爲一般來說我們都是公司一臺電腦,家裏一臺電腦,那麼如何將兩臺電腦上博客的內容同步內,即兩臺電腦上都可以編輯更新博客?
要解決這個問題,首先我們要清楚我們博客文件的組成:

  • node_modules
  • public
  • scaffolds
  • source
  • themes
  • _config_yml
  • db.json
  • package.json
  • .deploy_git

  以上爲利用hexo生成的博客全部內容,那麼當我們執行hexo d時,正真被推送到github上的又有哪些內容呢?
  我們可以看下github上的tengzhangchao.github.io項目,發現裏面只有Public目錄下的內容。也就是說,我們博客上呈現的內容,其實就是public下的文件內容。那麼這個Pulic目錄是怎麼生成的呢?
  一開始hexo init的時候是沒有public目錄的,而當我們運行hexo g命令時,public目錄被生成了,換句話說hexo g命令就是用來生成博客文件的(會根據_config.yml,source目錄文件以及themes目錄下文件生成)。同樣當我們運行hexo clean命令時,public目錄被刪除了。
  好了,既然我們知道了決定博客顯示內容的只有一個Public目錄,而public目錄又是可以動態生成的,那麼其實我們只要在不同電腦上同步可以生成Public目錄的文件即可。

以下文件以及目錄是必須要同步的:

  • source
  • themes
  • _config.yml
  • db.json
  • package.json
  • .deploy_git

  同步的方式有很多種,可以每次更新後都備份到一個地址。我採用github去備份,也就是新建一個項目用來存放以上文件,每次更新後推送到github上,用作備份同步。
  同步完必須的文件後,怎麼再其他電腦上也可以更新博客呢?
  前提假設我們現在配置了一臺新電腦,裏面沒有安裝任何有關博客的東西,那麼我們開始吧:

  • 下載node.js並安裝(官網下載安裝),默認會安裝npm。
  • 下載安裝git(官網下載安裝)
  • 下載安裝hexo。方法:打開cmd 運行npm install -g hexo(要翻牆)
  • 新建一個文件夾,如MyBlog
  • 進入該文件夾內,右擊運行git,輸入:hexo init(生成hexo模板,可能要翻牆)

  我們重複了一開始搭建博客的步驟,重新生成了一個新的模板,這個模板中包含了hexo生成的一些文件。

  • git clone 我們備份的項目,生成一個文件夾,如:MyBlogData
  • 將MyBlog裏面的node_modules、scaffolds文件夾複製到MyBlogData裏面。

  做完這些,從表面上看,兩臺電腦上MyBlogData目錄下的文件應該都是一樣的了。那麼我們運行hexo g
hexo d試試,如果會報錯,則往下看。

  • 這是因爲.deploy_git沒有同步,在MyBlogData目錄內運行:npm install hexo-deployer-git –save後再次推送即可

  總結流程:當我們每次更新MyBlog內容後,先利用hexo將public推送到github,然後再將其餘必須同步的文件利用git推送到github。

SEO優化

seo優化對於網站是否能被搜索引擎快速收錄有很大幫助,因此適當做一些seo還是有必要的,以下內容參考:https://lancelot_lewis.coding.me/2016/08/16/blog/Hexo-NexT-SEO/

添加sitemap文件

安裝以下2個插件,然後重啓hexo後,網站根目錄(source)下會生成sitemap.xml與baidusitemap.xml文件,搜索引擎在爬取時會參照文件中的url去收錄。


 

1

2

3

4


 

npm install hexo-generator-sitemap --save-dev

hexo d -g

npm install hexo-generator-baidu-sitemap --save-dev

hexo d -g

 

添加robots.txt

新建robots.txt文件,添加以下文件內容,把robots.txt放在hexo站點的source文件下。


 

1

2

3

4

5

6

7

8

9

10

11


 

User-agent: * Allow: /

Allow: /archives/

Disallow: /vendors/

Disallow: /js/

Disallow: /css/

Disallow: /fonts/

Disallow: /vendors/

Disallow: /fancybox/

Sitemap: http://thief.one/sitemap.xml

Sitemap: http://thief.one/baidusitemap.xml

 

首頁title的優化

更改index.swig文件,文件路徑是your-hexo-site\themes\next\layout,將下面代碼


 

1


 

{% block title %} {{ config.title }} {% endblock %}

 

改成


 

1


 

{% block title %} {{ config.title }} - {{ theme.description }} {% endblock

 

觀察首頁title就是標題+描述了。

MakeDown語法


 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18


 

[hexo](http://www.baidu.com) 表示超鏈接

##大標題

###小標題

<!-- more -->

<!-- 標籤別名 -->

{% cq %}blah blah blah{% endcq %}

空格 中文全角空格表示

---

文章標題

---

>內容 區塊引用

*1

*2

*3

列表

*內容* 表示強調內容

![Alt text](/path/to/img.jpg) 圖片

![](/upload_image/20161012/1.png)

詳細Markdown語法請參考:MakeDown語法

參考文章

http://www.jianshu.com/p/f054333ac9e6
https://neveryu.github.io/2016/09/30/hexo-next-two/

提醒:在更新博客內容時,最好先在本地調試完畢後(hexo server),再推送到github上。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章