文章页尾评论区计数模块修改

进行主题修改前必看

  • 博客魔改有风险,请务必备份你的原代码

  • 因为.pug.styl以及.yml等对缩进要求较为严格,请尽量不要使用记事本等无法提供语法高亮的文本编辑器进行修改。

  • 本文涉及修改内容会以diff代码块进行标识,复制时请不要忘记删除前面的+,-符号

  • 本帖基于Anzhiyu主题进行修改方案编写,因此请读者优先掌握Anzhiyu主题官方文档的内容后再来进行魔改。


本文实现效果的样式定义与部分源码来自于Heo博客,感谢优秀的设计令人赏心悦目。

前言

书承上文

既然处理了文章页头,就对文章页尾评论模块开刀。

修改概要

  • 增大评论标题图标与字体,仅作用于评论模块 #post-comment
  • 新增 .comment-count-number 角标,动态显示评论数;当评论数为 0 时不生成角标。
  • 适配多评论系统(Twikoo/Waline/Valine/Artalk),按各系统的计数方式读取并渲染角标(未验证)。
  • 隐藏 Twikoo 标题处的冗余计数,避免与角标重复。
  • 适配 PJAX 防止切换页面导致角标丢失

修改位置

  1. themes\anzhiyu\layout\includes\third-party\comments\index.pug:38-135
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
.comment-barrage
script(data-pjax).
(() => {
const use = !{JSON.stringify(theme.comments.use)}
const commentContainer = document.getElementById('post-comment')
const headline = commentContainer && commentContainer.querySelector('.comment-head .comment-headline')
if (!headline) return
const renderBadge = (num) => {
const count = Number(num)
const old = headline.querySelector('.comment-count-number')
if (!Number.isFinite(count) || count <= 0) {
if (old) old.remove()
return
}
const badge = old || document.createElement('span')
badge.className = 'comment-count-number'
badge.textContent = count
if (!old) headline.appendChild(badge)
}

const readTextInt = (selector) => {
const el = document.querySelector(selector)
if (!el) return 0
const t = (el.textContent || '').trim()
const m = t.match(/\d+/)
return m ? parseInt(m[0], 10) : 0
}

const updateFromSystem = (type) => {
switch(type){
case 'Twikoo': {
const domCount = readTextInt('#twikoo-count')
if (domCount > 0) { renderBadge(domCount); break }
if (typeof twikoo === 'object') {
twikoo.getCommentsCount({
envId: '!{theme.twikoo.envId}',
region: '!{theme.twikoo.region}',
urls: [window.location.pathname],
includeReply: false
}).then(res => {
renderBadge(res && res[0] ? res[0].count : 0)
}).catch(() => renderBadge(0))
} else {
renderBadge(0)
}
break
}
case 'Waline': {
const w = readTextInt('.waline-comment-count[data-path]')
renderBadge(w)
break
}
case 'Valine': {
const v = readTextInt('.valine-comment-count[data-xid]')
renderBadge(v)
break
}
case 'Artalk': {
const a = readTextInt('.artalk-count')
renderBadge(a)
break
}
default:
renderBadge(0)
}
}

const getCurrentType = () => {
if (!use || !use.length) return null
return commentContainer && commentContainer.classList.contains('move') ? use[1] : use[0]
}

const update = () => {
const t = getCurrentType()
if (t) updateFromSystem(t)
}

update()

const debouncedUpdate = (fn => {
try { return anzhiyu.debounce(fn, 200) } catch { return fn }
})(update)

const observer = new MutationObserver(() => debouncedUpdate())
try {
observer.observe(commentContainer, { childList: true, subtree: true, characterData: true })
anzhiyu.addGlobalFn('pjax', () => observer.disconnect(), 'comment-count-observer')
} catch {}

const switchBtn = document.getElementById('switch-btn')
if (switchBtn && typeof anzhiyu !== 'undefined' && anzhiyu.addEventListenerPjax) {
anzhiyu.addEventListenerPjax(switchBtn, 'click', () => setTimeout(update, 0))
} else if (switchBtn) {
switchBtn.addEventListener('click', () => setTimeout(update, 0))
}

})()
  1. themes\anzhiyu\source\css\_layout\comments.styl:23–41
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#post-comment
.comment-head
.comment-headline
display: inline-block
vertical-align: middle
font-weight: 700
font-size: 20px
position: relative
i.anzhiyufont.anzhiyu-icon-comments
font-size: 22px
line-height: 1
span.comment-count-number
font-size: 12px
background-color: var(--anzhiyu-fontcolor)
color: var(--anzhiyu-card-bg)
line-height: 1
position: absolute
top: 8px
left: 78px
padding: 3px 6px
border-radius: 20px
  1. themes\anzhiyu\source\css\_third-party\twikoo.styl:462–464
1
2
3
#twikoo .tk-comments-title .tk-comments-count,
#twikoo .tk-comments-title .tk-comment-count
display: none

修改内容说明

  • 图标与字体增大.comment-headline 字体设为 20px ,评论图标 i.anzhiyufont.anzhiyu-icon-comments 设为 22px

  • 脚本插入角标: 动态插入 span.comment-count-number ,当计数为 >0 时移除角标,不显示。

  • 多系统计数适配: 未验证,只是跟着变量写下读取计数的方法(存疑项,待验证)