🎨 Improved page & comment counter, Close #74.

This commit is contained in:
elkan1788 2023-04-05 12:16:05 +08:00
parent c7e4f52499
commit fbac63d70e
9 changed files with 54 additions and 44 deletions

View File

@ -1,35 +1,31 @@
/* Page's view & comment counter plugin */
NexT.plugins.others.counter = function() {
let busz_postview = false;
let pageview_js = undefined;
let comment_js = undefined;
const busz = NexT.CONFIG.busuanzi;
if (busz != undefined && busz.pageview) {
busz_postview = true;
}
const post_meta = NexT.CONFIG.postmeta;
// Here can append others pageview & comment plugin
const waline = NexT.CONFIG.waline;
if (waline != undefined){
if(!busz_postview && waline.cfg.pageview) {
pageview_js = NexT.utils.getCDNResource(NexT.CONFIG.page.waline.js[0]);
NexT.utils.getScript(pageview_js, function(){
Waline.pageviewCount({
serverURL: waline.cfg.serverurl
const views = post_meta.views;
if(views != undefined && views.enable) {
if (views.plugin == 'waline') {
pageview_js = NexT.utils.getCDNResource(NexT.CONFIG.page.waline.js[0]);
NexT.utils.getScript(pageview_js, function(){
Waline.pageviewCount({
serverURL: NexT.CONFIG.waline.cfg.serverurl
});
});
});
}
}
if (waline.cfg.comment) {
comment_js = NexT.utils.getCDNResource(NexT.CONFIG.page.waline.js[1]);
NexT.utils.getScript(comment_js, function(){
Waline.commentCount({
serverURL: waline.cfg.serverurl
const comments = post_meta.comments;
if (comments != undefined && comments.enable) {
if (comments.plugin == 'waline') {
comment_js = NexT.utils.getCDNResource(NexT.CONFIG.page.waline.js[1]);
NexT.utils.getScript(comment_js, function(){
Waline.commentCount({
serverURL: NexT.CONFIG.waline.cfg.serverurl
});
});
});
}
}
}
}

View File

@ -478,6 +478,18 @@ params:
categories: true
wordCount: true
readTime: true
# 是否开启评论数显示
comments:
enable: true
# 评论统计插件暂只支持waline
# Comment counter plugin, only support waline
plugin: waline
# 是否开启页面访问数显示
views:
enable: true
# 页面访问统计插件支持busuanzi, waline, leancloud
# Page views counter plugin, support: busuanzi, waline, leancloud
plugin: waline
# 文章底部的设置
# Post footer settings
@ -937,8 +949,6 @@ params:
busuanzi:
visitorsIcon: fa fa-user
viewsIcon: fa fa-eye
postViews: true
# ---------------------------------------------------------------
# 内容搜索服务

View File

@ -7,6 +7,6 @@
{{ if isset .Site.Params.analytics "google" }}
{{ partial "_thirdparty/analytics/google.html" . }}
{{ end }}
{{ if isset .Site.Params.analytics "busuanzi" }}
{{ if or (isset .Site.Params.analytics "busuanzi") (eq .Site.Params.postMeta.views.plugin "busuanzi") }}
{{ partial "_thirdparty/analytics/busuanzi.html" . }}
{{ end }}

View File

@ -8,7 +8,7 @@
}}
{{/* Append waline pageview & comment plugin */}}
{{ if and .Site.Params.waline ( or .Site.Params.waline.pageView .Site.Params.waline.comment) }}
{{ if or (eq .Site.Params.postMeta.views.plugin "waline") (eq .Site.Params.postMeta.comments.plugin "waline") }}
{{ $counter := dict
"js" .Site.Data.resources.plugins.waline.js
}}

View File

@ -58,6 +58,16 @@
{{ $config = merge $config $localSearch }}
{{ end }}
{{ with .Site.Params.postMeta }}
{{ $postmeta := dict
"views" .views
"comments" .comments
}}
{{ $config = merge $config (dict "postmeta" $postmeta) }}
{{ else }}
{{ end }}
{{ if .Site.Params.algoliaSearch.enable }}
{{ $algoliaSearch := dict
"js" .Site.Data.resources.algolia
@ -127,11 +137,6 @@
{{ $config = merge $config (dict "lawidget" $lawidget) }}
{{ end }}
{{ with .Site.Params.analytics.busuanzi }}
{{ $busz := dict "pageview" .postViews }}
{{ $config = merge $config (dict "busuanzi" $busz) }}
{{ end }}
{{ $globalVars.Set "config" $config }}
{{ with .Site.Params.customFilePath }}

View File

@ -22,21 +22,19 @@
{{ end }}
{{ end }}
{{ if $.IsHome }} </h2> {{ else }} </h1> {{ end }}
{{ $isComment := and (.Scratch.Get "isComment") .Site.Params.waline.comment }}
{{ $comments := .Site.Params.postMeta.comments.enable }}
<div class="post-meta-container">
<div class="post-meta-items">
{{ partial "post/header_meta/created_date.html" . }}
{{ partial "post/header_meta/update_date.html" . }}
{{ partial "post/header_meta/categories.html" . }}
{{ if and $.IsHome (not (isset .Params "extlink")) }}
<div class="post-meta-items">
{{ if .Site.Params.waline.pageView }}
{{ if and .Site.Params.postMeta.views.enable (ne .Site.Params.postMeta.views.plugin "busuanzi") }}
{{ partial "post/header_meta/views.html" . }}
{{ end }}
{{ if $isComment }}
{{ if $comments }}
{{ partial "post/header_meta/comments.html" . }}
{{ end }}
</div>
{{ end }}
</div>
{{ if not $.IsHome }}
@ -44,7 +42,7 @@
{{ partial "post/header_meta/words.html" . }}
{{ partial "post/header_meta/readtime.html" . }}
{{ partial "post/header_meta/views.html" . }}
{{ if $isComment }}
{{ if $comments }}
{{ partial "post/header_meta/comments.html" . }}
{{ end }}
</div>

View File

@ -5,7 +5,7 @@
<span class="post-meta-item-text" title="{{ T "PostMeta.comments" }}">
{{ print (T "PostMeta.comments") (T "Symbol.colon") }}
</span>
<span {{ with .Site.Params.waline.comment }}class="waline-comment-count"{{end}} data-path="{{ .RelPermalink | relLangURL }}">
<span {{ if and .Site.Params.postMeta.comments.enable (eq .Site.Params.postMeta.comments.plugin "waline") }}class="waline-comment-count"{{end}} data-path="{{ .RelPermalink | relLangURL }}">
<i class="fa fa-sync fa-spin"></i>
</span>
</span>

View File

@ -1,8 +1,8 @@
{{ if .Site.Params.postMeta.views.enable }}
{{ $pageViewId := "" }}
{{ if .Site.Params.leancloudVisitors.enable }}
{{ if eq .Site.Params.postMeta.views.plugin "leancloud" }}
{{ $pageViewId = "leancloud-visitors-count" }}
{{ end }}
{{ if .Site.Params.analytics.busuanzi.postViews }}
{{ else if eq .Site.Params.postMeta.views.plugin "busuanzi" }}
{{ $pageViewId = "busuanzi_value_page_pv" }}
{{ end }}
<span class="post-meta-item" title="{{ T "PostMeta.views" }}">
@ -12,7 +12,8 @@
<span class="post-meta-item-text">
{{ print (T "PostMeta.views") (T "Symbol.colon") }}
</span>
<span {{ with $pageViewId }}id="{{ . }}"{{end}} {{ with .Site.Params.waline.pageView }}class="waline-pageview-count"{{end}} data-path="{{ .RelPermalink | relLangURL }}">
<span {{ with $pageViewId }}id="{{ . }}"{{end}} {{ if eq .Site.Params.postMeta.views.plugin "waline" }}class="waline-pageview-count"{{end}} data-path="{{ .RelPermalink | relLangURL }}">
<i class="fa fa-sync fa-spin"></i>
</span>
</span>
{{ end }}

View File

@ -49,7 +49,7 @@
{{ if isset .Site.Params "waline" }}
{{ $walinejs := resources.Get "js/third-party/comments/waline.js" }}
{{ $nextjs = $nextjs | append $walinejs }}
{{ if or .Site.Params.waline.pageView .Site.Params.waline.comment }}
{{ if or .Site.Params.postMeta.views.enable .Site.Params.postMeta.comments.enable }}
{{ $counterjs := resources.Get "js/third-party/others/counter.js" }}
{{ $nextjs = $nextjs | append $counterjs }}
{{ end }}