步驟
-
確認 Hexo 博客的源文件在哪里
-
安裝 Hexo 命令行工具:
npm install -g hexo-cli
-
在源文件目錄中使用
hexo new "文章標題"
創建新文章 -
編輯生成的 Markdown 文件
-
使用
hexo generate
生成靜態文件 -
使用
hexo deploy
部署到這個 GitHub Pages 倉庫
設置Hexo博客環境
- 首先我們需要確保你的系統已經安裝了 Node.jshttps://learn.microsoft.com/zh-cn/windows/dev-environment/javascript/nodejs-on-windows,在windows上通過nvm-windows安裝Node.js和npm
節點版本管理器(通常稱為 nvm)是安裝多個版本的 Node.js的最常用方法,但僅適用于 Mac/Linux,在 Windows 上不受支持。 相反,我們建議安裝 nvm-windows,然后使用它安裝 Node.js 和節點包管理器(npm)。
Power shell中執行npm --version失敗,可以參考
https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_execution_policies?view=powershell-7.5#powershell-execution-policies
PowerShell 執行策略并說明如何管理它們
- 檢查 Node.js 環境:
PS C:\WINDOWS\system32> node -v
v22.18.0
PS C:\WINDOWS\system32> npm -v
10.9.3
- 安裝 Hexo 命令行工具并創建一個新的博客源文件目錄:
npm install -g hexo-cli
- 創建一個新的 Hexo 博客源文件目錄。我們將其命名為
blog-source
hexo init blog-source
cd blog-source
npm install
第一步若出現可以改用 git clone git@github.com:hexojs/hexo-starter.git 先克隆好
git clone https://github.com/hexojs/hexo-starter.git/
Cloning into ‘hexo-starter’…
fatal: unable to access ‘https://github.com/hexojs/hexo-starter.git/’: OpenSSL SSL_read: Connection was reset, errno 10054
- 安裝一些必要的插件,特別是 Git 部署插件:
npm install hexo-deployer-git --save
- 配置博客,改配置文件來設置你的博客信息:
# Hexo Configuration
## Basics
title: Programming In Bug
subtitle: '編程之路'
description: '分享編程經驗與心得'
keywords:
author: EndlessGo
language: zh-CN
timezone: 'Asia/Shanghai'# URL
## Set your site url here
url: https://endlessgo.github.io
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:
pretty_urls:trailing_index: truetrailing_html: true# Directory
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:# Writing
new_post_name: :title.md
default_layout: post
titlecase: false
external_link:enable: truefield: siteexclude: ''
filename_case: 0
render_drafts: false
post_asset_folder: true
relative_link: false
future: true
highlight:enable: trueline_number: trueauto_detect: falsetab_replace: ''wrap: truehljs: false
prismjs:enable: falsepreprocess: trueline_number: truetab_replace: ''# Category & Tag
default_category: uncategorized
category_map:
tag_map:# Metadata elements
meta_generator: true# Date / Time format
date_format: YYYY-MM-DD
time_format: HH:mm:ss# Pagination
per_page: 10
pagination_dir: page# Include / Exclude file(s)
include:
exclude:
ignore:# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: landscape# Deployment
## Docs: https://hexo.io/docs/one-command-deployment
deploy:type: gitrepo: https://github.com/EndlessGo/EndlessGo.github.io.gitbranch: main
- 在vscode terminal中運行npm和nvm等指令
若無法運行,可以嘗試讓 VS Code 的終端繼承 PATH,步驟如下
在 VS Code 里 打開設置
搜索 terminal.integrated.env.windows
在settings.json 中加入
"terminal.integrated.env.windows": {"PATH": "D:\\exe\\nodejs\\;D:\\exe\\nvm\\;${env:PATH}"}
編寫markdown文章測試與本地預覽
- 讓我們創建一個測試文章來確保一切正常工作,會在source/_posts/目錄下創建文件my-first-test-blog.md
hexo new "my first test blog"
- 編輯文章
---
title: 我的第一篇博客
date: 2024-02-06 14:55:30
tags:
- 博客
- Hexo
categories:
- 技術筆記
---## 歡迎來到我的博客這是我使用 Hexo 搭建的個人博客的第一篇文章。在這里,我將會分享:- 編程技術心得
- 學習筆記
- 項目經驗
- 個人感悟### Markdown 示例這是一個 Markdown 語法的簡單示例:```python
def hello_world():print("Welcome to my blog!")
```#### 列表示例
1. 第一項
2. 第二項
3. 第三項#### 引用示例
> 學習編程最好的方式就是動手實踐### 結語希望這個博客能夠記錄我的成長歷程,也希望能幫助到其他人!
- 本地預覽:在blog-source目錄下執行,生成靜態文件并啟動本地服務器來預覽
hexo clean && hexo generate && hexo server
若出現報錯
ERROR Cannot find module 'hexo' from 'D:\code\git\EndlessGo.github.io'
ERROR Local hexo loading failed in D:\code\git\EndlessGo.github.io
ERROR Try running: 'rm -rf node_modules && npm install --force'windwos下直接執行
npm install --force
npm audit fix
服務器已經啟動,你可以通過訪問 http://localhost:4000 來預覽你的博客
D:\blog\blog-source>hexo clean && hexo generate && hexo server
INFO Validating config
INFO Validating config
INFO Start processing
INFO Files loaded in 98 ms
INFO Generated: archives/2024/index.html
INFO Generated: archives/index.html
INFO Generated: archives/2025/index.html
INFO Generated: archives/2024/02/index.html
INFO Generated: archives/2025/08/index.html
INFO Generated: tags/Hexo/index.html
INFO Generated: index.html
INFO Generated: tags/博客/index.html
INFO Generated: categories/技術筆記/index.html
INFO Generated: css/style.css
INFO Generated: fancybox/jquery.fancybox.min.css
INFO Generated: js/script.js
INFO Generated: fancybox/jquery.fancybox.min.js
INFO Generated: js/jquery-3.6.4.min.js
INFO Generated: css/images/banner.jpg
INFO Generated: 2025/08/05/hello-world/index.html
INFO Generated: 2024/02/06/my-first-test-blog/index.html
INFO 17 files generated in 214 ms
INFO Validating config
INFO Start processing
INFO Hexo is running at http://localhost:4000/ . Press Ctrl+C to stop.
發布GitHub
當你要發布文章時,使用以下命令:
-
文章格式:
-
每篇文章開頭都有 Front-matter 配置(就是
---
之間的內容) -
可以設置標簽(tags)和分類(categories)
-
支持所有 Markdown 語法
-
-
圖片支持:
-
post_asset_folder: true
,這意味著每次創建新文章時,會自動創建同名文件夾 -
可以將圖片放在這個文件夾中,然后在文章中使用相對路徑引用
-
總結
-
寫作新文章:
-
使用命令
hexo new "文章標題"
創建新文章,位于source/_posts
目錄下對應的.md文件 -
使用任何支持 Markdown 的編輯器編輯文章
-
-
本地預覽
-
hexo clean && hexo generate && hexo server
-
訪問 http://localhost:4000 來預覽你的博客
-
-
發布github
- hexo clean && hexo generate && hexo deploy