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

View File

@ -478,6 +478,18 @@ params:
categories: true categories: true
wordCount: true wordCount: true
readTime: 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 # Post footer settings
@ -937,8 +949,6 @@ params:
busuanzi: busuanzi:
visitorsIcon: fa fa-user visitorsIcon: fa fa-user
viewsIcon: fa fa-eye viewsIcon: fa fa-eye
postViews: true
# --------------------------------------------------------------- # ---------------------------------------------------------------
# 内容搜索服务 # 内容搜索服务

View File

@ -7,6 +7,6 @@
{{ if isset .Site.Params.analytics "google" }} {{ if isset .Site.Params.analytics "google" }}
{{ partial "_thirdparty/analytics/google.html" . }} {{ partial "_thirdparty/analytics/google.html" . }}
{{ end }} {{ 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" . }} {{ partial "_thirdparty/analytics/busuanzi.html" . }}
{{ end }} {{ end }}

View File

@ -8,7 +8,7 @@
}} }}
{{/* Append waline pageview & comment plugin */}} {{/* 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 {{ $counter := dict
"js" .Site.Data.resources.plugins.waline.js "js" .Site.Data.resources.plugins.waline.js
}} }}

View File

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

View File

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

View File

@ -5,7 +5,7 @@
<span class="post-meta-item-text" title="{{ T "PostMeta.comments" }}"> <span class="post-meta-item-text" title="{{ T "PostMeta.comments" }}">
{{ print (T "PostMeta.comments") (T "Symbol.colon") }} {{ print (T "PostMeta.comments") (T "Symbol.colon") }}
</span> </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> <i class="fa fa-sync fa-spin"></i>
</span> </span>
</span> </span>

View File

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

View File

@ -49,7 +49,7 @@
{{ if isset .Site.Params "waline" }} {{ if isset .Site.Params "waline" }}
{{ $walinejs := resources.Get "js/third-party/comments/waline.js" }} {{ $walinejs := resources.Get "js/third-party/comments/waline.js" }}
{{ $nextjs = $nextjs | append $walinejs }} {{ $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" }} {{ $counterjs := resources.Get "js/third-party/others/counter.js" }}
{{ $nextjs = $nextjs | append $counterjs }} {{ $nextjs = $nextjs | append $counterjs }}
{{ end }} {{ end }}