From e1c088c355bac8d6a92a6a06d2b59136713b24f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=A1=E6=A2=A6=E6=98=9F=E5=B0=98?= Date: Sun, 30 Oct 2022 11:43:18 +0800 Subject: [PATCH] :lipstick: Add comment counter in home & page. --- assets/js/third-party/comments/waline.js | 2 ++ assets/js/third-party/others/counter.js | 35 +++++++++++++++++++ assets/js/third-party/others/pageview.js | 21 ----------- assets/js/utils.js | 16 ++++----- data/resources.yaml | 16 ++++++--- i18n/en.yaml | 2 ++ i18n/zh-cn.yaml | 2 ++ layouts/partials/head/config.html | 8 ++--- layouts/partials/post/header.html | 18 +++++++--- .../partials/post/header_meta/comments.html | 11 ++++++ layouts/partials/scripts/global.html | 6 ++-- 11 files changed, 91 insertions(+), 46 deletions(-) create mode 100644 assets/js/third-party/others/counter.js delete mode 100644 assets/js/third-party/others/pageview.js create mode 100644 layouts/partials/post/header_meta/comments.html diff --git a/assets/js/third-party/comments/waline.js b/assets/js/third-party/comments/waline.js index d5fc383..8c3b191 100644 --- a/assets/js/third-party/comments/waline.js +++ b/assets/js/third-party/comments/waline.js @@ -5,6 +5,7 @@ NexT.plugins.comments.waline = function() { || !NexT.utils.checkDOMExist(element)) return; const { + comment, emoji, imguploader, pageview, @@ -39,6 +40,7 @@ NexT.plugins.comments.waline = function() { locale, el : element, pageview : pageview, + comment : comment, emoji : emoji, imageUploader : imguploader, wordLimit : wordlimit, diff --git a/assets/js/third-party/others/counter.js b/assets/js/third-party/others/counter.js new file mode 100644 index 0000000..6027bee --- /dev/null +++ b/assets/js/third-party/others/counter.js @@ -0,0 +1,35 @@ +/* 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; + } + + // 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 + }); + }); + } + + 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 + }); + }); + } + } + + +} \ No newline at end of file diff --git a/assets/js/third-party/others/pageview.js b/assets/js/third-party/others/pageview.js deleted file mode 100644 index c0989d0..0000000 --- a/assets/js/third-party/others/pageview.js +++ /dev/null @@ -1,21 +0,0 @@ -/* Waline pageview plugin */ -NexT.plugins.others.pageview = function() { - const busz = NexT.CONFIG.busuanzi; - if (busz != undefined && busz.pageview) return; - - let pageview_js = undefined; - - // Here can append others pageview plugin - const waline = NexT.CONFIG.waline; - if (waline != undefined && waline.cfg.pageview) { - pageview_js = NexT.utils.getCDNResource(NexT.CONFIG.page.pageview.js); - } - - if (pageview_js == undefined) return; - - NexT.utils.getScript(pageview_js, function(){ - Waline.pageviewCount({ - serverURL: waline.cfg.serverurl - }); - }); -} \ No newline at end of file diff --git a/assets/js/utils.js b/assets/js/utils.js index fec3f32..f74b509 100644 --- a/assets/js/utils.js +++ b/assets/js/utils.js @@ -8,15 +8,15 @@ HTMLElement.prototype.wrap = function (wrapper) { NexT.utils = { registerImageLoadEvent: function() { - var images = document.querySelectorAll('.sidebar img, .post-block img, .vendors-list img'); + const images = document.querySelectorAll('.sidebar img, .post-block img, .vendors-list img'); - var callback = (entries) => { + const callback = (entries) => { entries.forEach(item => { if (item.intersectionRatio > 0) { - var ele = item.target; - var imgSrc = ele.getAttribute('data-src'); + let ele = item.target; + let imgSrc = ele.getAttribute('data-src'); if (imgSrc) { - var img = new Image(); + let img = new Image(); img.addEventListener('load', function() { ele.src = imgSrc; }, false); @@ -28,7 +28,7 @@ NexT.utils = { }) }; - var observer = new IntersectionObserver(callback); + const observer = new IntersectionObserver(callback); images.forEach(img => { observer.observe(img); }); @@ -125,7 +125,7 @@ NexT.utils = { if (laWidget.length > 0) { const valIds = [0, 2, 4, 6]; const domIds = ['today_site_pv', 'yesterday_site_pv', 'month_site_pv', 'total_site_pv'] - for (var i in valIds) { + for (let i in valIds) { let pv = NexT.utils.numberFormat(laWidget[valIds[i]].innerText); document.getElementById(domIds[i]).innerText = pv; } @@ -455,7 +455,7 @@ NexT.utils = { const comms = document.querySelectorAll('.comment-wrap > div'); if (comms.length <= 1) return; comms.forEach(function (item) { - var dis = window.getComputedStyle(item, null).display; + let dis = window.getComputedStyle(item, null).display; item.style.display = dis; }); }, diff --git a/data/resources.yaml b/data/resources.yaml index 819ce71..a3dfa7e 100644 --- a/data/resources.yaml +++ b/data/resources.yaml @@ -115,12 +115,18 @@ plugins: name: mermaid version: 9.1.7 file: dist/mermaid.min.js - # 文章访问统计 + # 文章访问/评论统计 waline: js: - name: '@waline/client' - version: 2.13.0 - file: dist/pageview.js - alias: waline + - name: pageview + alias_name: '@waline/client' + version: 2.13.0 + file: dist/pageview.js + alias: waline + - name: comment + alias_name: '@waline/client' + version: 2.13.0 + file: dist/comment.js + alias: waline diff --git a/i18n/en.yaml b/i18n/en.yaml index fc9971c..d114344 100644 --- a/i18n/en.yaml +++ b/i18n/en.yaml @@ -93,6 +93,8 @@ PostReadTime: other: "{{- .ReadingTime -}}min" PostViews: other: "Views" +PostComments: + other: "Comments" PostTag: other: "Tags" PostReadMore: diff --git a/i18n/zh-cn.yaml b/i18n/zh-cn.yaml index 8a77470..139470d 100644 --- a/i18n/zh-cn.yaml +++ b/i18n/zh-cn.yaml @@ -94,6 +94,8 @@ PostReadTime: other: "{{- .ReadingTime -}}分钟" PostViews: other: 浏览 +PostComments: + other: 评论 PostTags: other: 标签 PostReadMore: diff --git a/layouts/partials/head/config.html b/layouts/partials/head/config.html index 559dd6a..60fb5b4 100644 --- a/layouts/partials/head/config.html +++ b/layouts/partials/head/config.html @@ -7,12 +7,12 @@ "title" .Page.Title -}} -{{/* Append waline pageview plugin */}} -{{ if and .Site.Params.waline .Site.Params.waline.pageView }} -{{ $pageview := dict +{{/* Append waline pageview & comment plugin */}} +{{ if and .Site.Params.waline ( or .Site.Params.waline.pageView .Site.Params.waline.comment) }} +{{ $counter := dict "js" .Site.Data.resources.plugins.waline.js }} -{{ $pageCfg = merge $pageCfg (dict "pageview" $pageview) }} +{{ $pageCfg = merge $pageCfg (dict "waline" $counter) }} {{ end }} {{/* Append mermaid plugin */}} diff --git a/layouts/partials/post/header.html b/layouts/partials/post/header.html index 50293cf..ca348a8 100644 --- a/layouts/partials/post/header.html +++ b/layouts/partials/post/header.html @@ -27,17 +27,25 @@ {{ partial "post/header_meta/created_date.html" . }} {{ partial "post/header_meta/update_date.html" . }} {{ partial "post/header_meta/categories.html" . }} - {{ if .Site.Params.waline.pageView }} - {{ if and $.IsHome (not (isset .Params "extlink")) }} - {{ partial "post/header_meta/views.html" . }} - {{ end }} - {{ end }} + {{ if and $.IsHome (not (isset .Params "extlink")) }} +
+ {{ if .Site.Params.waline.pageView }} + {{ partial "post/header_meta/views.html" . }} + {{ end }} + {{ if .Site.Params.waline.comment }} + {{ partial "post/header_meta/comments.html" . }} + {{ end }} +
+ {{ end }} {{- if not $.IsHome }}
{{ partial "post/header_meta/words.html" . }} {{ partial "post/header_meta/readtime.html" . }} {{ partial "post/header_meta/views.html" . }} + {{ if .Site.Params.waline.comment }} + {{ partial "post/header_meta/comments.html" . }} + {{ end }}
{{ end }} diff --git a/layouts/partials/post/header_meta/comments.html b/layouts/partials/post/header_meta/comments.html new file mode 100644 index 0000000..2e54dd0 --- /dev/null +++ b/layouts/partials/post/header_meta/comments.html @@ -0,0 +1,11 @@ + + + + + + + \ No newline at end of file diff --git a/layouts/partials/scripts/global.html b/layouts/partials/scripts/global.html index e687835..69e14d3 100644 --- a/layouts/partials/scripts/global.html +++ b/layouts/partials/scripts/global.html @@ -49,9 +49,9 @@ {{ if isset .Site.Params "waline" }} {{ $walinejs := resources.Get "js/third-party/comments/waline.js" }} {{ $nextjs = $nextjs | append $walinejs }} -{{ if .Site.Params.waline.pageView }} - {{ $pageviewjs := resources.Get "js/third-party/others/pageview.js" }} - {{ $nextjs = $nextjs | append $pageviewjs }} +{{ if or .Site.Params.waline.pageView .Site.Params.waline.comment }} + {{ $counterjs := resources.Get "js/third-party/others/counter.js" }} + {{ $nextjs = $nextjs | append $counterjs }} {{ end }} {{ end }} {{ if isset .Site.Params "giscus" }}