hexo-comments-gitment 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 huazie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,340 @@
1
+ # Hexo Comments Gitment
2
+
3
+ [![NPM version](https://img.shields.io/npm/v/hexo-comments-gitment.svg?style=flat&logo=npm)](https://www.npmjs.com/package/hexo-comments-gitment)
4
+ [![Required Node.js version](https://img.shields.io/node/v/hexo-comments-gitment.svg?style=flat&logo=node.js)](https://nodejs.org/en/download/)
5
+ [![Required Hexo version](https://img.shields.io/badge/hexo-%3E%3D%205.3.0-blue.svg?style=flat&logo=hexo)](https://hexo.io/)
6
+ [![License](https://img.shields.io/npm/l/hexo-comments-gitment.svg?style=flat)](https://github.com/huazie/diversity-plugins/blob/main/packages/hexo-comments-gitment/LICENSE)
7
+ [![GitHub Repo stars](https://img.shields.io/github/stars/huazie/diversity-plugins?style=flat)](https://github.com/huazie/diversity-plugins/stargazers)
8
+
9
+ 轻松集成 [Gitment](https://github.com/imsun/gitment) 评论系统到您的 Hexo 博客中,基于 GitHub Issues 的轻量级评论解决方案。
10
+
11
+ [英文说明/English Documentation](README_EN.md)
12
+
13
+ ## 功能特性
14
+
15
+ | 特性 | 描述 | 优势 |
16
+ |------|------|------|
17
+ | **GitHub 集成** | 基于 GitHub Issues,无需数据库 | 零维护成本,高可用性 |
18
+ | **OAuth 认证** | 支持 GitHub OAuth 安全登录 | 保护用户隐私,安全可靠 |
19
+ | **主题切换** | 支持亮色/暗色主题自动切换 | 完美适配各种主题风格 |
20
+ | **响应式设计** | 适配各种设备屏幕 | 移动端友好的用户体验 |
21
+ | **Markdown 支持** | 支持 Markdown/GFM 语法 | 代码高亮,格式丰富 |
22
+ | **易于配置** | 简单的 YAML 配置 | 快速上手,灵活定制 |
23
+
24
+ ## 快速开始
25
+
26
+ ### 安装插件
27
+
28
+ ```bash
29
+ # 1. 安装多评论系统核心插件(必需)
30
+ npm install hexo-generator-comments --save
31
+
32
+ # 2. 安装 Gitment 评论插件
33
+ npm install hexo-comments-gitment --save
34
+ ```
35
+
36
+ > **提示**:`hexo-comments-gitment` 需要与 `hexo-generator-comments` 搭配使用
37
+ > 更多信息:[hexo-generator-comments](https://github.com/huazie/diversity-plugins/tree/main/packages/hexo-generator-comments)
38
+
39
+ ## 配置指南
40
+
41
+ ### 基本配置
42
+
43
+ 在 Hexo 站点配置 `_config.yml` 或 主题配置 `_config.yml` 、`_config.[theme].yml` 中添加以下内容:
44
+
45
+ ```yaml
46
+ gitment:
47
+ # 是否启用 Gitment 评论系统
48
+ enable: false
49
+ # 是否启用加载提示(评论加载时显示加载动画)
50
+ loading: true
51
+ # GitHub 仓库所有者
52
+ owner: your-github-id
53
+ # 用于存储评论的 GitHub 仓库名
54
+ repo: your-repo-name
55
+ # GitHub Application Client ID
56
+ client_id: your-client-id
57
+ # GitHub Application Client Secret
58
+ client_secret: your-client-secret
59
+ # 指定 GitHub Issue 的匹配规则,配置了这个会替换 id 属性的值
60
+ # 其中 pathname | url | title 用来匹配 issue 的标签,`自定义字符串` 是自定义其他页面唯一标识
61
+ issue_term: pathname
62
+ # 页面唯一标识(可选,默认使用 window.location.href)
63
+ id:
64
+ # 页面标题(可选,默认使用 document.title)
65
+ title:
66
+ # 页面链接(可选,默认使用 window.location.href)
67
+ link:
68
+ # 页面描述(可选)
69
+ desc:
70
+ # Issue 标签列表(可选)
71
+ labels: []
72
+ # 评论每页显示数量(可选,默认 20)
73
+ per_page: 20
74
+ # 评论最大高度限制(px),超过则折叠(可选,默认 250)
75
+ max_comment_height: 250
76
+ # OAuth 代理地址(可选),默认代理 gh-oauth.imsun.net 已失效,需自行部署
77
+ proxy:
78
+ ```
79
+
80
+ > **重要**:请将配置中的占位符替换为您的实际 GitHub 应用信息
81
+
82
+ ### 配置选项详解
83
+
84
+ | 选项 | 类型 | 默认值 | 必填 | 描述 |
85
+ |------|------|--------|------|------|
86
+ | `enable` | Boolean | `false` | 是 | 是否启用 Gitment 评论系统 |
87
+ | `loading` | Boolean | `true` | 否 | 是否启用加载提示(评论加载时显示加载动画) |
88
+ | `owner` | String | - | 是 | GitHub 仓库所有者的用户名 |
89
+ | `repo` | String | - | 是 | 用于存储评论的 GitHub 仓库名称 |
90
+ | `client_id` | String | - | 是 | GitHub Application 的 Client ID |
91
+ | `client_secret` | String | - | 是 | GitHub Application 的 Client Secret |
92
+ | `issue_term` | String | `pathname` | 否 | Issue 匹配规则,可选值见下方说明 |
93
+ | `id` | String | `window.location.href` | 否 | 页面唯一标识,用于区分不同页面的评论 |
94
+ | `title` | String | `document.title` | 否 | 页面标题,用作 issue 的标题 |
95
+ | `link` | String | `window.location.href` | 否 | 页面链接,用于 issue 的正文中 |
96
+ | `desc` | String | `''` | 否 | 页面描述,用于 issue 的正文中 |
97
+ | `labels` | Array | `[]` | 否 | 创建 issue 时添加的标签列表 |
98
+ | `per_page` | Number | `20` | 否 | 评论每页显示数量 |
99
+ | `max_comment_height` | Number | `250` | 否 | 评论最大高度限制(px),超过则折叠 |
100
+ | `proxy` | String | - | 否 | OAuth 代理地址,用于解决认证问题 |
101
+
102
+ ### 高级配置选项
103
+
104
+ **issue_term 映射方式**
105
+
106
+ | 值 | 描述 | 适用场景 |
107
+ |---|------|----------|
108
+ | `pathname` | 使用页面路径作为页面唯一标识 | **推荐**,适合大多数场景 |
109
+ | `url` | 使用页面完整 URL 作为页面唯一标识 | 需要包含域名信息时 |
110
+ | `title` | 使用页面标题作为页面唯一标识 | 希望 issue 标题更友好 |
111
+ | `[自定义字符串]` | 指定自定义的唯一标识 | 手动管理评论 |
112
+
113
+ **proxy 代理说明**
114
+
115
+ 由于 Gitment 默认的 OAuth 代理 `gh-oauth.imsun.net` 已失效,用户登录时会报 `ERR_SSL_PROTOCOL_ERROR`。你需要自行部署一个 OAuth 代理服务,常见方案:
116
+
117
+ - **Vercel Serverless** — 部署一个简单的代理函数
118
+ - **Cloudflare Workers** — 使用 Workers 转发请求
119
+ - **自建服务器** — 部署 Node.js 代理
120
+
121
+ 部署好后,将代理地址配置到 `proxy` 字段即可:
122
+
123
+ ```yaml
124
+ gitment:
125
+ proxy: https://your-proxy-server.com
126
+ ```
127
+
128
+ ### 支持的模板引擎
129
+
130
+ 本插件支持所有使用以下模板引擎的 Hexo 主题:
131
+
132
+ | 模板引擎 | 文件扩展名 | 支持状态 |
133
+ |----------|------------|----------|
134
+ | **EJS** | `.ejs` | ✅ 完全支持 |
135
+ | **Nunjucks** | `.njk` | ✅ 完全支持 |
136
+ | **JSX + Inferno** | `.jsx` | ✅ 完全支持 |
137
+
138
+ ## 使用前提
139
+
140
+ 在开始使用之前,请确保满足以下条件:
141
+
142
+ ### 1. GitHub 仓库准备
143
+ - 拥有一个 **公开的** GitHub 仓库
144
+ - 仓库已启用 Issues 功能
145
+
146
+ ### 2. 创建 GitHub OAuth 应用
147
+ - 访问 [GitHub OAuth 应用设置](https://github.com/settings/applications/new)
148
+ - 创建新的 OAuth 应用
149
+ - 获取 Client ID 和 Client Secret
150
+
151
+ > **提示**:OAuth 应用的 Authorization callback URL 可以设置为您的博客域名
152
+
153
+ ### 3. 部署 OAuth 代理(重要)
154
+
155
+ 由于 Gitment 默认的 OAuth 代理 `gh-oauth.imsun.net` 已失效,登录时会报 `ERR_SSL_PROTOCOL_ERROR`。
156
+
157
+ 你需要自行部署一个 OAuth 代理服务。以下是两种部署方式的示例(部署后复制地址填入 `proxy`):
158
+
159
+ - **Netlify 云函数示例(`netlify/functions/auth.js`):**
160
+
161
+ > 完整项目参考:[github-oauth-netlify](https://github.com/huazie/github-oauth-netlify)
162
+
163
+ ```js
164
+ exports.handler = async (event) => {
165
+ const headers = {
166
+ "Access-Control-Allow-Origin": "*",
167
+ "Access-Control-Allow-Headers":
168
+ "Origin, X-Requested-With, Content-Type, Accept, Authorization",
169
+ "Content-Type": "application/json",
170
+ };
171
+
172
+ // 处理 CORS 预检请求
173
+ if (event.httpMethod === "OPTIONS") {
174
+ return { statusCode: 204, headers, body: "" };
175
+ }
176
+
177
+ // 仅接受 POST 请求
178
+ if (event.httpMethod !== "POST") {
179
+ return {
180
+ statusCode: 405,
181
+ headers,
182
+ body: JSON.stringify({ error: "Method Not Allowed" }),
183
+ };
184
+ }
185
+
186
+ try {
187
+ const body = JSON.parse(event.body);
188
+ const tokenRes = await fetch(
189
+ "https://github.com/login/oauth/access_token",
190
+ {
191
+ method: "POST",
192
+ headers: {
193
+ "Content-Type": "application/x-www-form-urlencoded",
194
+ Accept: "application/json",
195
+ "User-Agent": "github-oauth-netlify",
196
+ },
197
+ body: new URLSearchParams(body).toString(),
198
+ }
199
+ );
200
+ const data = await tokenRes.json();
201
+ if (data.error) {
202
+ return {
203
+ statusCode: 400,
204
+ headers,
205
+ body: JSON.stringify({
206
+ error: data.error,
207
+ error_description: data.error_description,
208
+ }),
209
+ };
210
+ }
211
+ return { statusCode: 200, headers, body: JSON.stringify(data) };
212
+ } catch (err) {
213
+ console.error("OAuth proxy error:", err);
214
+ return {
215
+ statusCode: 500,
216
+ headers,
217
+ body: JSON.stringify({ error: "oauth_failed" }),
218
+ };
219
+ }
220
+ };
221
+ ```
222
+
223
+ > **提示**:首次使用 Netlify 云函数,确保在项目根目录创建 `netlify.toml`:
224
+ > ```toml
225
+ > [functions]
226
+ > directory = "netlify/functions"
227
+ > ```
228
+
229
+ - **Vercel Serverless 函数示例(`api/oauth.js`):**
230
+
231
+ > ⚠️ **注意**:此示例尚未经过实际验证,建议优先使用上方的 Netlify 方案。
232
+
233
+ ```js
234
+ // api/oauth.js
235
+ module.exports = (req, res) => {
236
+ const https = require('https');
237
+ const data = JSON.stringify(req.body);
238
+ const options = {
239
+ hostname: 'github.com',
240
+ path: '/login/oauth/access_token',
241
+ method: 'POST',
242
+ headers: {
243
+ 'Content-Type': 'application/json',
244
+ 'Accept': 'application/json',
245
+ 'Content-Length': data.length
246
+ }
247
+ };
248
+ const proxyReq = https.request(options, proxyRes => {
249
+ let body = '';
250
+ proxyRes.on('data', chunk => body += chunk);
251
+ proxyRes.on('end', () => {
252
+ res.setHeader('Access-Control-Allow-Origin', '*');
253
+ res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
254
+ res.status(200).json(JSON.parse(body));
255
+ });
256
+ });
257
+ proxyReq.write(data);
258
+ proxyReq.end();
259
+ };
260
+ ```
261
+
262
+ 然后在配置中设置代理地址:
263
+
264
+ ```yaml
265
+ gitment:
266
+ # Netlify
267
+ proxy: https://your-netlify-app.netlify.app/api/auth
268
+ # Vercel
269
+ # proxy: https://your-vercel-app.vercel.app/api/oauth
270
+ ```
271
+
272
+ ### 4. 初始化评论
273
+ 页面发布后,使用您的 GitHub 账号(确保是仓库所有者)登录,点击 **Initialize** 按钮在仓库中创建对应的 Issue。之后其他访客即可正常评论。
274
+
275
+ ## 工作原理
276
+
277
+ ```mermaid
278
+ sequenceDiagram
279
+ participant V as 访问者
280
+ participant P as 页面
281
+ participant G as Gitment
282
+ participant O as GitHub OAuth
283
+ participant A as GitHub API
284
+ participant I as GitHub Issues
285
+
286
+ V->>P: 访问页面
287
+ P->>G: 加载 Gitment 脚本
288
+ G->>A: 搜索相关 Issue
289
+ A->>I: 查询 Issues
290
+ I-->>A: 返回结果
291
+ A-->>G: Issue 数据
292
+ G-->>P: 渲染评论界面
293
+
294
+ Note over V,I: 用户评论时
295
+ V->>G: 点击登录
296
+ G->>O: GitHub OAuth 认证
297
+ O-->>G: 返回访问令牌
298
+ V->>G: 提交评论
299
+ G->>A: 创建/更新 Issue Comment
300
+ A->>I: 添加评论到 Issue
301
+ ```
302
+
303
+ ### 详细流程
304
+
305
+ 1. **页面加载**:访问者打开页面,Gitment 脚本开始工作
306
+ 2. **搜索 Issue**:根据配置的 `issue_term` 在指定仓库中搜索相关 Issue
307
+ 3. **显示评论**:如果找到对应 Issue,显示其中的评论
308
+ 4. **OAuth 认证**:访问者需要通过 GitHub OAuth 登录才能评论
309
+ 5. **初始化 Issue**:仓库所有者首次访问时,需要点击 **Initialize** 按钮创建 Issue
310
+
311
+ ## 系统要求
312
+
313
+ | 依赖 | 版本要求 | 说明 |
314
+ |------|----------|------|
315
+ | **Node.js** | >= 14.0.0 | JavaScript 运行环境 |
316
+ | **Hexo** | >= 5.3.0 | 静态站点生成器 |
317
+ | **GitHub 仓库** | 公开仓库 | 存储评论数据 |
318
+
319
+ ## 相关链接
320
+
321
+ ### 官方资源
322
+ - [Gitment 官方文档](https://github.com/imsun/gitment)
323
+ - [GitHub OAuth 应用设置](https://github.com/settings/applications/new)
324
+ - [GitHub Issues 文档](https://docs.github.com/en/issues)
325
+
326
+ ### Hexo 文档
327
+ - [Hexo 官方文档](https://hexo.io/zh-cn/docs/)
328
+ - [Hexo 配置文档](https://hexo.io/zh-cn/docs/configuration)
329
+ - [Hexo 插件开发文档](https://hexo.io/zh-cn/docs/plugins)
330
+
331
+ ### 相关插件
332
+ - [hexo-generator-comments](https://github.com/huazie/diversity-plugins/tree/main/packages/hexo-generator-comments) - 多评论系统核心插件
333
+ - [hexo-comments-utterances](https://github.com/huazie/diversity-plugins/tree/main/packages/hexo-comments-utterances) - Utterances 评论插件
334
+ - [hexo-comments-gitalk](https://github.com/huazie/diversity-plugins/tree/main/packages/hexo-comments-gitalk) - Gitalk 评论插件
335
+ - [hexo-comments-giscus](https://github.com/huazie/diversity-plugins/tree/main/packages/hexo-comments-giscus) - Giscus 评论插件
336
+ - [hexo-comments-twikoo](https://github.com/huazie/diversity-plugins/tree/main/packages/hexo-comments-twikoo) - Twikoo 评论插件
337
+
338
+ ## 许可证
339
+
340
+ 本项目基于 [MIT](LICENSE) 许可证开源。
package/default.yml ADDED
@@ -0,0 +1,53 @@
1
+ # Gitment
2
+ # 一个基于 GitHub Issues 的评论系统。
3
+ # A comment system based on GitHub Issues.
4
+ # For more information: https://github.com/imsun/gitment
5
+ gitment:
6
+ # 可选值:true 【启用】 | false 【禁用】
7
+ # Available values: true | false
8
+ enable: false
9
+ # 是否启用加载提示,可选值:true | false
10
+ # Whether to enable loading indicator, Available values: true | false
11
+ loading: true
12
+ # GitHub 仓库所有者
13
+ # GitHub repo owner
14
+ owner:
15
+ # 用于存储评论的 GitHub 仓库名
16
+ # Repository name to store comments
17
+ repo:
18
+ # GitHub 应用客户端 ID
19
+ # GitHub Application Client ID
20
+ client_id:
21
+ # GitHub 应用客户端密钥
22
+ # GitHub Application Client Secret
23
+ client_secret:
24
+ # 指定GitHub issue的匹配规则,配置了这个会替换 id 属性的值
25
+ # 其中 pathname | url | title 用来匹配 issue 的标签,`other string` 是 自定义其他页面唯一标识
26
+ # Available values: pathname | url | title | `other string`
27
+ issue_term: pathname
28
+ # 页面唯一标识,用于区分不同页面的评论,默认使用 window.location.href
29
+ # Page unique identifier, used to distinguish comments on different pages, default to window.location.href
30
+ id:
31
+ # 页面标题,用作 issue 的标题,默认使用 document.title
32
+ # Page title, used as issue's title, default to document.title
33
+ title:
34
+ # 页面链接,用于 issue 的正文中,默认使用 window.location.href
35
+ # Page link, used in issue's body, default to window.location.href
36
+ link:
37
+ # 页面描述,用于 issue 的正文中
38
+ # Page description, used in issue's body
39
+ desc:
40
+ # issue 标签列表,创建 issue 时添加的标签
41
+ # An array of labels to add when creating the issue
42
+ labels: []
43
+ # 评论分页数,默认 20
44
+ # Number of comments per page, default 20
45
+ per_page: 20
46
+ # 评论最大高度限制(px),超过则折叠
47
+ # Max height of comments (px), over which comments will be folded
48
+ max_comment_height: 250
49
+ # OAuth 代理地址(可选),用于解决 OAuth 认证问题
50
+ # 由于默认代理 gh-oauth.imsun.net 已失效,需自行部署代理服务
51
+ # OAuth proxy URL (optional), used to resolve OAuth authentication issues
52
+ # The default proxy gh-oauth.imsun.net is no longer available, you need to deploy your own proxy
53
+ proxy:
package/index.js ADDED
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ const { Utils } = require('hexo-generator-comments/lib/utils');
4
+ const utils = new Utils(hexo, __dirname);
5
+
6
+ // 在 Hexo 初始化完成后输出插件信息
7
+ hexo.on('ready', () => {
8
+ if (!/^(g|s|v)/.test(hexo.env.cmd)) return;
9
+ const { name, version } = require('./package.json');
10
+ hexo.log.info(`[${hexo.config.theme}] ${name} v${version}`);
11
+ }, 10);
12
+
13
+ // 添加 Gitment 评论系统相关的Diversity主题注入过滤器
14
+ hexo.extend.filter.register('theme_injects', injects => {
15
+ // 合并优先级:站点配置 > 主题配置 > 默认配置
16
+ let config = utils.getMergedConfig('gitment', 'default.yml');
17
+ // 主题配置中的gitment配置覆盖,便于评论相关的布局文件中使用
18
+ hexo.theme.config.gitment = config;
19
+
20
+ // 没有启用 Gitment
21
+ if (!config.enable) return;
22
+
23
+ // 没有配置GitHub 仓库所有者
24
+ if (!config.owner) {
25
+ hexo.log.error('gitment.owner can\'t be null.');
26
+ return;
27
+ }
28
+
29
+ // 没有配置GitHub 仓库名
30
+ if (!config.repo) {
31
+ hexo.log.error('gitment.repo can\'t be null.');
32
+ return;
33
+ }
34
+
35
+ // 没有配置GitHub 应用客户端 ID
36
+ if (!config.client_id) {
37
+ hexo.log.error('gitment.client_id can\'t be null.');
38
+ return;
39
+ }
40
+
41
+ // 没有配置GitHub 应用客户端密钥
42
+ if (!config.client_secret) {
43
+ hexo.log.error('gitment.client_secret can\'t be null.');
44
+ return;
45
+ }
46
+
47
+ // comment 视图添加 gitment
48
+ if (injects.comment.extName === '.jsx') {
49
+ injects.comment.raw('gitment', utils.getFileContent('layout/comment/gitment' + injects.comment.extName));
50
+ } else {
51
+ injects.comment.raw('gitment', '<div class="comments gitment-wrap"></div>');
52
+ }
53
+
54
+ // pageEnd 视图添加 gitment
55
+ injects.pageEnd.raw('gitment', utils.getFileContent('layout/gitment' + injects.pageEnd.extName));
56
+
57
+ });
@@ -0,0 +1,9 @@
1
+ const { Component } = require('inferno');
2
+
3
+ module.exports = class extends Component {
4
+ render() {
5
+ return (
6
+ <div class="comments gitment-wrap"></div>
7
+ );
8
+ }
9
+ }