|
236 | 236 | // 调用 twikoo 评论,正则 path,刷新当前评论 |
237 | 237 | hook.doneEach(function () { |
238 | 238 | // 获取当前 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; |
242 | 248 | } |
| 249 | + var currentPath = normalizeCommentPath(vm.route.path || location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/'); |
243 | 250 |
|
244 | 251 | // 如果路径相同,不重复初始化 |
245 | 252 | if (lastPath === currentPath) { |
|
262 | 269 | // 延迟加载 twikoo,避免影响页面滑动性能 |
263 | 270 | twikooTimer = setTimeout(function() { |
264 | 271 | // 再次检查路径是否仍然匹配(用户可能已切换) |
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(/\?.*$/, '') || '/'); |
269 | 273 | if (nowPath !== currentPath) { |
270 | 274 | return; |
271 | 275 | } |
|
0 commit comments