用hexo搭建我的博客

前言

現現在搭建我的博客並非一個很是難的事情,你甚至不須要會代碼,由於有現成的框架。如今我將要搭建的博客是利用hexo搭建博客,託管在GitHub上。html

什麼是hexo

Hexo 是一個快速、簡潔且高效的博客框架。Hexo 使用 Markdown(或其餘渲染引擎)解析文章,在幾秒內,便可利用靚麗的主題生成靜態網頁。node

準備工做

  1. 首先準備GitHub帳號github1

GitHub提供了GitHub Pages 幫助咱們來架設一個靜態網站,因此咱們就不須要服務器了;由於Hexo 能夠直接將文章編譯成靜態網頁文件併發布,因此這樣文章的內容、標題等信息就不必存數據庫裏面了,是直接純靜態頁面了,也就不須要數據庫了。android

  1. 安裝git

雖然也能用cmd來安裝使用,但可能會出現問題,最好全程使用git bashgit

  1. 安裝node.js

Hexo需用經過npm安裝,而npm須要node,如今只要安裝node 就自帶 npm了github

nodejs

安裝完成後,在打開cmd終端輸入web

node -v
npm -v

確認添加到環境變量shell

nodejs

由於咱們是要經過npm來安裝,而npm在國內下載很慢,須要更換爲國內鏡像源,這裏使用淘寶的鏡像源數據庫

右鍵打開git bash, 輸入命令npm

npm install -g cnpm --registry=https://registry.npm.taobao.org

可能要等一會,沒有進度條就直接裝好了

git1

使用hexo搭建博客

安裝hexo

直接在git bash輸入命令

cnpm install -g hexo-cli

安裝完成能夠用hexo -v查看hexo的信息

初始化

首先要先建一個文件夾,初始化後會在這個文件夾下生成一堆文件,以後全部操做都在這個文件中操做。

若是搭建過程當中有什麼解決不了的問題,大不了把這個文件夾刪了重來

初始化命令:

hexo init

速度可能比較慢,須要等待一會

完成以後就能夠啓動博客了

輸入命令hexo s 就能夠在本地4000端口啓動博客

它會默認建立一篇文章爲Hello World,裏面會簡單介紹hexo的使用方法

hexo1

如何寫文章

很簡單,在git bash使用命令hexo n '個人第一篇文章',這樣就會在你的博客文件夾路徑下\source\_posts文件夾下生成一篇markdown格式的文件,這樣你就能夠寫文章了

寫完後,保存,使用命令hexo generate(能夠簡寫hexo g)生成,而後hexo s就能夠在本地看到。

部署到GitHub上

在GitHub上新建一個倉庫

nodejs

倉庫的命名:

nodejs

而後create

安裝插件

cnpm install --save hexo-deployer-git 這是用來遠程部署到github上的一個插件

在部署以前,咱們須要先知道博客的部署地址,它須要對應 GitHub 的一個 Repository 的地址,這個信息須要咱們來配置一下。

打開博客根目錄下的 _config.yml 文件,找到 Deployment 這個地方,把剛纔新建的 Repository 的地址貼過來,而後指定分支爲 master 分支,最終修改成以下內容:

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type: git
  repo: {git repo ssh address}
  branch: master

個人就修改成:

# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
  type: git
  repo: git@github.com:wickteam/wickteam.github.io.git
  branch: master

而後就使用hsxo d 將博客推送到遠端,中間可能會要求你輸入GitHub用戶名和密碼

這樣你就能輸入username.github.io查看你的博客了

經常使用hexo命令

常見命令

hexo new "postName" #新建文章
hexo new page "pageName" #新建頁面
hexo generate #生成靜態頁面至public目錄
hexo server #開啓預覽訪問端口(默認端口4000,'ctrl + c'關閉server)
hexo deploy #部署到GitHub
hexo help  # 查看幫助
hexo version  #查看Hexo的版本

縮寫:

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

組合命令:

hexo s -g #生成並本地預覽
hexo d -g #生成並上傳

配置SSH Key(能夠不作)

git使用https協議,每次pull,push都要輸入密碼,使用git協議,使用ssh祕鑰,能夠省去每次輸密碼

大概須要三個步驟:

  1. 本地生成密鑰對;
  2. 設置github上的公鑰;
  3. 修改git的remote url爲git協議。

生成密鑰對

大多數 Git 服務器都會選擇使用 SSH 公鑰來進行受權。系統中的每一個用戶都必須提供一個公鑰用於受權,沒有的話就要生成一個。生成公鑰的過程在全部操做系統上都差很少。首先先確認一下是否已經有一個公鑰了。SSH 公鑰默認儲存在帳戶的主目錄下的 ~/.ssh 目錄。進去看看:

cwzdz@DESKTOP-PAT2F9O MINGW64 /e/hexo_blog
$ cd ~/.ssh

cwzdz@DESKTOP-PAT2F9O MINGW64 ~/.ssh
$ ls
id_rsa  id_rsa.pub  known_hosts

關鍵是看有沒有用 something 和 something.pub 來命名的一對文件,這個 something 一般就是 id_dsa 或 id_rsa。有 .pub 後綴的文件就是公鑰,另外一個文件則是密鑰。

若是沒有.ssh目錄

$ ssh-keygen -t rsa -C "your_email@youremail.com"   # 輸入註冊github的郵箱

而後直接三次enter,固然你也能夠根據提示輸入密碼,我是默認回車的

這樣 本地生成密鑰對的工做就作好了

添加公鑰到你的github賬戶

到.ssh目錄下查看生成的公鑰,通常爲id_rsa.pub

  1. 能夠直接在git bash上查看:

nodejs

  1. 登陸GitHub,而後 Account Settings -> 左欄點擊 SSH Keys -> 點擊 Add SSH key
  2. 而後你複製上面的公鑰內容,粘貼進「Key」文本域內。完了,add key

上述操做完成好了,輸入

cwzdz@DESKTOP-PAT2F9O MINGW64 /e/hexo_blog
$ ssh -T git@github.com
Hi setcreed! You've successfully authenticated, but GitHub does not provide shell access.

若是輸出這樣的信息,就說明已經配置好了

綁定域名(看我的)

固然,你不綁定域名確定也是能夠的,就用默認的 xxx.github.io 來訪問,若是你想更個性一點,想擁有一個屬於本身的域名,那也是OK的。

首先你須要註冊一個域名,推薦去阿里雲,畢竟大公司,不過最近.com域名提價了

綁定域名分2種狀況:帶www和不帶www的。

域名配置最多見有2種方式,CNAME和A記錄,CNAME填寫域名,A記錄填寫IP,因爲不帶www方式只能採用A記錄,因此必須先ping一下你的用戶名.github.io的IP,而後到你的域名DNS設置頁,將A記錄指向你ping出來的IP,將CNAME指向你的用戶名.github.io,這樣能夠保證不管是否添加www均可以訪問,以下:

nodejs

而後到你的github項目博客的根目錄新建一個名爲CNAME的文件(無後綴),裏面填寫你的域名,加不加www看你本身喜愛。

另外說一句,在你綁定了新域名以後,原來的你的用戶名.github.io並無失效,而是會自動跳轉到你的新域名。

hexo美化優化

修改主題

hexo默認的主題不怎麼好看,能夠修改。你能夠在hexo文檔查看hexo相關操做,能夠自選主題,這裏選擇next主題,能夠參考next使用文檔

咱們能夠直接到 Next 主題的 GitHub Repository 上把這個主題下載下來。

主題的 GitHub 地址是:https://github.com/theme-next/hexo-theme-next,咱們能夠直接把 master 分支 Clone 下來。

首先命令行進入到博客根目錄,執行命令:

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

執行完畢以後 Next 主題的源碼就會出如今博客的 themes/next 文件夾下。

而後咱們須要修改下博客所用的主題名稱,修改項目根目錄下的 _config.yml文件,找到 theme 字段,修改成 next 便可,修改以下:

# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: next

而後執行hexo g從新生成

若是出現一些莫名其妙的問題,能夠先執行hexo clean來清理一下public的內容,而後再來從新生成和發佈。

就是下面的效果:

nodejs

如今咱們已經成功切換到 next 主題上面了,接下來咱們就對主題進行進一步地詳細配置吧,好比修改樣式、增長其餘各項功能的支持。

主題的樣式

Next 主題還提供了多種樣式,風格都是相似黑白的搭配,但整個佈局位置不太同樣,經過修改配置文件的 scheme 字段便可,我選了 Pisces 樣式,修改 _config.yml(注意是 themes/next/_config.yml文件)以下:

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

nodejs

增長標籤頁和分類頁

如今咱們的博客只有首頁、文章頁,若是咱們想要增長標籤頁,能夠自行添加,這裏 Hexo 也給咱們提供了這個功能,在博客根目錄執行命令以下:

hexo new page tags

執行這個命令以後會自動幫咱們生成一個 source/tags/index.md 文件,打開後是這樣的:

---
title: tags
date: 2019-10-01 13:05:19
---

咱們能夠自行添加一個 type 字段來指定頁面的類型:

---
title: tags
date: 2019-10-01 13:05:19
type: tags
---

一樣的,咱們能夠這樣增長分類頁:

hexo new page categories

執行這個命令以後會自動幫咱們生成一個 source/categories/index.md 文件,打開後是這樣的:

---
title: categories
date: 2019-10-01 13:15:13
---

添加type字段指定頁面類型:

---
title: categories
date: 2019-10-01 13:15:13
type: categories
---

都設置好了,如今再在主題的 _config.yml文件將頁面的連接添加到主菜單裏面,修改 menu 字段以下:

menu:
  home: / || home
  #about: /about/ || user
  tags: /tags/ || tags
  categories: /categories/ || th
  archives: /archives/ || archive
  #schedule: /schedule/ || calendar
  #sitemap: /sitemap.xml || sitemap
  #commonweal: /404/ || heartbeat

這樣頁面就會這樣:

nodejs

添加中文

在博客根目錄_config.yml文件下修改language:

title: Hexo
subtitle:
description:
keywords:
author: John Doe
language: zh-CN
timezone:

添加搜索頁

不少狀況下咱們須要搜索全站的內容,因此一個搜索功能的支持也是頗有必要的。

若是要添加搜索的支持,須要先安裝一個插件,叫作 hexo-generator-searchdb,命令以下:

npm install hexo-generator-searchdb --save

而後在博客根目錄下的_config.yml 裏面添加搜索設置以下:

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

而後在主題下的_config.yml文件中修改:

# Local Search
# Dependencies: https://github.com/wzpan/hexo-generator-search
local_search:
  enable: true
  # If auto, trigger search by changing input.
  # If manual, trigger search by pressing enter key or search button.
  trigger: auto
  # Show top n results per article, show all results by setting to -1
  top_n_per_article: 5
  # Unescape html strings to the readable one.
  unescape: false
  # Preload the search data when the page loads.
  preload: false

製做favicon標籤欄圖標

favicon 就是站點標籤欄的小圖標,默認是用的 Hexo 的小圖標,若是咱們有站點 Logo 的圖片的話,咱們能夠本身定製小圖標。

咱們能夠上傳圖片到這個網站https://tool.lu/favicon/,製做站點小圖標。

圖標下載下來以後把它放在 themes/next/source/images 目錄下面。

而後在next主題配置文件裏面找到 favicon 配置項,把一些相關路徑配置進去便可,示例以下:

favicon:
  small: /images/favicon -16x16-next.ico
  medium: /images/favicon -32x32-next.ico
  apple_touch_icon: /images/apple-touch-icon-next.png
  safari_pinned_tab: /images/logo.svg
  #android_manifest: /images/manifest.json
  #ms_browserconfig: /images/browserconfig.xml

配置完成以後刷新頁面,整個頁面的標籤圖標就被更新了。

更改頭像

avatar 這個就相似站點的頭像,若是設置了這個,會在站點的做者信息旁邊額外顯示一個頭像.

將其放置到 themes/next/source/images/avatar.png 路徑,而後在主題 _config.yml文件下編輯 avatar 的配置,修改成正確的路徑便可。

# Sidebar Avatar
avatar:
  # In theme directory (source/images): /images/avatar.gif
  # In site directory (source/uploads): /uploads/avatar.gif
  # You can also use other linking images.
  url: /images/avatar.jpg
  # If true, the avatar would be dispalyed in circle.
  rounded: true
  # If true, the avatar would be rotated with the cursor.
  rotated: true

把url的#去掉,後面跟上你設置的圖像路徑,rounded是否設置成圓形,rotated是否設置成可旋轉的。

nodejs

設置RSS訂閱

博客通常是須要 RSS 訂閱的,若是要開啓 RSS 訂閱,這裏須要安裝一個插件,叫作 hexo-generator-feed,安裝完成以後,站點會自動生成 RSS Feed 文件,安裝命令以下:

cnpm install hexo-generator-feed --save

在博客根目錄下運行這個命令,安裝完成以後不須要其餘的配置,之後每次編譯生成站點的時候就會自動生成 RSS Feed 文件了。

啓用側邊欄社交鏈

在next主題下的_config.yml設置以下:

social:
  GitHub: https://github.com/wickteam || github
  #E-Mail: mailto:yourname@gmail.com || envelope
  #Weibo: https://weibo.com/yourname || weibo
  #Google: https://plus.google.com/yourname || google
  #Twitter: https://twitter.com/yourname || twitter
  #FB Page: https://www.facebook.com/yourname || facebook
  #VK Group: https://vk.com/yourname || vk
  #StackOverflow: https://stackoverflow.com/yourname || stack-overflow
  #YouTube: https://youtube.com/yourname || youtube
  #Instagram: https://instagram.com/yourname || instagram
  #Skype: skype:yourname?call|chat || skype

快速返回頂部

咱們在瀏覽網頁的時候,若是已經看完了想快速返回到網站的上端,通常都是有一個按鈕來輔助的,在next主題下的_config.yml設置:

back2top:
  enable: true
  # Back to top in sidebar.
  sidebar: false
  # Scroll percent label in b2t button.
  scrollpercent: true

reading_process閱讀進度條

# Reading progress bar
reading_progress:
  enable: true
  # Available values: top | bottom
  position: top
  color: "#37c6c0"
  height: 2px

bookmark書籤

書籤,能夠根據閱讀歷史記錄,在下次打開頁面的時候快速幫助咱們定位到上次的位置,你們能夠根據喜愛開啓和關閉

bookmark:
  enable: false
  # Customize the color of the bookmark.
  color: "#222"
  # If auto, save the reading progress when closing the page or clicking the bookmark-icon.
  # If manual, only save it by clicking the bookmark-icon.
  save: auto

github標籤

在一些技術博客上,你們可能注意到在頁面的右上角有個 GitHub 圖標,點擊以後能夠跳轉到其源碼頁面,能夠爲 GitHub Repository 引流,你們若是想顯示的話能夠自行選擇打開,個人配置以下:

# Follow me on GitHub` banner in the top-right corner.
github_banner:
  enable: true
  permalink: https://github.com/wickteam
  title: WickTeam GitHub

gitalk評論

因爲 Hexo 的博客是靜態博客,並且也沒有鏈接數據庫的功能,因此它的評論功能是不能自行集成的,但能夠集成第三方的服務。

next主題集成了不少第三方評論插件,有changyan | disqus | disqusjs | facebook_comments_plugin | gitalk | livere | valine | vkontaktegitalk是利用GitHub上的issue來評論的。

首先須要在 GitHub 上面註冊一個 OAuth Application,連接爲:https://github.com/settings/applications/new,註冊完畢以後拿到 Client ID、Client Secret 就能夠了。

首先須要在next主題_config.yml文件的 comments 區域配置使用 gitalk:

# Multiple Comment System Support
comments:
  # Available values: tabs | buttons
  style: tabs
  # Choose a comment system to be displayed by default.
  # Available values: changyan | disqus | disqusjs | facebook_comments_plugin | gitalk | livere | valine | vkontakte
  active: gitalk
  # Setting `true` means remembering the comment system selected by the visitor.

上面主要填寫active

而後找到 gitalk 配置,添加它的各項配置:

# Gitalk
# Demo: https://gitalk.github.io
# For more information: https://github.com/gitalk/gitalk
gitalk:
  enable: true
  github_id: wickteam   # GitHub repo owner
  repo: wickteam.github.io  # Repository name to store issues
  client_id: your_id   # GitHub Application Client ID
  client_secret: your_secret   # GitHub Application Client Secret
  admin_user: wickteam  # GitHub repo owner and collaborators, only these guys can initialize gitHub issues
  distraction_free_mode: true # Facebook-like distraction free mode
  # Gitalk's display language depends on user's browser or system environment
  # If you want everyone visiting your site to see a uniform language, you can set a force language value
  # Available values: en | es-ES | fr | ru | zh-CN | zh-TW
  language: zh-CN

GitHub 受權登陸以後就可使用了,評論的內容會自動出如今 Issue 裏面。

支持數學公式

可能在一些狀況下咱們須要寫一個公式,makdown是支持的,next也支持。

Next 主題提供了兩個渲染引擎,分別是 mathjaxkatex,後者相對前者來講渲染速度更快,並且不須要 JavaScript 的額外支持,但後者支持的功能如今還不如前者豐富,具體的對比能夠看官方文檔:https://theme-next.org/docs/third-party-services/math-equations。

這裏選擇 mathjax ,經過修改配置便可啓用:

# Math Formulas Render Support
math:
  enable: true

  # Default (true) will load mathjax / katex script on demand.
  # That is it only render those page which has `mathjax: true` in Front-matter.
  # If you set it to false, it will load mathjax / katex srcipt EVERY PAGE.
  per_page: true

  # hexo-renderer-pandoc (or hexo-renderer-kramed) required for full MathJax support.
  mathjax:
    enable: true
    # See: https://mhchem.github.io/MathJax-mhchem/
    mhchem: true

Hexo 默認使用 hexo-renderer-marked 引擎渲染網頁, 咱們須要卸載hexo-renderer-marked

mathjax 的使用須要咱們額外安裝一個插件,叫作 hexo-renderer-kramed, 命令以下:

npm uninstall hexo-renderer-marked --save
npm install hexo-renderer-kramed --save

注意:使用的時候須要在 文章開頭加上mathjax: true

pjax

可能你們據說過 Ajax,沒據說過 pjax,這個技術實際上就是利用 Ajax 技術實現了局部頁面刷新,既能夠實現 URL 的更換,又能夠作到無刷新加載。

要開啓這個功能須要先將 pjax 功能開啓,而後安裝對應的 pjax 依賴庫,首先修改_config.yml 修改以下:

pjax: true

而後安裝依賴庫,切換到 next 主題下,而後安裝依賴庫:

$ cd themes/next
$ git clone https://github.com/theme-next/theme-next-pjax source/lib/pjax

這樣 pjax 就開啓了,頁面就能夠實現無刷新加載了

tag標籤

改成true,這樣標籤的#就沒了

tag_icon: true

代碼美化codeblock

在next主題_config.yml修改:

codeblock:
  # Code Highlight theme
  # Available values: normal | night | night eighties | night blue | night bright | solarized | solarized dark | galactic
  # See: https://github.com/chriskempson/tomorrow-theme
  highlight_theme: night
  # Add copy button on codeblock
  copy_button:
    enable: true
    # Show text copy result.
    show_result: false
    # Available values: default | flat | mac
    style: mac

修改文章底部版權信息

creative_commons:
  license: by-nc-sa
  sidebar: true
  post: true

leancloud添加文章訪問量統計

leancloud_visitors:
  enable: true
  app_id: # <app_id>
  app_key: # <app_key>
    # Dependencies: https://github.com/theme-next/hexo-leancloud-counter-security
  # If you don't care about security in leancloud counter and just want to use it directly
  # (without hexo-leancloud-counter-security plugin), set `security` to `false`.
  security: false
  betterPerformance: false

字數統計

NexT 集成了 hexo-symbols-count-time 插件。

步驟:

  1. npm install hexo-symbols-count-time --save
  2. 在站點配置文件下加入
symbols_count_time:
  symbols: true                # 文章字數統計
  time: true                   # 文章閱讀時長
  total_symbols: true          # 站點總字數統計
  total_time: true             # 站點總閱讀時長
  exclude_codeblock: false     # 排除代碼字數統計
  1. 在 next主題配置文件 中添加 symbols_count_time 配置。
# Post wordcount display settings
# Dependencies: https://github.com/theme-next/hexo-symbols-count-time
symbols_count_time:
  separated_meta: true
  item_text_post: true
  item_text_total: false
  awl: 4
  wpm: 275
  suffix: mins.

其中:

awl:平均字符長度,默認爲 4。
漢字 ≈ 2
英文 ≈ 4
俄文 ≈ 6
wpm:閱讀速度。
慢 ≈ 200
正常 ≈ 275
快 ≈ 350
suffix:後綴,默認爲 mins.。
對中文用戶來講:漢字的平均長度 ≈ 1.5,若是僅用中文書寫沒有英文的話,建議 awl 和 wmp 分別設置爲 2 和 300。若是中英混合,建議 awl 和 wmp 分別設置爲 4 和 275。

修改配色

NexT 主題默認色系是黑白色系。目前官方還沒有提供顏色修改的配置,因此咱們能夠經過修改相關 .styl 文件來修改主題顏色。

相關文件:

themes/next/source/css/_common/components/post/post-title.styl
themes/next/source/css/_schemes/Pisces/_brand.styl
themes/next/source/css/_variables/base.styl
themes/next/source/css/_variables/Pisces.styl

連接持久化

Hexo 默認的文章連接是「年/月/日/標題」。之因此要作連接持久化是由於,中文 url 不利於 SEO,另外若是標題修改了,會致使連接發生變化,不利於文章的推廣。因此咱們要作的就是把標題轉成惟一的英文或數字字符串。這裏推薦 rozbo 大神的 hexo-abbrlink

步驟:

  1. npm install hexo-abbrlink --save
  2. site config file 中添加
permalink: posts/:abbrlink/

# abbrlink config
abbrlink:
  alg: crc32  #support crc16(default) and crc32
  rep: dec    #support dec(default) and hex

例子:

crc16 & hex
https://post.zz173.com/posts/66c8.html

crc16 & dec
https://post.zz173.com/posts/65535.html

crc32 & hex
https://post.zz173.com/posts/8ddf18fb.html

crc32 & dec
https://post.zz173.com/posts/1690090958.html

設置博客底部佈局

footer:
  # Specify the date when the site was setup.
  # If not defined, current year will be used.
  since: 2019 #建站時間

  # Icon between year and copyright info.
  icon:
    # Icon name in fontawesome, see: https://fontawesome.com/v4.7.0/icons/
    # `heart` is recommended with animation in red (#ff0000).
    name: heart  #做者圖標(默認是author人像)
    # If you want to animate the icon, set it to true.
    animated: true #圖標是否閃動
    # Change the color of icon, using Hex Code.
    color: "#808080" #圖標顏色

  # If not defined, `author` from Hexo main config will be used.
  copyright: 黃飄 #別填bool型,最後顯示的東西是copyright || author,即左邊沒有設置的話就顯示做者
  # -------------------------------------------------------------
  powered:
    # Hexo link (Powered by Hexo).
    enable: false #是否顯示 Powered by hexo
    # Version info of Hexo after Hexo link (vX.X.X).
    version: false #是否顯示Hexo版本

  theme:
    # Theme & scheme info link (Theme - NexT.scheme).
    enable: false #是否顯示主題信息
    # Version info of NexT after scheme info (vX.X.X).
    version: false #是否顯示主題版本
  # -------------------------------------------------------------
  # Beian icp information for Chinese users. In China, every legal website should have a beian icp in website footer.
  # http://www.miitbeian.gov.cn
  beian:
    enable: false #是否顯示網站備案信息
    icp:

  # -------------------------------------------------------------
  # Any custom text can be defined here.
  #custom_text: Hosted by <a href="https://pages.coding.me" class="theme-link" rel="noopener" target="_blank">Coding Pages</a>

設置google字體鏡像

font:
  # Use custom fonts families or not.
  # Depended options: `external` and `family`.
  enable: true

  # Uri of fonts host, e.g. //fonts.googleapis.com (Default).
  host: //fonts.lug.ustc.edu.cn

添加動態背景 canvas

站點地圖sitemap

添加標籤雲

相關文章
相關標籤/搜索