动态

详情 返回 返回

hexo實戰:(二)個人獨立博客豪華裝修合集 - 动态 详情

前言

上次介紹了使用 Hexo+GitHub Pages,零成本搭建一個專屬自己的獨立博客網站。我覺得那篇文章是沒有入門門檻的,不管你是什麼行業,只要想打造個人 IP,又不太想受博客平台約束,那麼讀完後動手操作一下也能輕鬆完成。
而這次呢,上篇也説了 Hexo 會出三篇內容,這裏就要在前者基礎上做進一步升級,將完成下面七個主要的博客常用功能。

指標

  • Hexo 如何安裝 Butterfly 主題並配置?
  • Hexo 如何創建頁面和添加文章?
  • Hexo 如何添加第三方評論系統?
  • Butterfly 主題如何添加站內搜索?
  • Butterfly 主題如何添加百度統計?
  • Butterfly 主題如何添加文章置頂功能?
  • Butterfly 主題如何配置 RSS 和 404 頁面?
  • 如何配合 Typora 完成 md 的同步與本地備份?

主題添加與配置

Hexo 官網專門有一個欄目的主題列表,這裏我選的 “butterfly”,首先拉取主題代碼到 themes 目錄下,然後在 Hexo 的配置中啓動主題。

拉取主題包

git clone -b master https://github.com/jerryc127/hexo-theme-butterfly.git themes/butterfly

啓動主題

theme: butterfly
圖片

基礎配置

基礎配置主要是設置網站的標題、描述、SEO、菜單等項,下面我就列出 hexo 的 config(根目錄下_config.yml),和主題 Butterfly 的 config(themes 的 Butterfly 下_config.yml),並註釋相關項的簡介僅供參考。

hexo_config.yml

title: ZERO開發                           #網站標題
subtitle: 一個獨立開發者的博客               #網站副標題
description: 公眾號:ZERO開發               #網站描述
keywords: 技術博客、獨立開發者、PHP開發、Pthon開發、人工智能、物聯網、遊戲開發
author: 北橋蘇              #您的名字
language: zh-CN            #網站使用的語言
timezone:                   #網站時區。Hexo 默認使用您電腦的時區

# URL 網址
## 如果您的網站存放在子目錄中,
## 例如 http://yoursite.com/blog,則請將您的 url 
## 設為 http://yoursite.com/blog 並把 root 設為 /blog/。
url: http://z11r00.github.io
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:

# Directory 目錄配置
source_dir: source       #源文件夾,這個文件夾用來存放內容。
public_dir: public       #公共文件夾,這個文件夾用於存放生成的站點文件。
tag_dir: tags            #標籤文件夾
archive_dir: archives    #歸檔文件夾
category_dir: categories #分類文件夾
code_dir: downloads/code #nclude code 文件夾
i18n_dir: :lang          #國際化(i18n)文件夾
skip_render:             #跳過指定文件的渲染,您可使用 glob 表達式來匹配路徑。

# Writing 文章
new_post_name: :title.md # 新建文章默認文件名
default_layout: post     # 默認佈局
titlecase: false # Transform title into titlecase
external_link: true # 在新標籤中打開一個外部鏈接,默認為true
filename_case: 0    #轉換文件名,1代表小寫;2代表大寫;默認為0,意思就是創建文章的時候,是否自動幫你轉換文件名,默認就行,意義不大。
render_drafts: false  #是否渲染_drafts目錄下的文章,默認為false
post_asset_folder: false #啓動 Asset 文件夾
relative_link: false    #把鏈接改為與根目錄的相對位址,默認false
future: true        #顯示未來的文章,默認false
syntax_highlighter: highlight.js
highlight:    #代碼塊的設置
  enable: true
  line_number: true
  auto_detect: false
  tab_replace:
prismjs:
  preprocess: true
  line_number: true
  tab_replace: ''

# Category & Tag 分類和標籤的設置
default_category: uncategorized        #默認分類
category_map:                          #分類別名
tag_map:                               #標籤別名

# 全局日期格式化
date_format: YYYY-MM-DD
time_format: HH:mm:ss
updated_option: 'mtime'
pagination_dir: page    #分頁目錄

## 插件index,用於顯示分頁和排序配置
index_generator:
  path: ''
  per_page: 5    # 0.關閉分頁功能  >0.每頁條數
  order_by:
    top: -1        # 置頂排序: -1.倒序 1.正序
    date: -1    # 日期排序: -1.倒序 1.正序

# 主題啓動配置
theme: butterfly

# Deployment github部署配置
deploy:
  type: git
  repository: https://github.com/z11r00/z11r00.github.io
  branch: main
  
##hexo-generator-search搜索插件配置
search:
  path: search.xml
  field: post
  format: html
  limit: 10000
  
## rss配置
feed:
  type: atom
  path: atom.xml
  limit: 20

## rss位置
rss: /atom.xml

hexo_butterfly_config.yml

nav:
  logo: /img/logo.png  # 導航欄左側LOGO
  display_title: true
  fixed: true # 是否置頂導航欄

## 菜單圖標配置
Home: / || fas fa-home
Archives: /archives/ || fas fa-archive
Tags: /tags/ || fas fa-tags
Categories: /categories/ || fas fa-folder-open
List||fas fa-list:
  Music: /music/ || fas fa-music
  Movie: /movies/ || fas fa-video
Link: /link/ || fas fa-link
About: /about/ || fas fa-heart

# Menu 菜單跳轉配置
menu:
  首頁: / || fas fa-home
  時間軸: /archives/ || fas fa-archive
  標籤: /tags/ || fas fa-tags
  分類: /categories/ || fas fa-folder-open
  #清單||fa fa-heartbeat:
    #音樂: /music/ || fas fa-music
    #照片: /Gallery/ || fas fa-images
    #電影: /movies/ || fas fa-video
  友鏈: /link/ || fas fa-link
  關於: /about/ || fas fa-heart

# Code Blocks (代碼相關)
# --------------------------------------
highlight_theme: light #  darker / pale night / light / ocean / mac / mac light / false
highlight_copy: true # copy button
highlight_lang: true # show the code language
highlight_shrink: false # true: shrink the code blocks / false: expand the code blocks | none: expand code blocks and hide the button
highlight_height_limit: false # unit: px
code_word_wrap: false   # 代碼是否自動換行

# Social Settings (社交圖標設置)
social:
  fab fa-github: https://github.com/z11r00 || Github || '#24292e'
  fas fa-envelope: 2652364582@qq.com || Email || '#4a7dbe'
  fas fa-rss: /atom.xml || RSS
  

# Image (圖片設置)
# --------------------------------------

# Favicon(網站圖標)
favicon: /img/favicon.png

# Avatar (頭像)
avatar:
  img: /img/avatar.png
  effect: false

# 是否禁用所有跳轉頁的頂部圖片
disable_top_img: true

# 首頁圖
index_img: /img/index_img.jpg

# 所有頁面的默認頂部圖
default_top_img: /img/default_top_img.jpeg

# 歸檔(時間線)頁面頂部圖
archive_img: /img/archive_img.jpg

# 標籤頁面頂部圖
tag_img:

# The banner image of tag page
# format:
#  - tag name: xxxxx
tag_per_img:

# 分類頁面頂部圖
category_img:

# The banner image of category page
# format:
#  - category name: xxxxx
category_per_img:

## 封面配置
cover:
  # display the cover or not (是否顯示文章封面)
  index_enable: true
  aside_enable: true
  archives_enable: true
  # the position of cover in home page (封面顯示的位置)
  # left/right/both
  position: both
  (當沒有設置cover時,默認的封面顯示)
  default_cover:
    - https://i.loli.net/2020/05/01/gkihqEjXxJ5UZ1C.jpg

# Replace Broken Images (替換無法顯示的圖片)
error_img:
  flink: /img/friend_404.gif
  post_page: /img/404.jpg

# 404 頁面配置
error_404:
  enable: true
  subtitle: 'Page Not Found'
  background: /img/404.jpg

## 文章配置
post_meta:
  page: # Home Page
    date_type: created # created or updated or both 主頁文章日期是創建日或者更新日或都顯示
    date_format: date # date/relative 顯示日期還是相對日期
    categories: true # true or false 主頁是否顯示分類
    tags: false # true or false 主頁是否顯示標籤
    label: true # true or false 顯示描述性文字
  post:
    date_type: both # created or updated or both 文章頁日期是創建日或者更新日或都顯示
    date_format: date # date/relative 顯示日期還是相對日期
    categories: true # true or false 文章頁是否顯示分類
    tags: true # true or false 文章頁是否顯示標籤
    label: true # true or false 顯示描述性文字

# 錨點配置
anchor:
  # when you scroll, the URL will update according to header id.
  auto_update: false
  # Click the headline to scroll and update the anchor
  click_to_scroll: false

# figcaption (圖片描述文字)
photofigcaption: false

# 複製功能的配置
# copyright: Add the copyright information after copied content (複製的內容後面加上版權信息)
copy:
  enable: true      # 是否開啓網站複製權限
  copyright:
    enable: false   # 是否開啓複製版權信息添加
    limit_count: 50 # 字數限制,當複製文字大於這個字數限制時,將在複製的內容後面加上版權信息

# Post
# --------------------------------------

# toc (文章的目錄配置)
toc:
  post: true
  page: false
  number: true
  expand: false
  style_simple: false 
  scroll_percent: true

# 文章版權設置
post_copyright:
  enable: true
  decode: false
  author_href:
  license: CC BY-NC-SA 4.0
  license_url: https://creativecommons.org/licenses/by-nc-sa/4.0/

# Sponsor/reward 文章打賞
reward:
  enable: true
  text: 打賞一下~
  QR_code:
    - img: /img/qrcode/wechat_trade.jpg
      link:
      text: 微信
    - img: /img/qrcode/alipay_trade.jpg
      link:
      text: 支付寶

# 文章編輯器配置
# Easily browse and edit blog source code online.
post_edit:
  enable: false
  # url: https://github.com/user-name/repo-name/edit/branch-name/subdirectory-name/
  # For example: https://github.com/jerryc127/butterfly.js.org/edit/main/source/
  url:

# 文章推薦配置
related_post:
  enable: true
  limit: 6 # Number of posts displayed
  date_type: created # or created or updated 文章日期顯示創建日或者更新日

# post_pagination (分頁)
# value: 1 || 2 || false
# 1: The 'next post' will link to old post
# 2: The 'next post' will link to new post
# false: disable pagination
post_pagination: 1

# Displays outdated notice for a post (文章過期提醒)
noticeOutdate:
  enable: false
  style: flat # style: simple/flat
  limit_day: 500 # When will it be shown
  position: top # position: top/bottom
  message_prev: It has been
  message_next: days since the last update, the content of the article may be outdated.

# Footer Settings(頁尾設置)
# --------------------------------------
footer:
  owner:
    enable: true
    since: 2017
  custom_text: Copyright© ZERO開發-獨立開發者的日常總結<br/><a href="https://beian.miit.gov.cn/" target="_blank">贛ICP備16002525號-1</a>
  # copyright: true # Copyright of theme and framework
  copyright: false
  

# aside (側邊欄)
# --------------------------------------

aside:
  enable: true
  hide: false
  button: true
  mobile: true # display on mobile
  position: left # left or right
  display:
    archive: true
    tag: true
    category: true
  card_author:
    enable: true
    description:
    button:
      enable: true
      icon: fas fa-video
      text: 關注我
      link: https://space.bilibili.com/286666708/
  card_announcement:
    enable: true
    content: 微信公眾號:ZERO開發,致力於為猿友們提供有價值的內容
  card_recent_post:
    enable: true
    limit: 5 # if set 0 will show all
    sort: date # date or updated
    sort_order: # Don't modify the setting unless you know how it works
  card_categories:
    enable: true
    limit: 8 # if set 0 will show all
    expand: none # none/true/false
    sort_order: # Don't modify the setting unless you know how it works
  card_tags:
    enable: true
    limit: 40 # if set 0 will show all
    color: false
    orderby: random # Order of tags, random/name/length
    order: 1 # Sort of order. 1, asc for ascending; -1, desc for descending
    sort_order: # Don't modify the setting unless you know how it works
  card_archives:
    enable: true
    type: monthly # yearly or monthly
    format: MMMM YYYY # eg: YYYY年MM月
    order: -1 # Sort of order. 1, asc for ascending; -1, desc for descending
    limit: 8 # if set 0 will show all
    sort_order: # Don't modify the setting unless you know how it works
  card_webinfo:
    enable: true
    post_count: true
    last_push_date: true
    sort_order: # Don't modify the setting unless you know how it works
  card_post_series:
    enable: true
    series_title: false # The title shows the series name
    orderBy: 'date' # Order by title or date
    order: -1 # Sort of order. 1, asc for ascending; -1, desc for descending

# busuanzi count for PV / UV in site
# 訪問人數
busuanzi:
  site_uv: true
  site_pv: true
  page_pv: true

# Time difference between publish date and now (網頁運行時間)
# Formal: Month/Day/Year Time or Year/Month/Day Time
runtimeshow:
  enable: false
  publish_date:

# Aside widget - 最新評論
newest_comments:
  enable: true
  sort_order: # Don't modify the setting unless you know how it works
  limit: 6
  storage: 10 # unit: mins, save data to localStorage
  avatar: true

# Bottom right button (右下角按鈕)
# --------------------------------------

# Conversion between Traditional and Simplified Chinese (簡繁轉換)
translate:
  enable: false
  # The text of a button
  default: 繁
  # the language of website (1 - Traditional Chinese/ 2 - Simplified Chinese)
  defaultEncoding: 2
  # Time delay
  translateDelay: 0
  # The text of the button when the language is Simplified Chinese
  msgToTraditionalChinese: '繁'
  # The text of the button when the language is Traditional Chinese
  msgToSimplifiedChinese: '簡'

# Read Mode (閲讀模式)
readmode: true

# Local search (hexo-generator-search創建搜索)
local_search:
  enable: true
  # Preload the search data when the page loads.
  preload: false
  # Show top n results per article, show all results by setting to -1
  top_n_per_article: 1
  # Unescape html strings to the readable one.
  unescape: false
  CDN:

# Docsearch
docsearch:
  enable: false
  appId:
  apiKey:
  indexName:
  option:

# Share System (分享)
# --------------------------------------
# Share.js
# https://github.com/overtrue/share.js
sharejs:
  enable: true
  sites: weibo,wechat,qq,facebook,twitter

# AddToAny
# https://www.addtoany.com/
addtoany:
  enable: false
  item: facebook,twitter,wechat,sina_weibo,facebook_messenger,email,copy_link

# Comments System (評論系統)
# --------------------------------------
# 評論配置
comments:
  # Up to two comments system, the first will be shown as default
  # Choose: Disqus/Disqusjs/Livere/Gitalk/Valine/Waline/Utterances/Facebook Comments/Twikoo/Giscus/Remark42/Artalk
  use: gitalk # Valine,Disqus
  text: true # Display the comment name next to the button
  # lazyload: The comment system will be load when comment element enters the browser's viewport.
  # If you set it to true, the comment count will be invalid
  lazyload: true
  count: false # Display comment count in post's top_img
  card_post_count: false # Display comment count in Home Page

# disqus
# https://disqus.com/
disqus:
  shortname:
  apikey: # For newest comments widget

# Alternative Disqus - Render comments with Disqus API
# DisqusJS 評論系統,可以實現在網路審查地區載入 Disqus 評論列表,兼容原版
# https://github.com/SukkaW/DisqusJS
disqusjs:
  shortname:
  apikey:
  option:

# livere (來必力)
# https://www.livere.com/
livere:
  uid:

# gitalk
# https://github.com/gitalk/gitalk
gitalk:
  client_id: Ov23…………1C6fM
  client_secret: 4304…………44433f77352
  repo: z11r00.github.io
  owner: z11r00
  admin: z11r00
  option:

# Analysis (第三方統計配置)
# --------------------------------------

# Baidu Analytics
# https://tongji.baidu.com/web/welcome/login
baidu_analytics: 74270…………………………………………

# Advertisement
# --------------------------------------

# Google Adsense (谷歌廣告)
google_adsense:
  enable: false
  auto_ads: true
  js: https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js
  client:
  enable_page_level_ads: true

# Verification (站長驗證)
# --------------------------------------

site_verification:
  # - name: google-site-verification
  #   content: xxxxxx
  # - name: baidu-site-verification
  #   content: xxxxxxx

創建菜單頁面

頁面包括標籤頁(tags)、分類頁(categories)、友鏈頁(link)、關於頁(about),當然也可以自行添加,比如音樂電影之類。不過添加這種頁面方式都大同小異,以下以標籤頁舉例。

運行命令

hexo new page tags

編輯 MD

運行命令後,會在 source 下根據 butterfly 模板生成對應的 md 文件,tags 就是 tags,不過要將 md 文件的 type 修改為對應的類型,類型見上。

title: 標籤頁
date: 2024-05-02 21:01:24
type: "tags"

圖片

Page(頁面)front-matter

title: 頁面名稱
date:  創建日期
type: (tags,link,categories這三個頁面需要配置)
comments: (是否需要顯示評論,默認true)
description: 用於SEO優化
top_img: (設置頂部圖)
mathjax: (數學公式顯示是否支持)
katex:   (Tex公式顯示是否支持)
創建文章寫文章,可以通過命令創建,也可以在 source/_posts 目錄下,自行新建 markedown 文件,因為命令也是在_posts 文件夾下新建。運行命令hexo new "文章的大標題"
Post(文章)front-mattertitle: CentOS7下Tomcat啓動慢的原因及解決方案
date: 2017-12-02 21:01:24
description:文章描述,用於做SEO優化的
keywords: 文章SEO關鍵詞
top_img: 文章頂部圖
cover: 文章縮略圖(封面圖)
toc: true (是否顯示文章目錄)
toc_number: true (是否顯示文章目錄的標識數字)
copyright: true (是否顯示版權)
mathjax: (數學公式顯示是否支持)
katex:   (Tex公式顯示是否支持)
hide: false (是否隱藏當前文章)
comments: true #是否可評論
toc: true #是否顯示文章目錄
categories: "雲服務器" #分類
tags:   #標籤
 - centOS
 - tomcat

站內搜索添加

站內搜索,我是採用的 hexo-generator-search 插件解決方案,安裝完插件,然後配置一下,最後用 hexo 清理再生成一下全站靜態,同時在根目錄也會生成一個 search.xml,用於做字符串模糊匹配的。

插件安裝

npm install hexo-generator-search --save

添加或配置

-config(hexo配置)

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

-butterfly-config(butterfly主題配置)

local_search:
  enable: true
  preload: false
  CDN:

清理與生成

hexo clean && hexo g

圖片

評論系統添加

第三方評論系統我這裏使用的是 Gitalk ,Gitalk 是一個基於 GitHub Issue 和 Preact 開發的評論插件。使用 GitHub 登陸,能支持多國語言,至於他的原理,其他博主有介紹。這裏就不展開了,如果有時間以後可能會整體介紹一下常用的第三方評論系統,因為我還是希望評論能支持多種方式登陸的,目前沒有找到合適的就先選擇 Gitalk。

註冊應用賬號

圖片

獲取參數

註冊成功後拿到 “Client ID” 和 “Client secrets”(點擊生成再複製),其中注意的是 Homepage URL 要設置為 用户名.github.io 精確到 https 的地址。Authorization callback URL 填寫註冊的域名,如果綁定了個性化域名就填自己的域名,也是要精確到 https 的位置。
圖片

修改 Butterfly 配置

打開 butterfly 主題的配置文件,第一個找到 “gitalk” 項,將上面複製的 client id 和 client secrets 粘貼。第二個找到 “comments”,將 use 配置為 "gitalk",當然也可以用 valine、Disqus 之類的第三方評論系統。

comments:
  use: # 使用的第三方評論系統名稱
  text: true # 是否在按鈕旁顯示評論名稱
  # If you set it to true, the comment count will be invalid
  lazyload: false # 是否設置評論窗為懶加載
  count: true # 是否設置評論數統計
  card_post_count: true # 是否將評論數顯示到首頁
  
gitalk:
  client_id: # github應用ID
  client_secret: # github應用密鑰
  repo: 用户名.github.io
  owner: 用户名
  admin: 用户名

博客倉庫設置

進入倉庫,點擊 “Settings”,找到 “Features”, 將 “Issues” 勾選。
圖片

文章置頂功能添加

打開 hexo 配置文件 _config.yml,如果前面有 per_page 的關閉,使用的是 hexo-generator-index 插件排序,可以自定義序號和日期排序。如何要置頂文章,只需在文章 md 的 front-matter 裏添加一個 top 參數,數值自定。文章列表會出現一個釘子圖標,要看到效果則要 hexo 先清理再生成。

添加配置

index_generator:
  path: ''
  per_page: 5  # 每頁條數
  order_by:       
    top: -1       # 置頂: -1.倒序 1.順序
    date: -1   # 日期: -1.倒序 1.順序

md 文章設置

圖片

清理與生成

hexo clean && hexo g

圖片

RSS 配置

對於 RSS 輸出功能,需要安裝 hexo-generator-feed 插件。最後使用 hexo 清理再生成,博客根目錄就會生成 atom.xml

插件安裝

npm install hexo-generator-feed --save

添加配置

feed:
  type: atom
  path: atom.xml
  limit: 20

設置 RSS 地址

rss: /atom.xml

設置 404 頁面

在 hexo 的_config.yml 找到或添加 error_404,設置開啓,分別添加標題和背景圖。

error_404:
  enable: true
  subtitle: 'Page Not Found'
  background: /img/404.jpg

圖片

添加百度統計

先登陸或註冊百度統計平台,找到 “使用設置” -> “代碼獲取” -> “新版統計代碼獲取”,然後會看到一段 js 代碼,只需要複製如下的一個 32 位長度的字符串。最後將該字符串粘貼到 butterfly 的 config.yml 中的 baidu_analytics 中,部署後一天就可以在後台查看統計報表了。 

MarketDown 用法

關於新建的文章,我希望本地能備份一份,有 md 文件和圖片,方便上傳到其他平台博客。那要怎麼做呢,這裏我用一個免費的 md 工具 ——Typora 為例。

創建文件和文件夾

創建一個以文章標題的文件夾,裏面再分別創建一 img 文件夾和同文章標題的 md 文件,img 下再創建一個與文章標題同名的文件夾。
圖片

Typora 設置

依次找到 “文件” -> “偏好設置” -> "圖像" , 將插入圖片時的下拉選中 “複製到指定路徑”,填入下面的值。./img/${filename}/

圖片
第二步找到 “格式” -> "圖像" -> “設置圖片根目錄”,選擇 markdown 文件同級的目錄,最後複製圖片時就會複製到 img 下的文章同名目錄下。
圖片
為什麼要這樣設置?因為 Hexo 的圖片在打包前都是在主題包 /source/img 裏,而為了方便本地能按文章存儲,也方便將本地的文章圖片直接丟到 img 下,然後 md 文件放到_post 中。打包之後就能以倉庫圖片展示,而且按文章分類存儲,以後刪除起來也一目瞭然。
圖片

寫在最後

這次的優化看起來好像是面面俱到了,但其實關於 Hexo 主題還有更細節的處理。比如可以添加博客看板精靈,酷炫的動態大背景,還有鼠標跟隨或點擊特效等等。但是我這博客目前定位主要以內容為主,所以暫時就就演示了,當然,如果有用户喜歡也可以馬上加上去的。最後呢,就是三部曲的第二篇結束了,那下次就自然地要開始進入 Hexo 章最後一篇。現在是計劃介紹一下怎麼讓博客被各大搜索引擎收錄,以及一些推廣心得等等。具體的詳細內容未定,更新時間也還不知道,因為在等我那個小遊戲的軟著下來,下來後我一定要好好寫一篇軟著申請避坑的文章,可實在是太難等了!(我是一個持續摸索個人副業的普通程序員,關注我,和你一起探索更多可能。——ZERO 開發)

user avatar alibabawenyujishu 头像 smalike 头像 jingdongkeji 头像 themesdog 头像 huajianketang 头像 woniuseo 头像 bugDiDiDi 头像 libubai 头像 kitty-38 头像 geeklab 头像 Asp1rant 头像 laggage 头像
点赞 64 用户, 点赞了这篇动态!
点赞

Add a new 评论

Some HTML is okay.