xfm-plugin-comments 0.0.0-stage → 0.4.16
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/README.md +114 -2
- package/index.js +394 -0
- package/package.json +48 -4
package/README.md
CHANGED
|
@@ -1,3 +1,115 @@
|
|
|
1
|
-
#
|
|
1
|
+
# xfm-plugin-comments
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Comment systems for xfmpress — one plugin, ten providers, one config key.
|
|
4
|
+
|
|
5
|
+
Part of [xfm](https://github.com/xfm0797/xfm) — a zero-dependency static site generator for Node.js.
|
|
6
|
+
**Plugins are not bundled with the core**: install only the ones you need.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm i -D xfm-plugin-comments
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
That is all — the generator loads every `xfm-plugin-*` dependency found in your site's
|
|
15
|
+
`package.json` automatically (same as Hexo). Restart `xfm dev` if it is already running.
|
|
16
|
+
|
|
17
|
+
Explicit configuration also works:
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
// xfm.config.mjs
|
|
21
|
+
export default {
|
|
22
|
+
plugins: ['comments'],
|
|
23
|
+
};
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
> Replaces `xfm-plugin-giscus` (0.4.2). The config key is still `comments`, and
|
|
27
|
+
> `provider: 'giscus'` behaves exactly as before — just swap the package.
|
|
28
|
+
|
|
29
|
+
## Pick a provider
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
export default {
|
|
33
|
+
comments: { provider: 'giscus', repo: 'owner/repo', repoId: 'R_kgDO…' },
|
|
34
|
+
};
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
| `provider` | Backend needed | Sign-in | Notes |
|
|
38
|
+
| --- | --- | --- | --- |
|
|
39
|
+
| `giscus` | none (GitHub Discussions) | GitHub | Default. Threads follow the theme toggle. |
|
|
40
|
+
| `beaudar` | none (GitHub Discussions) | GitHub | giscus-compatible fork, reachable from mainland China. |
|
|
41
|
+
| `utterances` | none (GitHub Issues) | GitHub | Lightest GitHub option, no reactions. |
|
|
42
|
+
| `disqus` | none (hosted) | many | Long-standing, ad-supported tier. |
|
|
43
|
+
| `waline` | self-host (Vercel/…+ DB) | email or socials | Markdown, emoji, page views. |
|
|
44
|
+
| `twikoo` | Tencent CloudBase / Vercel | email or socials | Popular in China, one-click deploy. |
|
|
45
|
+
| `valine` | LeanCloud | email or socials | Lightweight, older but stable. |
|
|
46
|
+
| `gitalk` | GitHub Issues + OAuth app | GitHub | Issues stored in your repo. |
|
|
47
|
+
| `artalk` | self-host (Docker/binary) | email or socials | Built-in admin dashboard. |
|
|
48
|
+
| `cusdis` | self-host or cusdis.com | email | Minimal, tiny script. |
|
|
49
|
+
|
|
50
|
+
Unknown providers and missing required fields fail loudly in the build log and leave the
|
|
51
|
+
page untouched — a typo in `repoId` never ships a broken widget silently.
|
|
52
|
+
|
|
53
|
+
## Configuration
|
|
54
|
+
|
|
55
|
+
Common options:
|
|
56
|
+
|
|
57
|
+
| Option | Default | Meaning |
|
|
58
|
+
| --- | --- | --- |
|
|
59
|
+
| `provider` | `giscus` | See the table above. Aliases: `github` → `giscus`. |
|
|
60
|
+
| `title` | `评论` | Section heading; `''` hides it. |
|
|
61
|
+
| `identifier` | `path` | What binds a thread to a page: `path` (site-relative URL), `url` (absolute), `title`. |
|
|
62
|
+
| `dev` | `false` | Also render in `xfm dev` (third-party scripts stay out of local previews by default). |
|
|
63
|
+
|
|
64
|
+
Provider-specific options:
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
// giscus / beaudar
|
|
68
|
+
{ provider: 'giscus', repo, repoId, category: 'General', categoryId, mapping: 'pathname',
|
|
69
|
+
theme: 'preferred_color_scheme', lang: 'zh-CN', strict: false, reactionsEnabled: true,
|
|
70
|
+
inputPosition: 'bottom' }
|
|
71
|
+
|
|
72
|
+
// utterances
|
|
73
|
+
{ provider: 'utterances', repo, issueTerm: 'pathname', label: 'comment', theme: 'preferred-color-scheme' }
|
|
74
|
+
|
|
75
|
+
// disqus
|
|
76
|
+
{ provider: 'disqus', shortname: 'my-blog' }
|
|
77
|
+
|
|
78
|
+
// waline
|
|
79
|
+
{ provider: 'waline', serverURL: 'https://comments.example.com', lang: 'zh-CN',
|
|
80
|
+
emoji: ['https://…/emoji/'], requiredMeta: ['nick', 'mail'], login: 'enable', commentSorting: 'newest' }
|
|
81
|
+
|
|
82
|
+
// twikoo
|
|
83
|
+
{ provider: 'twikoo', envId: 'https://xxx.vercel.app', region: 'ap-shanghai', lang: 'zh-CN' }
|
|
84
|
+
|
|
85
|
+
// valine
|
|
86
|
+
{ provider: 'valine', appId, appKey, placeholder: '欢迎留言', serverURLs, meta: ['nick', 'mail', 'link'] }
|
|
87
|
+
|
|
88
|
+
// gitalk
|
|
89
|
+
{ provider: 'gitalk', clientID, clientSecret, repo, owner, admin: ['owner'], distractionFreeMode: true, proxy }
|
|
90
|
+
|
|
91
|
+
// artalk
|
|
92
|
+
{ provider: 'artalk', server: 'https://artalk.example.com', site: 'My Blog', dark: true }
|
|
93
|
+
|
|
94
|
+
// cusdis
|
|
95
|
+
{ provider: 'cusdis', host: 'https://cusdis.com', appId }
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Behaviour
|
|
99
|
+
|
|
100
|
+
- Inserts a `.comments` section just before `</article>` (falling back to `</main>` / `</body>`),
|
|
101
|
+
with the heading, the mount point and the provider script.
|
|
102
|
+
- **Post pages only** — a page needs a `date` and must not be `type: page` / `type: index`.
|
|
103
|
+
Home, archives, tag pages and standalone pages never get a widget.
|
|
104
|
+
- `comments: false` in front-matter disables it for one page.
|
|
105
|
+
- Put `<div data-xfm-comments></div>` in your template to choose the position yourself; the
|
|
106
|
+
plugin then injects only the scripts.
|
|
107
|
+
- `site.comments` (`{ provider, title }`) is available in templates.
|
|
108
|
+
- Stylesheets the provider needs (Waline, Gitalk, Artalk) are added to `<head>` once, on
|
|
109
|
+
pages that actually mount a widget.
|
|
110
|
+
- giscus and beaudar follow the site's `data-theme` toggle via `postMessage`; Waline switches
|
|
111
|
+
by CSS selector, Cusdis uses `auto`.
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT © xfm0797
|
package/index.js
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 评论插件(xfm-plugin-comments)
|
|
4
|
+
*
|
|
5
|
+
* 安装:npm i -D xfm-plugin-comments —— 装了就生效(也可在 config.plugins 写 'comments')
|
|
6
|
+
* -------------------------------------------------------------------
|
|
7
|
+
* 一个插件接十家评论系统,切 provider 就行,模板与样式不用动:
|
|
8
|
+
*
|
|
9
|
+
* giscus GitHub Discussions(推荐:无后端、无广告、跟随主题)
|
|
10
|
+
* beaudar giscus 的国内可访问版本
|
|
11
|
+
* utterances GitHub Issues,更轻量
|
|
12
|
+
* disqus 老牌通用评论
|
|
13
|
+
* waline 可自托管,支持 Markdown / 表情 / 浏览量
|
|
14
|
+
* twikoo 腾讯云开发或 Vercel 部署,国内常用
|
|
15
|
+
* valine LeanCloud 存储,轻量
|
|
16
|
+
* gitalk GitHub Issues + OAuth
|
|
17
|
+
* artalk 可自托管,带后台管理
|
|
18
|
+
* cusdis 极简自托管
|
|
19
|
+
*
|
|
20
|
+
* 只挂在文章页(有 date、且不是 type: page),首页 / 归档 / 404 不插入。
|
|
21
|
+
* 默认只在生产构建生效(第三方脚本不该拖慢本地预览),comments.dev = true 可打开。
|
|
22
|
+
*
|
|
23
|
+
* 通用配置:
|
|
24
|
+
* comments: {
|
|
25
|
+
* provider: 'giscus',
|
|
26
|
+
* title: '评论', // 区块标题,'' 则不显示
|
|
27
|
+
* identifier: 'path', // 评论与哪一页绑定:path(默认)| url | title
|
|
28
|
+
* dev: false, // dev 下是否也渲染
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* 各 provider 的专属字段(都写在 comments 下):
|
|
32
|
+
* giscus / beaudar: repo, repoId, category, categoryId, mapping, theme, lang,
|
|
33
|
+
* strict, reactionsEnabled, inputPosition
|
|
34
|
+
* utterances: repo, issueTerm, label, theme
|
|
35
|
+
* disqus: shortname, theme
|
|
36
|
+
* waline: serverURL, lang, emoji, requiredMeta, login, commentSorting, dark
|
|
37
|
+
* twikoo: envId, region, lang
|
|
38
|
+
* valine: appId, appKey, placeholder, serverURLs
|
|
39
|
+
* gitalk: clientID, clientSecret, repo, owner, admin, distractionFreeMode, proxy
|
|
40
|
+
* artalk: server, site, dark, pageTitle
|
|
41
|
+
* cusdis: host, appId
|
|
42
|
+
*
|
|
43
|
+
* 页面级:front-matter 写 comments: false 关闭单页评论;
|
|
44
|
+
* 模板里放 `<div data-xfm-comments></div>` 可自定位置,插件只补脚本不重复建容器。
|
|
45
|
+
*/
|
|
46
|
+
const esc = (s) =>
|
|
47
|
+
String(s == null ? '' : s)
|
|
48
|
+
.replace(/&/g, '&')
|
|
49
|
+
.replace(/</g, '<')
|
|
50
|
+
.replace(/>/g, '>')
|
|
51
|
+
.replace(/"/g, '"');
|
|
52
|
+
|
|
53
|
+
/** JSON → 安全的脚本字面量(内容里的 </script> 不会提前闭合) */
|
|
54
|
+
const json = (v) => JSON.stringify(v).replace(/</g, '\\u003c');
|
|
55
|
+
|
|
56
|
+
/** 对象 → HTML 属性串;true 渲染成裸属性,false / 空值跳过 */
|
|
57
|
+
function attrs(map) {
|
|
58
|
+
return Object.entries(map)
|
|
59
|
+
.filter(([, v]) => v !== undefined && v !== null && v !== false && v !== '')
|
|
60
|
+
.map(([k, v]) => (v === true ? ` ${k}` : ` ${k}="${esc(v)}"`))
|
|
61
|
+
.join('');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** giscus / beaudar:iframe 是异步插进来的,主题切换用 postMessage 通知 */
|
|
65
|
+
const themeSync = (origin) => `<script>
|
|
66
|
+
(function () {
|
|
67
|
+
var root = document.documentElement;
|
|
68
|
+
function sync() {
|
|
69
|
+
var f = document.querySelector('iframe.giscus-frame');
|
|
70
|
+
if (!f) return;
|
|
71
|
+
var theme = root.dataset.theme === 'dark' ? 'dark' : 'light';
|
|
72
|
+
f.contentWindow.postMessage({ giscus: { setConfig: { theme: theme } } }, '${origin}');
|
|
73
|
+
}
|
|
74
|
+
new MutationObserver(sync).observe(document.body, { childList: true, subtree: true });
|
|
75
|
+
new MutationObserver(sync).observe(root, { attributes: true, attributeFilter: ['data-theme'] });
|
|
76
|
+
})();
|
|
77
|
+
</script>`;
|
|
78
|
+
|
|
79
|
+
/** giscus 系的主题取值,写错就回落成跟随系统 */
|
|
80
|
+
const GISCUS_THEMES = /^(?:light|dark|dark_dimmed|dark_high_contrast|preferred_color_scheme|transparent_dark|custom)$/;
|
|
81
|
+
|
|
82
|
+
/** 每个 provider:必需字段、容器与脚本 */
|
|
83
|
+
const PROVIDERS = {
|
|
84
|
+
giscus: {
|
|
85
|
+
needs: ['repo', 'repoId'],
|
|
86
|
+
build(c) {
|
|
87
|
+
return {
|
|
88
|
+
container: '<div class="giscus" data-xfm-comments></div>',
|
|
89
|
+
scripts:
|
|
90
|
+
'<script src="https://giscus.app/client.js"' +
|
|
91
|
+
attrs({
|
|
92
|
+
'data-repo': c.repo,
|
|
93
|
+
'data-repo-id': c.repoId,
|
|
94
|
+
'data-category': c.category || 'General',
|
|
95
|
+
'data-category-id': c.categoryId || '',
|
|
96
|
+
'data-mapping': c.mapping || 'pathname',
|
|
97
|
+
'data-strict': c.strict ? '1' : '0',
|
|
98
|
+
'data-reactions-enabled': c.reactionsEnabled === false ? '0' : '1',
|
|
99
|
+
'data-emit-metadata': '0',
|
|
100
|
+
'data-input-position': c.inputPosition || 'bottom',
|
|
101
|
+
'data-theme': GISCUS_THEMES.test(c.theme || '') ? c.theme : 'preferred_color_scheme',
|
|
102
|
+
'data-lang': c.lang || 'zh-CN',
|
|
103
|
+
crossorigin: 'anonymous',
|
|
104
|
+
async: true,
|
|
105
|
+
}) +
|
|
106
|
+
'></script>' +
|
|
107
|
+
themeSync('https://giscus.app'),
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
beaudar: {
|
|
113
|
+
needs: ['repo', 'repoId'],
|
|
114
|
+
build(c) {
|
|
115
|
+
return {
|
|
116
|
+
container: '<div class="giscus" data-xfm-comments></div>',
|
|
117
|
+
scripts:
|
|
118
|
+
'<script src="https://beaudar.lipk.org/client.js"' +
|
|
119
|
+
attrs({
|
|
120
|
+
'data-repo': c.repo,
|
|
121
|
+
'data-repo-id': c.repoId,
|
|
122
|
+
'data-category': c.category || 'General',
|
|
123
|
+
'data-category-id': c.categoryId || '',
|
|
124
|
+
'data-mapping': c.mapping || 'pathname',
|
|
125
|
+
'data-theme': c.theme || 'preferred_color_scheme',
|
|
126
|
+
'data-lang': c.lang || 'zh-CN',
|
|
127
|
+
crossorigin: 'anonymous',
|
|
128
|
+
async: true,
|
|
129
|
+
}) +
|
|
130
|
+
'></script>' +
|
|
131
|
+
themeSync('https://beaudar.lipk.org'),
|
|
132
|
+
};
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
utterances: {
|
|
137
|
+
needs: ['repo'],
|
|
138
|
+
build(c) {
|
|
139
|
+
return {
|
|
140
|
+
container: '<div class="utterances" data-xfm-comments></div>',
|
|
141
|
+
scripts:
|
|
142
|
+
'<script src="https://utteranc.es/client.js"' +
|
|
143
|
+
attrs({
|
|
144
|
+
repo: c.repo,
|
|
145
|
+
'issue-term': c.issueTerm || c.mapping || 'pathname',
|
|
146
|
+
label: c.label || 'comment',
|
|
147
|
+
theme: c.theme || 'preferred-color-scheme',
|
|
148
|
+
crossorigin: 'anonymous',
|
|
149
|
+
async: true,
|
|
150
|
+
}) +
|
|
151
|
+
'></script>',
|
|
152
|
+
};
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
|
|
156
|
+
disqus: {
|
|
157
|
+
needs: ['shortname'],
|
|
158
|
+
build(c, ctx) {
|
|
159
|
+
const config = `{ this.page.url = ${json(ctx.url)}; this.page.identifier = ${json(ctx.key)}; }`;
|
|
160
|
+
return {
|
|
161
|
+
container: '<div id="disqus_thread" data-xfm-comments></div>',
|
|
162
|
+
scripts: `<script>
|
|
163
|
+
(function () {
|
|
164
|
+
var d = document;
|
|
165
|
+
window.disqus_config = function () ${config};
|
|
166
|
+
var s = d.createElement('script');
|
|
167
|
+
s.src = 'https://' + ${json(c.shortname)} + '.disqus.com/embed.js';
|
|
168
|
+
s.setAttribute('data-timestamp', +new Date());
|
|
169
|
+
(d.head || d.body).appendChild(s);
|
|
170
|
+
})();
|
|
171
|
+
</script>`,
|
|
172
|
+
};
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
|
|
176
|
+
waline: {
|
|
177
|
+
needs: ['serverURL'],
|
|
178
|
+
build(c, ctx) {
|
|
179
|
+
const opt = {
|
|
180
|
+
el: '#waline',
|
|
181
|
+
serverURL: c.serverURL,
|
|
182
|
+
path: ctx.key,
|
|
183
|
+
lang: c.lang || 'zh-CN',
|
|
184
|
+
dark: c.dark === false ? false : 'html[data-theme="dark"]',
|
|
185
|
+
};
|
|
186
|
+
for (const k of ['emoji', 'requiredMeta', 'login', 'commentSorting']) {
|
|
187
|
+
if (c[k]) opt[k] = c[k];
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
css: '<link rel="stylesheet" href="https://unpkg.com/@waline/client@v3/dist/waline.css">',
|
|
191
|
+
container: '<div id="waline" data-xfm-comments></div>',
|
|
192
|
+
scripts:
|
|
193
|
+
'<script type="module">' +
|
|
194
|
+
"import { init } from 'https://unpkg.com/@waline/client@v3/dist/waline.js';" +
|
|
195
|
+
`init(${json(opt)});` +
|
|
196
|
+
'</script>',
|
|
197
|
+
};
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
twikoo: {
|
|
202
|
+
needs: ['envId'],
|
|
203
|
+
build(c, ctx) {
|
|
204
|
+
const opt = { envId: c.envId, el: '#tcomment', path: ctx.key };
|
|
205
|
+
if (c.lang) opt.lang = c.lang;
|
|
206
|
+
if (c.region) opt.region = c.region;
|
|
207
|
+
return {
|
|
208
|
+
container: '<div id="tcomment" data-xfm-comments></div>',
|
|
209
|
+
scripts:
|
|
210
|
+
'<script src="https://cdn.jsdelivr.net/npm/twikoo@2/dist/twikoo.min.js"></script>' +
|
|
211
|
+
`<script>twikoo.init(${json(opt)});</script>`,
|
|
212
|
+
};
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
|
|
216
|
+
valine: {
|
|
217
|
+
needs: ['appId', 'appKey'],
|
|
218
|
+
build(c, ctx) {
|
|
219
|
+
const opt = {
|
|
220
|
+
el: '#vcomments',
|
|
221
|
+
appId: c.appId,
|
|
222
|
+
appKey: c.appKey,
|
|
223
|
+
path: ctx.key,
|
|
224
|
+
placeholder: c.placeholder || '欢迎留言',
|
|
225
|
+
};
|
|
226
|
+
for (const k of ['serverURLs', 'lang', 'meta']) {
|
|
227
|
+
if (c[k]) opt[k] = c[k];
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
container: '<div id="vcomments" data-xfm-comments></div>',
|
|
231
|
+
scripts:
|
|
232
|
+
'<script src="https://unpkg.com/valine@1/dist/Valine.min.js"></script>' +
|
|
233
|
+
`<script>new Valine(${json(opt)});</script>`,
|
|
234
|
+
};
|
|
235
|
+
},
|
|
236
|
+
},
|
|
237
|
+
|
|
238
|
+
gitalk: {
|
|
239
|
+
needs: ['clientID', 'clientSecret', 'repo', 'owner', 'admin'],
|
|
240
|
+
build(c, ctx) {
|
|
241
|
+
const opt = {
|
|
242
|
+
clientID: c.clientID,
|
|
243
|
+
clientSecret: c.clientSecret,
|
|
244
|
+
repo: c.repo,
|
|
245
|
+
owner: c.owner,
|
|
246
|
+
admin: Array.isArray(c.admin) ? c.admin : [c.admin],
|
|
247
|
+
id: ctx.key,
|
|
248
|
+
distractionFreeMode: c.distractionFreeMode !== false,
|
|
249
|
+
};
|
|
250
|
+
if (c.proxy) opt.proxy = c.proxy;
|
|
251
|
+
return {
|
|
252
|
+
css: '<link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">',
|
|
253
|
+
container: '<div id="gitalk-container" data-xfm-comments></div>',
|
|
254
|
+
scripts:
|
|
255
|
+
'<script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>' +
|
|
256
|
+
`<script>new Gitalk(${json(opt)}).render('gitalk-container');</script>`,
|
|
257
|
+
};
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
artalk: {
|
|
262
|
+
needs: ['server'],
|
|
263
|
+
build(c, ctx) {
|
|
264
|
+
const opt = {
|
|
265
|
+
el: '#Comments',
|
|
266
|
+
server: c.server,
|
|
267
|
+
pageKey: ctx.key,
|
|
268
|
+
pageTitle: c.pageTitle === false ? '' : ctx.title,
|
|
269
|
+
};
|
|
270
|
+
if (c.site) opt.site = c.site;
|
|
271
|
+
if (c.dark !== false) opt.darkMode = 'auto';
|
|
272
|
+
return {
|
|
273
|
+
css: '<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/artalk@2/dist/Artalk.css">',
|
|
274
|
+
container: '<div id="Comments" data-xfm-comments></div>',
|
|
275
|
+
scripts:
|
|
276
|
+
'<script src="https://cdn.jsdelivr.net/npm/artalk@2/dist/Artalk.js"></script>' +
|
|
277
|
+
`<script>Artalk.init(${json(opt)});</script>`,
|
|
278
|
+
};
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
|
|
282
|
+
cusdis: {
|
|
283
|
+
needs: ['appId'],
|
|
284
|
+
build(c, ctx) {
|
|
285
|
+
const host = String(c.host || 'https://cusdis.com').replace(/\/+$/, '');
|
|
286
|
+
return {
|
|
287
|
+
container:
|
|
288
|
+
'<div id="cusdis_thread"' +
|
|
289
|
+
attrs({
|
|
290
|
+
'data-xfm-comments': true,
|
|
291
|
+
'data-host': host,
|
|
292
|
+
'data-app-id': c.appId,
|
|
293
|
+
'data-page-id': ctx.key,
|
|
294
|
+
'data-page-url': ctx.url,
|
|
295
|
+
'data-page-title': ctx.title,
|
|
296
|
+
'data-theme': c.dark === false ? 'light' : 'auto',
|
|
297
|
+
}) +
|
|
298
|
+
'></div>',
|
|
299
|
+
scripts: `<script async defer src="${esc(host)}/js/cusdis.es.js"></script>`,
|
|
300
|
+
};
|
|
301
|
+
},
|
|
302
|
+
},
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
/** 常见写法归一:github 其实就是 giscus,comments 是旧名字 */
|
|
306
|
+
const ALIASES = { github: 'giscus', comments: 'giscus' };
|
|
307
|
+
|
|
308
|
+
module.exports = (site) => {
|
|
309
|
+
const c = site.config.comments || {};
|
|
310
|
+
const raw = String(c.provider || 'giscus').toLowerCase();
|
|
311
|
+
const provider = ALIASES[raw] || raw;
|
|
312
|
+
const def = PROVIDERS[provider];
|
|
313
|
+
const inDev = c.dev === true;
|
|
314
|
+
const title = c.title === undefined ? '评论' : String(c.title);
|
|
315
|
+
const base = (site.config.site && site.config.site.base) || '/';
|
|
316
|
+
|
|
317
|
+
const missing = def ? def.needs.filter((k) => !c[k] || (Array.isArray(c[k]) && !c[k].length)) : [];
|
|
318
|
+
|
|
319
|
+
let warned = false;
|
|
320
|
+
const warn = (msg) => {
|
|
321
|
+
if (warned || site.mode !== 'production') return;
|
|
322
|
+
warned = true;
|
|
323
|
+
console.warn(msg);
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
return {
|
|
327
|
+
name: 'comments',
|
|
328
|
+
|
|
329
|
+
/** 模板可用:{% if site.comments %}<div data-xfm-comments></div>{% endif %} */
|
|
330
|
+
extendContext(ctx) {
|
|
331
|
+
if (!def || missing.length) return ctx;
|
|
332
|
+
return { ...ctx, comments: { provider, title } };
|
|
333
|
+
},
|
|
334
|
+
|
|
335
|
+
transformHtml(html, ctx) {
|
|
336
|
+
if (!def) {
|
|
337
|
+
warn(`[xfm-plugin-comments] 不支持的 provider "${raw}",可选:${Object.keys(PROVIDERS).join(' / ')}`);
|
|
338
|
+
return html;
|
|
339
|
+
}
|
|
340
|
+
if (missing.length) {
|
|
341
|
+
warn(`[xfm-plugin-comments] provider ${provider} 缺少配置:${missing.join(', ')},评论区不会显示`);
|
|
342
|
+
return html;
|
|
343
|
+
}
|
|
344
|
+
if (!inDev && site.mode !== 'production') return html;
|
|
345
|
+
|
|
346
|
+
const page = (ctx && ctx.page) || null;
|
|
347
|
+
if (!page) return html;
|
|
348
|
+
const data = page.data || {};
|
|
349
|
+
if (data.comments === false) return html;
|
|
350
|
+
// 只挂文章:聚合页、首页、分类页、独立页面一律跳过
|
|
351
|
+
if (data.type === 'page' || data.type === 'index' || !page.date || page.url === '/') return html;
|
|
352
|
+
|
|
353
|
+
// 评论与页面的绑定键:默认用站点内路径(换域名不影响),也可用完整 URL 或标题
|
|
354
|
+
const path = site.helpers.withBase(base, page.url || '/');
|
|
355
|
+
const key =
|
|
356
|
+
c.identifier === 'url'
|
|
357
|
+
? site.helpers.absoluteUrl(site.config.site, page.url)
|
|
358
|
+
: c.identifier === 'title'
|
|
359
|
+
? page.title || path
|
|
360
|
+
: path;
|
|
361
|
+
const url = site.helpers.absoluteUrl(site.config.site, page.url);
|
|
362
|
+
|
|
363
|
+
const snippet = def.build(c, { key, url, title: page.title || '' });
|
|
364
|
+
const section =
|
|
365
|
+
`<section class="comments" data-comments-provider="${provider}" style="margin-top:3rem;padding-top:2rem;border-top:1px solid var(--border,#e6e6e6)">` +
|
|
366
|
+
(title ? `<h2 style="font-size:1.15rem;margin:0 0 1rem">${esc(title)}</h2>` : '') +
|
|
367
|
+
snippet.container +
|
|
368
|
+
snippet.scripts +
|
|
369
|
+
'</section>';
|
|
370
|
+
|
|
371
|
+
// 模板自己留了挂载点:只补脚本,不重复建容器
|
|
372
|
+
const body = html.includes('data-xfm-comments') ? snippet.scripts : section;
|
|
373
|
+
|
|
374
|
+
const out = insertBefore(html, body);
|
|
375
|
+
return snippet.css ? insertHead(out, snippet.css) : out;
|
|
376
|
+
},
|
|
377
|
+
};
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
/** 优先贴着文章结尾,其次主内容区,最后退回 body 末尾 */
|
|
381
|
+
function insertBefore(html, body) {
|
|
382
|
+
for (const tag of ['</article>', '</main>', '</body>']) {
|
|
383
|
+
const at = html.lastIndexOf(tag);
|
|
384
|
+
if (at !== -1) return html.slice(0, at) + body + '\n' + html.slice(at);
|
|
385
|
+
}
|
|
386
|
+
return html + body;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** 样式表插到 head 末尾(没有 head 就原样返回) */
|
|
390
|
+
function insertHead(html, link) {
|
|
391
|
+
return /<\/head>/i.test(html) ? html.replace(/<\/head>/i, `${link}\n</head>`) : html;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
module.exports.PROVIDERS = Object.keys(PROVIDERS);
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xfm-plugin-comments",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.4.16",
|
|
4
|
+
"description": "Comment systems for xfmpress — giscus, beaudar, utterances, Disqus, Waline, Twikoo, Valine, Gitalk, Artalk, Cusdis",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=18.0.0"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"xfm",
|
|
16
|
+
"xfmpress",
|
|
17
|
+
"xfmpress-plugin",
|
|
18
|
+
"static-site-generator",
|
|
19
|
+
"plugin",
|
|
20
|
+
"comments",
|
|
21
|
+
"giscus",
|
|
22
|
+
"utterances",
|
|
23
|
+
"disqus",
|
|
24
|
+
"waline",
|
|
25
|
+
"twikoo",
|
|
26
|
+
"valine",
|
|
27
|
+
"gitalk",
|
|
28
|
+
"artalk",
|
|
29
|
+
"cusdis"
|
|
30
|
+
],
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/xfm0797/xfm.git",
|
|
34
|
+
"directory": "packages/comments"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/xfm0797/xfm/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/xfm0797/xfm/tree/main/packages/comments#readme",
|
|
40
|
+
"author": "xfm0797",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"xfmpress": ">=0.4.2"
|
|
44
|
+
},
|
|
45
|
+
"peerDependenciesMeta": {
|
|
46
|
+
"xfmpress": {
|
|
47
|
+
"optional": true
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|