Skip to content

Commit 2ad1f93

Browse files
committed
fix(twikoo): normalize README paths for shared comments
1 parent bad687e commit 2ad1f93

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- 同步要求:腾讯云 COS 需开启 CORS,并在 Expose Headers 中加入 `Last-Modified`
66
- 站点 Origin 建议:优先保留 `https://www.sicnuwiki.com`(可按需追加 `https://sicnuwiki.com`
77
- twikoo:修正 Docsify 页面 path 计算,确保不同页面评论区独立
8+
- twikoo:统一 README 相关路径(如 `/README``/./README`)以共享评论区
89

910
### 腾讯云 COS CORS 设置要点
1011
- 路径:对象存储 COS → 存储桶 → 安全管理 → 跨域访问 CORS

docs/index.html

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,17 @@
236236
// 调用 twikoo 评论,正则 path,刷新当前评论
237237
hook.doneEach(function () {
238238
// 获取当前 docsify 页面路径作为评论区分标识
239-
var currentPath = decodeURIComponent(vm.route.path || location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/');
240-
if (currentPath.charAt(0) !== '/') {
241-
currentPath = '/' + currentPath;
239+
function normalizeCommentPath(path) {
240+
var p = decodeURIComponent(path || '/').trim();
241+
if (!p || p === '#') return '/';
242+
if (p.charAt(0) !== '/') p = '/' + p;
243+
p = p.replace(/\/\.\//g, '/');
244+
p = p.replace(/\/{2,}/g, '/');
245+
// 统一 README 首页写法
246+
if (p === '/README' || p === '/README.md' || p === '/./README') return '/README';
247+
return p;
242248
}
249+
var currentPath = normalizeCommentPath(vm.route.path || location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/');
243250

244251
// 如果路径相同,不重复初始化
245252
if (lastPath === currentPath) {
@@ -262,10 +269,7 @@
262269
// 延迟加载 twikoo,避免影响页面滑动性能
263270
twikooTimer = setTimeout(function() {
264271
// 再次检查路径是否仍然匹配(用户可能已切换)
265-
var nowPath = decodeURIComponent(vm.route.path || location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/');
266-
if (nowPath.charAt(0) !== '/') {
267-
nowPath = '/' + nowPath;
268-
}
272+
var nowPath = normalizeCommentPath(vm.route.path || location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/');
269273
if (nowPath !== currentPath) {
270274
return;
271275
}

0 commit comments

Comments
 (0)