这份记录把三件事串成一条线:把站点搭起来 → 接上评论系统 → 让评论真正能用。按顺序做完,一个带深色主题、静态托管、可评论的个人博客就成型了。

  • 一、从零搭建站点:环境检查、hexo init、主题、配置、背景图处理、统计、部署
  • 二、接入 Giscus 评论系统:方案对比、原理、配置
  • 三、安装 Giscus App:把评论区从”能看”变成”能发”

每一段都附了当时真实出现的报错与解决办法。

一、从零搭建站点(Hexo + Butterfly)

Hexo + Butterfly 个人博客搭建全程记录

记录日期:2026-10-26
站点目录:D:\<站点目录>
最终结果:Hexo 8.1.2 + Butterfly 5.7.0,默认深色模式,银河背景,2 篇文章,6 条友链,赞助页,GitHub Pages 部署已就绪

本文按”从装依赖到可上传”的真实顺序写,每一步都附上当时实际出现的报错与解决办法。第 11 节是报错速查表,遇到问题可以直接跳到那里。


0. 环境事实

项 版本 / 值
操作系统 Windows 10.0.26200
Node.js v24.21.0
npm 11.19.0
git 2.55.0.windows.5
hexo-cli 4.3.2
Hexo 本体 8.1.2(package.json 写 ^8.0.0)
主题 hexo-theme-butterfly 5.7.0
字数插件 hexo-wordcount 6.0.1
部署插件 hexo-deployer-git
站点 url https://fwind-hidden.github.io
GitHub 用户名 fwind-hidden(带横线,不是下划线)

1. 动手前的三项检查

这三项如果不先查,后面一定会在某个步骤莫名其妙失败。

1.1 版本确认

1
2
3
4
node -v
npm.cmd -v
git --version
hexo -v

1.2 PowerShell 执行策略(决定你敲命令的方式)

1
2
# 直接敲 npm -v 若报 SecurityError,就是这个原因
Get-ExecutionPolicy -List

症状:

1
File C:\Program Files\nodejs\npm.ps1 cannot be loaded because running scripts is disabled on this system.

两个选择:

  • 改策略(一劳永逸,属于改系统设置):
    1
    Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
  • 不改策略,改用 .cmd 入口(本文采用的方式):
    1
    2
    3
    npm.cmd -v
    hexo.cmd -v
    pnpm.cmd -v

注意:hexo、npm、pnpm 在 C:\Users\<你>\AppData\Roaming\npm\ 下都有 .ps1 和 .cmd 两份,.ps1 被执行策略拒绝时,.cmd 通常还能用。

1.3 git 的 HTTPS 通道

先测一下能不能连 GitHub:

1
git ls-remote --heads https://github.com/hexojs/hexo-starter.git

如果报:

1
2
fatal: unable to access 'https://github.com/...': schannel: AcquireCredentialsHandle failed:
SEC_E_NO_CREDENTIALS (0x8009030e) - 安全程序包没有可用的凭证

说明 Windows 自带的 SChannel 取证书凭据失败(在受限令牌/沙箱环境里很常见)。网络本身没问题,换 TLS 后端即可:

1
git config --global http.sslBackend openssl

验证:

1
2
git config --global --get http.sslBackend   # 输出 openssl
git ls-remote --heads https://github.com/hexojs/hexo-starter.git # 能列出分支即成功

回滚方法:

1
git config --global --unset http.sslBackend

判断”是网络问题还是本机 TLS 问题”的快速方法:用 Node 测一次 HTTPS。Node 用自带 OpenSSL,不走 SChannel。

1
node -e "const h=require('https');h.get('https://registry.npmjs.org/hexo',r=>{console.log(r.statusCode);r.destroy()})"

若这里返回 200,而 curl/git 全失败,那就是本机 TLS 后端问题,不是墙、也不是 VPN。


2. 初始化站点

1
2
cd D:\<站点目录>
hexo.cmd init .

正常输出:

1
2
INFO  Cloning hexo-starter https://github.com/hexojs/hexo-starter.git
INFO Install dependencies

生成的结构:

1
2
3
4
5
6
7
8
9
10
fwind_hidden_blog/
├─ _config.yml # 站点主配置
├─ _config.butterfly.yml # 主题配置(后加)
├─ package.json
├─ scaffolds/ # 文章模板
├─ source/
│ ├─ _posts/ # 文章
│ └─ _data/ # 友链等数据(后加)
├─ themes/ # 新版 starter 不再放主题,主题走 npm
└─ .gitignore

2.1 这里踩的第一个坑:starter 默认用 pnpm

新版 hexo-starter 自带 pnpm-lock.yaml 和 pnpm-workspace.yaml,hexo init 检测到就直接用 pnpm 装依赖,然后失败:

1
2
3
4
5
ERR_PNPM_IGNORED_BUILDS
× installing dependencies
╰─▶ Ignored build scripts: hexo-util@3.3.0, hexo-util@4.0.0
help: Run "pnpm approve-builds" to pick which dependencies should be allowed
WARN Failed to install dependencies. Please run 'npm install' in "..." folder.

两条路:

  • 继续 pnpm:pnpm install --allow-build 或 pnpm approve-builds
  • 改 npm(推荐,node_modules 扁平、行为可预期):
    1
    2
    3
    Remove-Item pnpm-lock.yaml, pnpm-workspace.yaml -Force
    Remove-Item node_modules -Recurse -Force
    npm.cmd install

2.2 npm 11 的新拦截:allowScripts

npm 装完会警告:

1
2
3
npm warn install-scripts 4 packages have install scripts not yet covered by allowScripts:
npm warn install-scripts hexo-util@4.0.0 (postinstall: npm run build:highlight)
npm warn install-scripts Run `npm install-scripts ls` to review, or `npm install-scripts approve <pkg>` to allow.

hexo-util 的 build:highlight 不跑,之后 hexo generate 会因为缺少高亮资源出错。放行:

1
npm.cmd install-scripts approve hexo-util

验证(dist 下应有 highlight.js):

1
Test-Path node_modules\hexo-util\dist\highlight.js

3. 安装主题与插件

1
2
3
4
5
6
7
8
9
10
cd D:\<站点目录>

# 主题
npm.cmd install hexo-theme-butterfly --save

# 字数统计(文章字数 + 阅读时长 + 全站总字数,Butterfly 明确依赖它)
npm.cmd install hexo-wordcount --save

# 一键部署(hexo d)
npm.cmd install hexo-deployer-git --save

启用主题:_config.yml 里

1
theme: butterfly

把主题的默认配置复制到站点根目录,之后只改这一份,主题升级不会覆盖:

1
Copy-Item node_modules\hexo-theme-butterfly\_config.yml _config.butterfly.yml

starter 自带的 landscape 主题可以清掉:

1
2
npm.cmd uninstall hexo-theme-landscape
Remove-Item _config.landscape.yml

4. 站点主配置 _config.yml

1
2
3
4
5
6
7
8
9
10
title: fwind_hidden的小破站
subtitle: '忍把浮名云散,换半分逍遥。风骤起,落红满径,自在芳草斜阳外。'
description: 'fwind_hidden 的个人博客,记录代码、阅读与生活。'
keywords: fwind_hidden,Hexo,Butterfly,技术笔记,生活随笔
author: fwind_hidden
language: zh-CN
timezone: Asia/Shanghai

url: https://fwind-hidden.github.io
permalink: :year/:month/:day/:title/

要点:

  • url 必须是最终域名。写错会影响 RSS、站内绝对链接、本地预览的资源路径。
  • language: zh-CN 决定主题中文文案与日期格式。
  • permalink 里的 :title 取自文件名,所以中文标题的文章请给一个 ASCII 文件名(见 9.3)。

4.1 写 YAML 的编码坑

不要用 Set-Content -Encoding utf8(Windows PowerShell 5.1 会写入 BOM),Hexo 解析带 BOM 的 YAML 有风险。用:

1
2
$enc = New-Object System.Text.UTF8Encoding($false)   # $false = 不带 BOM
[IO.File]::WriteAllText('D:\...\_config.yml', $content, $enc)

检查是否带 BOM:

1
[IO.File]::ReadAllBytes('_config.yml')[0..2] -join ','   # 239,187,191 就是有 BOM

5. 背景图 / Banner 处理(本文重点之一)

站点一共用到三处图片,作用不同、尺寸要求也不同:

配置项 作用范围 建议尺寸 实际做法
index_img 只有首页顶部 16:9 宽屏 1920×1080
default_top_img 其余所有页(文章/关于/友链/归档) 16:9 宽屏 1920×1080
avatar.img 头像(圆形显示) 正方形 400×400

5.1 图片放哪

统一放 source/img/,Hexo 会原样复制到 public/img/,页面里用 /img/xxx.jpg 引用。

1
2
3
4
5
6
7
8
source/img/
├─ avatar.png
├─ banner.jpg # 首页
├─ top.jpg # 其它页
├─ link-default.png # 友链占位头像
├─ posts/hello/ # 文章配图
├─ posts/flag/
└─ sponsor/ # 收款码、表情包

5.2 处理思路(三条经验)

  1. 原图往往太大:相机/NASA 原图 2000×1867、6.4 MB 这种,直接引用会让首屏加载十几秒。目标压到 300—700 KB。
  2. 必须裁剪,不能只缩放:Banner 区域是宽屏,近正方形的图会被 CSS 裁掉上下(background-size: cover),与其让浏览器乱裁,不如自己按 16:9 裁好,保证主体居中。
  3. 裁剪锚点要看图:银河/星野类图片主体通常在中上或中下,居中裁可能把主体切掉。例如银河带在图片高度 13% 处时,用顶部对齐裁(从 y=0 开始)比居中裁更好。

5.3 可复用的处理脚本(PowerShell + .NET,无需 PIL/ImageMagick)

等比缩放 + 居中裁剪到 16:9:

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
Add-Type -AssemblyName System.Drawing
$src = 'C:\原图.png'
$dst = 'D:\...\source\img\banner.jpg'
$img = [System.Drawing.Image]::FromFile($src)

# ① 先等比缩到目标宽度
$nw = 1920; $nh = [int]($img.Height * (1920.0 / $img.Width))
$stage = New-Object System.Drawing.Bitmap $nw, $nh
$g1 = [System.Drawing.Graphics]::FromImage($stage)
$g1.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$g1.DrawImage($img, 0, 0, $nw, $nh)
$g1.Dispose(); $img.Dispose()

# ② 居中裁 1920×1080(顶部对齐就把 $cropY 改成 0)
$cropY = [int](($nh - 1080) / 2)
$crop = New-Object System.Drawing.Bitmap 1920, 1080
$g2 = [System.Drawing.Graphics]::FromImage($crop)
$g2.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$g2.DrawImage($stage,
(New-Object System.Drawing.Rectangle 0,0,1920,1080),
(New-Object System.Drawing.Rectangle 0,$cropY,1920,1080),
[System.Drawing.GraphicsUnit]::Pixel)
$g2.Dispose(); $stage.Dispose()

# ③ 存 JPEG 并指定质量(90 基本无损,88 更省体积)
$codec = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { $_.MimeType -eq 'image/jpeg' }
$ps = New-Object System.Drawing.Imaging.EncoderParameters 1
$ps.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter ([System.Drawing.Imaging.Encoder]::Quality), 90
$crop.Save($dst, $codec, $ps)
$crop.Dispose()

头像裁正方形(居中):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Add-Type -AssemblyName System.Drawing
$img = [System.Drawing.Image]::FromFile('C:\Users\<用户名>\Pictures\星级星源.png') # 704×546
$side = [Math]::Min($img.Width, $img.Height) # 546
$sx = [int](($img.Width - $side) / 2); $sy = [int](($img.Height - $side) / 2)
$bmp = New-Object System.Drawing.Bitmap 400, 400
$g = [System.Drawing.Graphics]::FromImage($bmp)
$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$g.DrawImage($img,
(New-Object System.Drawing.Rectangle 0,0,400,400),
(New-Object System.Drawing.Rectangle $sx,$sy,$side,$side),
[System.Drawing.GraphicsUnit]::Pixel)
$g.Dispose()
$bmp.Save('D:\...\source\img\avatar.png', [System.Drawing.Imaging.ImageFormat]::Png)
$bmp.Dispose(); $img.Dispose()

5.4 本站实际处理参数

图 原始 处理后 说明
首页 Banner(银河北斗星系) 2000×1867 / 6.4 MB 1920×1080 / 517 KB 居中裁,上下各去 356px;质量 90
其它页顶图(银河全景) 2048×1536 / 约 1.1 MB 1920×1080 / 706 KB 顶部对齐裁,保住银河带;质量 88
头像 704×546 Png 400×400 / 326 KB 居中裁正方形
文章配图(升旗 3 张) 4608×3456 / 3—5 MB 1600×1200 / 165—387 KB 宽 1600 + 质量 85
文章配图(Hello 2 张) 1079×1079 1079×1079 / 123、144 KB 本来就小,只重编码

5.5 配置写法(_config.butterfly.yml)

1
2
3
4
5
6
index_img: /img/banner.jpg
default_top_img: /img/top.jpg

avatar:
img: /img/avatar.png
effect: false # true = 鼠标悬停头像旋转

default_top_img 一设,所有没有单独指定 top_img 的页面都会用它,包括关于、友链、赞助、归档页。


6. Butterfly 主题配置

6.1 默认深色模式

1
2
3
4
5
6
7
8
9
display_mode: dark      # light / dark

darkmode:
enable: true
button: true # 右上角保留切换按钮
autoChangeMode: false

code_blocks:
theme: darker # 深色页面配深色代码块(可选 darker / pale night / light / ocean / false)

配置文件改了必须重启 hexo s,见 11 节 E06。

6.2 导航菜单

1
2
3
4
5
6
7
8
menu:
首页: / || fas fa-home
归档: /archives/ || fas fa-archive
标签: /tags/ || fas fa-tags
分类: /categories/ || fas fa-folder-open
友链: /link/ || fas fa-link
赞助: /sponsor/ || fas fa-coffee
关于: /about/ || fas fa-user

格式是 名称: 路径 || FontAwesome 图标。

6.3 社交图标

1
2
3
4
5
social:
fab fa-github: https://github.com/fwind-hidden || GitHub || '#24292e'
fas fa-envelope: mailto:<你的邮箱> || Email || '#4a7dbe'
fab fa-bilibili: https://space.bilibili.com/326093280 || BiliBili || '#00A1D6'
fas fa-broadcast-tower: https://www.qrz.com/db/BH7IBD || QRZ || '#8B6914'

格式是 图标: 链接 || 提示文字 || 品牌色。

6.4 Follow Me 按钮(容易漏的坑)

侧边栏作者卡片的按钮,站点配置从主题模板复制过来时带着占位地址:

1
2
3
4
5
6
7
8
9
10
11
12
aside:
card_author:
enable: true
description:
button:
enable: true
icon: fab fa-github
text: Follow Me
link: https://github.com/xxxxxx # ← 必须改成你的
card_announcement:
enable: true
content: 这里写公告内容

6.5 首页副标题打字机

1
2
3
4
5
6
7
8
subtitle:
enable: true
effect: true
typed_option:
typeSpeed: 45 # 默认 150,越小越快
source: false
sub:
- "忍把浮名云散,换半分逍遥。\n风骤起,落红满径,自在芳草斜阳外。"

两个关键点:

  1. typed_option 是覆盖 typed.js 的默认参数。主题模板里写的是
    Object.assign({typeSpeed:150, backSpeed:50, loop:true, startDelay:300}, <你的typed_option>),
    所以页面源码里永远能看到 typeSpeed: 150——那是左侧默认值,你的参数在右侧对象里才生效。判断是否生效要看右侧。
  2. sub 里要显示多行,字符串必须用双引号(YAML 单引号里的 \n 是字面反斜杠 n,不会被解析为换行),并且要配一条 CSS,否则 HTML 会把换行符当空格吃掉:
1
2
3
inject:
head:
- '<style>#site-subtitle #subtitle { white-space: pre-wrap; }</style>'

6.6 页脚配色(主题写死了颜色)

node_modules/hexo-theme-butterfly/source/css/_layout/footer.styl 里:

1
2
3
#footer
position: relative
background-color: $light-blue // = #49b1f5,且没有深色模式覆盖

主题没有提供配置项来改它,只能注入 CSS 覆盖:

1
2
3
inject:
head:
- '<style>#footer{background-color:#12151c!important;background-image:none!important}#footer>*, #footer .copyright, #footer .framework-info{color:#aeb9c6!important}#footer a{color:#aeb9c6!important}#footer a:hover{color:#49b1f5!important}</style>'

副作用:浅色模式下页脚也会是深色(主题本身不区分)。要区分就再加 [data-theme="light"] #footer{...}。

6.7 文章 meta(日期 / 分类 / 标签)

1
2
3
4
5
6
7
8
9
10
11
12
post_meta:
page: # 首页卡片
date_type: created
date_format: date
categories: true
tags: true
post: # 文章页
position: left
date_type: both # created / updated / both
date_format: date
categories: true
tags: true

7. 文章字数 / 阅读时间 / 全站统计

7.1 文章级(插件方案)

装 hexo-wordcount,然后主题配置打开:

1
2
3
4
5
6
# Need to install the hexo-wordcount plugin
wordcount:
enable: true
post_wordcount: true # 文章字数
min2read: true # 阅读时长(中文 300 字/分钟)
total_wordcount: true # 侧边栏全站总字数

效果(文章页 meta 一行):

1
发表于 2026-10-26 | 更新于 2026-10-26 | 随笔 | 总字数: 246 | 阅读时长: 1分钟

插件注册的三个 helper:wordcount(content)、min2read(content)、totalcount(site)。

7.2 页面级(自定义 tag,构建时统计)

“关于”页要显示”文章数 / 总字数”,而 markdown 里拿不到站点的实时数据。做法是写一个 Hexo tag 插件,在构建时把数字算好写进 HTML。

scripts/site-stat.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
34
35
36
37
38
39
40
41
42
43
44
'use strict';

// {% site_stat %} —— 构建时统计全站文章数与总字数,输出表格
// 字数算法与 hexo-wordcount 一致:中文字符逐字 + 英文/数字按词
var stripHTML = require('hexo-util').stripHTML;

var BUILT_AT = '2026-10-26';
var CONTENT_DESC = '一些思考,一点想法,一段笔记。';

function count(content) {
content = stripHTML(content || '');
var cn = (content.match(/[\u4E00-\u9FA5]/g) || []).length;
var en = (content.replace(/[\u4E00-\u9FA5]/g, '')
.match(/[a-zA-Z0-9_\u0392-\u03c9\u0400-\u04FF]+|\w+/g) || []).length;
return cn + en;
}

hexo.extend.tag.register('site_stat', function () {
var posts;
try {
posts = hexo.locals.get('posts').toArray(); // 关键:不要用 this.site
} catch (e) {
posts = [];
}

var total = 0;
posts.forEach(function (post) {
total += count(post.content || post._content || '');
});

var rows = [
['文章数', posts.length + ' 篇'],
['总字数', total.toLocaleString('en-US') + ' 字'],
['建站时间', BUILT_AT],
['内容', CONTENT_DESC]
];

var body = rows.map(function (r) {
return '<tr><td>' + r[0] + '</td><td>' + r[1] + '</td></tr>';
}).join('');

return '<table><thead><tr><th>项目</th><th>内容</th></tr></thead><tbody>'
+ body + '</tbody></table>';
});

markdown 里直接:

1
2
3
## 本站信息

{% site_stat %}

优点:数字跟着构建自动更新,加文章不用手改;页面是纯静态,不依赖任何外部 API。

注意 scripts/ 目录是 Hexo 约定的站点级插件目录,改完脚本同样要重启 hexo s。


8. 页面(page)与前端结构

Hexo 有两类内容,别混:

文章(post) 页面(page)
位置 source/_posts/xxx.md source/<名字>/index.md
路径 /2026/10/26/标题/ /about/
进首页列表 是 否
进归档/标签/分类 是 否

本站建了 5 个页面:

1
2
3
4
5
source/about/index.md        →  /about/     关于(含 {% site_stat %})
source/tags/index.md → /tags/ 标签云 front-matter 需 type: tags
source/categories/index.md → /categories/ 分类 front-matter 需 type: categories
source/link/index.md → /link/ 友链 front-matter 需 type: link
source/sponsor/index.md → /sponsor/ 赞助

tags / categories 两个页面的 type 字段不能漏,否则 Butterfly 不会渲染成标签墙/分类墙。

8.1 友链数据

页面 source/link/index.md 只负责”壳”,内容来自 source/_data/link.yml:

1
2
3
4
5
6
7
- class_name: 友情链接
class_desc: 那些人,那些事
link_list:
- name: 洪流
link: https://hongliu.icu
avatar: /img/link-default.png
descr: 是个无线电大佬

没有头像图时用一张自制占位图(/img/link-default.png,200×200 深色渐变 + “友”字),避免出现破图。

8.2 赞助页

一张收款码 + 一个表情包 + 文案,用内联样式排版(暗色模式下也不会违和):

1
2
3
4
5
6
7
8
9
10
11
12
---
title: 赞助我
date: 2026-10-26 12:00:00
---

<div style="display:flex;justify-content:center;margin:8px 0 20px;">
<img src="/img/sponsor/meme.png" alt="能给我打钱吗,哥" style="width:165px;max-width:42%;border-radius:14px;">
</div>

<div style="display:flex;justify-content:center;margin:24px 0 8px;">
<img src="/img/sponsor/qrcode.jpg" alt="微信收款码" style="width:260px;max-width:70%;border-radius:12px;box-shadow:0 6px 20px rgba(0,0,0,.18);">
</div>

9. 文章排版与配图

9.1 front-matter 模板

1
2
3
4
5
6
7
8
9
10
11
---
title: "print('Hello World')"
date: 2026-10-26 12:00:00
categories:
- 随笔
tags:
- 随笔
- 记录
---

正文……

9.2 图片放在正文中的位置

按原文位置放,别偷懒堆到文末。示例文章的两张图就是夹在两句”吊胃口”的话之间,插在文末就毁了节奏。

1
2
3
4
5
都说了没有什么东西可以给你看的。

![图片](/img/posts/hello/hello-2.jpg)

好吧……

9.3 中文标题的 URL

permalink 的 :title 取自文件名(不是 front-matter 的 title)。所以:

  • 文件命名用 ASCII:print-hello-world.md、ji-xue-xiao-sheng-qi.md
  • 页面里显示的仍是 front-matter 里的中文/符号标题
  • 最终地址:/2026/10/26/print-hello-world/

9.4 从 docx 取内容

首选 Office COM 工具(本机有 mcp__office__*)。若不可用,docx 本质是 zip,可以直接解 XML:

1
2
3
4
5
6
import zipfile, re
with zipfile.ZipFile(path) as z:
xml = z.read("word/document.xml").decode("utf-8", "replace")
for para in re.findall(r"<w:p[ >].*?</w:p>", xml, re.S):
text = "".join(re.findall(r"<w:t[^>]*>(.*?)</w:t>", para, re.S))
print(text)

图片在 word/media/ 下。注意:

  • 图片可能是 VML(<v:imagedata>)而非 DrawingML(<a:blip>),判断位置时两种都要查
  • docx 里的 OLE 内嵌对象(提取出来是 ⑖ 这类怪异字符)markdown 无法承载,只能丢弃
  • 中文输出到控制台会乱码,见 E07;把结果写成 UTF-8 文件再读

10. 部署准备(GitHub Pages)

10.1 已完成的部分(本地)

1
2
3
4
cd D:\<站点目录>

npm.cmd install hexo-deployer-git --save
git init -b main

_config.yml 写入部署目标:

1
2
3
4
5
deploy:
type: git
repo: https://github.com/fwind-hidden/fwind-hidden.github.io.git
branch: gh-pages
message: "{{ now('YYYY-MM-DD HH:mm:ss') }} 更新站点"

.gitignore 必须排除生成物(starter 已自带,核对一下):

1
2
3
4
node_modules/
public/
db.json
.deploy*/

首次提交源码:

1
2
git add -A
git commit -m "chore: 初始化 Hexo 博客源码"

当前状态:main 分支保存源码(30 个文件),hexo d 会把 public/ 推到 gh-pages 分支。

10.2 还需要你做的(需要联网 + 你的账号)

  1. 在 GitHub 建仓库:名字必须是 fwind-hidden.github.io(用户主页仓库),不要勾选初始化 README。
  2. 配置推送认证(二选一):
    • PAT:GitHub → Settings → Developer settings → Personal access tokens,勾 repo 权限。推送时用户名填 fwind-hidden,密码填 token。
    • SSH:ssh-keygen -t ed25519 -C "<你的邮箱>",把 ~/.ssh/id_ed25519.pub 加到 GitHub → Settings → SSH keys;然后把 deploy.repo 改成 git@github.com:fwind-hidden/fwind-hidden.github.io.git。
  3. 发布:
    1
    2
    3
    hexo.cmd clean
    hexo.cmd generate
    hexo.cmd deploy # 或 hexo.cmd d
  4. GitHub 仓库设置:Settings → Pages → Source 选 Deploy from a branch → 分支 gh-pages → / (root) → Save。等 1—2 分钟,访问 https://fwind-hidden.github.io。
  5. 源码备份(强烈建议):把 main 分支(源码)推到另一个私有仓库,例如 fwind-hidden.github.io-source:
    1
    2
    git remote add origin https://github.com/fwind-hidden/fwind-hidden.github.io-source.git
    git push -u origin main
    否则生成物一覆盖,源码就只剩本地一份。

10.3 部署后的预期差异

  • 浏览量:Butterfly 的 busuanzi 统计依赖外部服务,本地离线显示为空,上线后自动正常
  • 本地预览的 hexo s 是动态生成,线上是静态文件,行为一致但不再自动重算 {% site_stat %} 以外的内容
  • 记得每次改完:hexo clean && hexo generate && hexo deploy

11. 报错速查表

编号 症状 根因 解决
E01 npm.ps1 / hexo.ps1 cannot be loaded because running scripts is disabled PowerShell 执行策略禁脚本 改用 npm.cmd / hexo.cmd,或 Set-ExecutionPolicy -Scope CurrentUser RemoteSigned
E02 schannel: AcquireCredentialsHandle failed: SEC_E_NO_CREDENTIALS (0x8009030e) Windows SChannel 在受限环境下取不到凭据;网络本身正常 git config --global http.sslBackend openssl
E03 ERR_PNPM_IGNORED_BUILDS ... Ignored build scripts: hexo-util 新版 starter 自带 pnpm 配置,pnpm 默认拦构建脚本 删 pnpm-lock.yaml pnpm-workspace.yaml node_modules 后改用 npm
E04 npm warn install-scripts ... hexo-util (postinstall: npm run build:highlight) npm 11 的 allowScripts 机制默认拦 postinstall npm.cmd install-scripts approve hexo-util
E05 EPERM: operation not permitted, mkdir 'D:\...\.github' / Error: D:/.../.git: Permission denied 目标目录在文件沙箱允许范围之外 放宽该目录写权限(或把站点建在工作区/用户目录内)
E06 改了 _config.yml,页面还是旧标题/旧作者 hexo s 不热更新站点主配置和 scripts/ 重启 hexo s;_config.butterfly.yml 和文章改动才自动热更新
E07 控制台里中文全是 ������,文件名显示 ��� 控制台按 GBK 解码 UTF-8 读文件时显式 -Encoding UTF8;Python 前设置 $env:PYTHONIOENCODING='utf-8';不要据此判断文件损坏
E08 YAML 解析异常 / 页面首行多出怪字符 PowerShell Set-Content -Encoding utf8 写了 BOM 用 [IO.File]::WriteAllText(path, text, (New-Object System.Text.UTF8Encoding($false)))
E09 Template render error: TypeError: Cannot read properties of undefined (reading 'posts') 自定义 tag 里用了 this.site,页面渲染上下文里没有 改用模块级 hexo.locals.get('posts').toArray()
E10 页脚永远是亮蓝色 #49b1f5,深色模式下也不变 主题 footer.styl 写死 background-color: $light-blue,无配置项 inject.head 注入 #footer{background-color:...!important}
E11 改了 typed_option 但速度没变 主题模板里 Object.assign(默认值, 你的参数),页面上仍能看到默认值 检查覆盖对象(Object.assign 第二个参数)里是否是你的新值
E12 sub 里写了 \n 但不换行 HTML 把换行符折叠成空格 配 #subtitle { white-space: pre-wrap; };YAML 用双引号
E13 中文文章 URL 变成一长串 %E5%BF%8D... :title 取自文件名 文件名用 ASCII,标题写在 front-matter 里
E14 hexo g 卡很久 / docx 体积巨大 文章内嵌图是 4608×3456 原图 压到宽 1600、JPEG 质量 85
E15 用编辑器打开 .md 看不到图 写的是站点绝对路径 /img/... 本地查看时临时改相对路径 images/...;博客里保持 /img/...
E16 后台预览服务突然消失 沙箱里的后台进程随会话结束被回收 重新 hexo.cmd server -p 4000
E17 首页没有任何文章 source/_posts 为空 至少放一篇 .md,或别删光示例文章

12. 命令速查

1
2
3
4
5
6
7
8
9
10
11
12
cd D:\<站点目录>

hexo.cmd new "文章标题" # 新建文章到 source/_posts
hexo.cmd new page "页面名" # 新建页面到 source/页面名/index.md
hexo.cmd clean # 清缓存与 public
hexo.cmd generate # 生成静态站到 public
hexo.cmd server -p 4000 # 本地预览 http://127.0.0.1:4000
hexo.cmd deploy # 推送到 gh-pages
hexo.cmd clean; hexo.cmd generate; hexo.cmd deploy # 标准发布三连

npm.cmd install <包> --save # 装插件
git status / git log --oneline # 源码版本

13. 目录结构(当前)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
D:\<站点目录>\
├─ _config.yml # 站点配置(含 deploy)
├─ _config.butterfly.yml # 主题配置(约 1100 行,全部定制都在这)
├─ package.json
├─ scripts\
│ └─ site-stat.js # {% site_stat %} 全站统计标签
├─ scaffolds\
├─ source\
│ ├─ _posts\
│ │ ├─ print-hello-world.md
│ │ └─ ji-xue-xiao-sheng-qi.md
│ ├─ _data\
│ │ └─ link.yml # 友链数据
│ ├─ about\index.md
│ ├─ tags\index.md
│ ├─ categories\index.md
│ ├─ link\index.md
│ ├─ sponsor\index.md
│ └─ img\
│ ├─ avatar.png banner.jpg top.jpg link-default.png
│ ├─ posts\hello\ posts\flag\
│ └─ sponsor\qrcode.jpg meme.png
├─ themes\
└─ public\ # 生成物,不进 git

14. 后续待办

  • 在 GitHub 建 fwind-hidden.github.io 仓库并配置 PAT / SSH
  • 首次 hexo d 并开启 Pages(gh-pages 分支)
  • 源码另推一个私有仓库做备份
  • 友链补充真实名称与描述(当前 6 条按 友链.txt 填写,其中 fffuria、BI7KHI、广东工业大学 Wiki 三个名称是推断的)
  • 需要时再加:评论系统(Waline / Twikoo)、搜索(local-search)、文章目录、RSS 优化

二、接入 Giscus 评论系统

GitHub Pages 只托管静态文件,没有数据库,所以评论必须”存在别处”。这篇记录本站接入 Giscus 的全过程,含踩坑点。

1. 四种方案怎么选

方案 评论存在哪 需要后端 登录方式 适用
Giscus 仓库 Discussions 否 GitHub 推荐,读者有 GitHub 账号即可
Utterances 仓库 Issues 否 GitHub 界面朴素,把评论当 issue 管理
Waline 自建数据库 是(Vercel/LeanCloud) 微信/匿名/邮箱 不想让读者登录 GitHub
Twikoo 自建数据库 是 微信/QQ/邮箱 功能最全,部署最麻烦

本站选 Giscus:零后端、零费用、和站点同仓库、支持暗色模式自动切换。

2. 原理

Giscus 是一个 GitHub App + 前端脚本:

  1. 页面加载时插入一段 <script src="https://giscus.app/client.js">
  2. 脚本按 data-mapping 规则把当前页面映射成一个 Discussion
  3. 读写评论全部走 GitHub API,权限就是读者自己的 GitHub 账号

所以它需要的配置就是:仓库、仓库 ID、分类 ID、映射方式。

3. 实操

1. 开启 Discussions

仓库 Settings 里勾选 Discussions,或者用 API:

1
2
PATCH /repos/{owner}/{repo}
{ "has_discussions": true }

2. 拿 repo_id 和 category_id

这两个 ID 只能通过 GraphQL 查,REST 接口没有:

1
2
3
4
5
6
7
8
query {
repository(owner: "fwind-hidden", name: "fwind-hidden.github.io") {
id
discussionCategories(first: 25) {
nodes { id name slug }
}
}
}

本站拿到的值:

1
2
repo_id              = R_kgDOVXzZrw
Announcements 分类 = DIC_kwDOVXzZr84DJpuF

分类建议选 Announcements:只有你能在里面新建讨论,读者的评论都会挂在已有讨论下,不会污染分类。

3. 写 Butterfly 配置

改 _config.butterfly.yml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
comments:
# 系统名必须大小写一致
use: Giscus
text: true
# 开启后评论数统计会失效
lazyload: false
count: false

giscus:
repo: fwind-hidden/fwind-hidden.github.io
repo_id: R_kgDOVXzZrw
category_id: DIC_kwDOVXzZr84DJpuF
light_theme: light
dark_theme: dark
js:
option:
data-lang: zh-CN
data-mapping: pathname
data-strict: 0
data-reactions-enabled: 1
data-input-position: top
data-loading: lazy

两个要点:

  • option 里的键会原样变成 giscus 脚本的 data-* 属性,所以键名必须用带 data- 前缀的写法
  • light_theme / dark_theme 对应站点的明暗模式,Butterfly 会在切换主题时通过 postMessage 通知 iframe 换肤,不需要刷新

4. 生成并发布

1
2
3
hexo.cmd clean
hexo.cmd generate
hexo.cmd deploy

5. 装 GitHub App(最容易漏)

打开 https://github.com/apps/giscus → Install → 选择你的仓库。

没装的话评论区会直接报 giscus is not installed on this repository,前端看起来”加载了但用不了”。

4. 验证

  • 文章页源码里应有 #giscus-wrap 容器和 giscus.app/client.js
  • 用 pathname 映射时,每篇文章自动对应一个讨论,改标题也不会重复建讨论
  • 首次有人评论后,仓库 Discussions 里会出现对应条目

5. 踩过的坑

现象 原因 解决
全站 404 仓库推送了但 GitHub Pages 没开 Settings → Pages 选 gh-pages 分支,约 60—90 秒生效
评论区空了 只填了 repo,没填两个 ID ID 必须用 GraphQL 查,REST 拿不到
评论框不出现 comments.use 写成 giscus 必须首字母大写 Giscus
换肤后颜色不对 只配了一个主题 light_theme 和 dark_theme 都要配
评论数永远是 0 开了 lazyload 关掉 lazyload

小结

Giscus 的配置量不大,真正容易卡住的是三件事:Pages 有没有开、两个 ID 有没有用 GraphQL 拿对、App 有没有装。把这三步做完,剩下的就只是贴一段 YAML。

三、安装 Giscus App,把评论区点亮

上一篇记录了 Giscus 在 Butterfly 里的配置。但配置完只是”页面上长出了评论区”,离真正能发评论还差最后一步:安装 GitHub App。这篇只讲操作链路,每一步说清点哪儿、看到什么、失败长什么样。

1. 先理解:这里有两层授权

很多人卡住,是因为把两件事当成了一件:

层级 谁来做 授权对象 作用
App 安装 仓库主人(你,一次) 仓库 允许 giscus 读写这个仓库的 Discussions
个人授权 每个评论者(首次) 自己的 GitHub 账号 允许 giscus 代表他发评论

你只需要做第一层。第二层由读者在点「登录」时自己完成。

两层都没做时,评论区显示的是 giscus is not installed on this repository——这个报错只跟第一层有关。

2. 操作链路

步骤 1 · 打开 App 页面

1
https://github.com/apps/giscus

先确认右上角登录的是仓库所属账号。登录错账号是后面所有失败的头号原因。

步骤 2 · 点 Install

页面右上角绿色 Install 按钮。

如果这里显示的是 Configure,说明已经装过,直接跳到步骤 5 检查范围。

步骤 3 · 选择安装位置

GitHub 要求 App 装在”账号或组织”上,不是直接装在仓库上:

  • 仓库属于个人 → 选个人账号
  • 仓库属于组织 → 选组织

选错的话,下一步的仓库列表里根本搜不到你的仓库。

步骤 4 · 限定仓库范围(唯一容易出错的一步)

进入 Install giscus 配置页,找到 Repository access:

选项 建议
All repositories 不选(等于授权给你所有仓库)
Only select repositories 选它,然后只勾目标仓库

下拉框支持输入仓库名搜索。勾完点绿色 Install。

这一步没勾上仓库,后面一定报 not installed——而且安装页已经”成功”跳走了,很容易误以为装好了。

步骤 5 · 核对安装结果

两个入口任选其一:

1
2
3
4
5
https://github.com/settings/installations
→ giscus → Configure → 看 Repository access

https://github.com/<用户名>/<仓库名>/settings/installations
→ 应能看到 giscus

正确的状态:Only select repositories (1),且列表里就是你的博客仓库。

步骤 6 · 看评论区的变化

打开任意文章页,滚到最底部:

显示 含义
红色 giscus is not installed on this repository 步骤 4 没勾上仓库,回去重配
Sign in with GitHub 按钮 安装成功
一片空白 多半是缓存,强制刷新(Ctrl+F5)

步骤 7 · 发第一条评论

  1. 点 Sign in with GitHub
  2. 弹出 GitHub 页面 → 点 Authorize giscus(这是第二层授权,代表”你自己”)
  3. 回到页面,出现输入框
  4. 输入内容,提交

步骤 8 · 确认评论存到了仓库

1
https://github.com/<用户名>/<仓库名>/discussions

应该多出一条讨论,标题就是文章路径(配置里 data-mapping: pathname 的效果):

1
/2026/10/26/文章-slug/

每篇文章对应一条讨论,所以改文章标题不会重复建讨论。

3. 卡点速查

现象 原因 处理
只有 Configure 没有 Install 之前装过 去 Configure 检查范围
仓库下拉搜不到博客仓库 登录账号或安装位置选错 确认账号,重选安装位置
装完仍报 not installed 范围没勾上目标仓库 回 settings/installations 重配
点登录后反复跳回 浏览器拦第三方 Cookie 放行 giscus.app
评论提交成功但页面不显示 iframe 缓存 等 30 秒或强刷
讨论建到了错误的分类 category_id 不对 用 GraphQL 重新取分类 ID

4. 小结

Giscus 的配置量其实很小,真正的时间都花在三件事上:装 App 时有没有勾对仓库、两层授权有没有分清、报错是不是真的来自 App 未安装。把这三件事拆开看,剩下的都是复制粘贴。


说明:本文由 AI 撰写,内容基于实际搭建与验证过程整理。