Skip to content

Commit 23647f3

Browse files
stevenjoezhangivan-nginx
authored andcommitted
LeanCloud comment counter now loaded faster (#559)
* Fix #271 comment counter * Update _config.yml * Update lean-analytics.swig * Update lean-analytics.swig * Update post.swig * add this
1 parent 997216e commit 23647f3

File tree

4 files changed

+68
-67
lines changed

4 files changed

+68
-67
lines changed

_config.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ cheers_enabled: true
389389
# - All fonts set here will have the following styles:
390390
# light, light italic, normal, normal italic, bold, bold italic
391391
# - Be aware that setting too much fonts will cause site running slowly
392-
# - Introduce in 5.0.1
392+
# - Introduced in 5.0.1
393393
# ---------------------------------------------------------------
394394
# CAUTION! Safari Version 10.1.2 bug: https://github.com/iissnan/hexo-theme-next/issues/1844
395395
# To avoid space between header and sidebar in Pisces / Gemini themes recommended to use Web Safe fonts for `global` (and `logo`):
@@ -523,7 +523,7 @@ disqus:
523523
count: true
524524
lazyload: false
525525

526-
# changyan
526+
# Changyan
527527
changyan:
528528
enable: false
529529
appid:
@@ -543,6 +543,7 @@ valine:
543543
guest_info: nick,mail,link # custom comment header
544544
pageSize: 10 # pagination size
545545
visitor: false # leancloud-counter-security is not supported for now. When visitor is set to be true, appid and appkey are recommended to be the same as leancloud_visitors' for counter compatibility. Article reading statistic https://valine.js.org/visitor.html
546+
comment_count: true # if false, comment count will only be displayed in post page, not in home page
546547

547548
# Support for LiveRe comments system.
548549
# You can get your uid from https://livere.com/insight/myCode (General web site)
@@ -583,7 +584,8 @@ gitalk:
583584
#baidushare:
584585
## type: button
585586

586-
# addthis Share, see: https://www.addthis.com
587+
# AddThis Share, See: https://www.addthis.com
588+
# Go to https://www.addthis.com/dashboard to customize your tools
587589
#add_this_id:
588590

589591
# NeedMoreShare2

layout/_macro/post.swig

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@
188188
<span class="post-meta-item-text">{{ __('post.comments_count') + __('symbol.colon') }}</span> <span class="post-comments-count gitment-comments-count" data-xid="{{ url_for(post.path) }}" itemprop="commentsCount"></span>
189189
</a>
190190
</span>
191-
{% elseif theme.valine.enable and theme.valine.appid and theme.valine.appkey %}
191+
{% elseif (is_post() or theme.valine.comment_count) and theme.valine.enable and theme.valine.appid and theme.valine.appkey %}
192192
<span class="post-comments-count">
193193
<span class="post-meta-divider">|</span>
194194
<span class="post-meta-item-icon">
@@ -203,16 +203,16 @@
203203

204204
{# LeanCould PageView #}
205205
{% if theme.leancloud_visitors.enable or (theme.valine.enable and theme.valine.appid and theme.valine.appkey and theme.valine.visitor) %}
206-
<span id="{{ url_for(post.path) }}" class="leancloud_visitors" data-flag-title="{{ post.title }}">
207-
<span class="post-meta-divider">|</span>
208-
<span class="post-meta-item-icon">
209-
<i class="fa fa-eye"></i>
210-
</span>
211-
{% if theme.post_meta.item_text %}
212-
<span class="post-meta-item-text">{{__('post.views') + __('symbol.colon') }}</span>
213-
{% endif %}
214-
<span class="leancloud-visitors-count"></span>
215-
</span>
206+
<span id="{{ url_for(post.path) }}" class="leancloud_visitors" data-flag-title="{{ post.title }}">
207+
<span class="post-meta-divider">|</span>
208+
<span class="post-meta-item-icon">
209+
<i class="fa fa-eye"></i>
210+
</span>
211+
{% if theme.post_meta.item_text %}
212+
<span class="post-meta-item-text">{{ __('post.views') + __('symbol.colon') }}</span>
213+
{% endif %}
214+
<span class="leancloud-visitors-count"></span>
215+
</span>
216216
{% endif %}
217217

218218
{% if not is_index and theme.busuanzi_count.enable and theme.busuanzi_count.post_views %}
@@ -262,8 +262,8 @@
262262
{% endif %}
263263

264264
{% if post.description and (not theme.excerpt_description or not is_index) %}
265-
<div class="post-description">{#
266-
#}{{ post.description }}{#
265+
<div class="post-description">{#
266+
#}{{ post.description }}{#
267267
#}</div>
268268
{% endif %}
269269

@@ -396,7 +396,7 @@
396396

397397
{% if (theme.vkontakte_api.enable and theme.vkontakte_api.like) or (theme.facebook_sdk.enable and theme.facebook_sdk.like_button) %}
398398
{% if theme.rating.enable %}
399-
<span class="post-meta-divider">|</span>
399+
<span class="post-meta-divider">|</span>
400400
{% endif %}
401401
<div class="social-like">
402402
{% if theme.vkontakte_api.enable and theme.vkontakte_api.like %}
@@ -419,16 +419,16 @@
419419
{% endif %}
420420
<div class="social_share">
421421
{% if theme.baidushare.type === "button" %}
422-
<div>
423-
{% include '../_partials/share/baidushare.swig' %}
424-
</div>
422+
<div>
423+
{% include '../_partials/share/baidushare.swig' %}
424+
</div>
425425
{% endif %}
426426
{% if theme.needmoreshare2.enable and theme.needmoreshare2.postbottom.enable %}
427-
<div id="needsharebutton-postbottom">
428-
<span class="btn">
429-
<i class="fa fa-share-alt" aria-hidden="true"></i>
430-
</span>
431-
</div>
427+
<div id="needsharebutton-postbottom">
428+
<span class="btn">
429+
<i class="fa fa-share-alt" aria-hidden="true"></i>
430+
</span>
431+
</div>
432432
{% endif %}
433433
</div>
434434
{% endif %}

layout/_third-party/analytics/lean-analytics.swig

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,62 @@
11
{% if theme.leancloud_visitors.enable and !theme.valine.visitor %}
2-
32
{# custom analytics part create by xiamo; edited by LEAFERx #}
43
<script>
54
{% if page.layout === 'post' %}
65
function addCount(Counter) {
7-
var $visitors = $(".leancloud_visitors");
6+
var $visitors = $('.leancloud_visitors');
87
var url = $visitors.attr('id').trim();
98
var title = $visitors.attr('data-flag-title').trim();
109

1110
Counter('get', '/classes/Counter', { where: JSON.stringify({ url }) })
12-
.done(function ({ results }) {
11+
.done(function({ results }) {
1312
if (results.length > 0) {
1413
var counter = results[0];
1514
{% if theme.leancloud_visitors.betterPerformance %}
1615
var $element = $(document.getElementById(url));
1716
$element.find('.leancloud-visitors-count').text(counter.time + 1);
1817
{% endif %}
19-
Counter('put', `/classes/Counter/${counter.objectId}`, JSON.stringify({ time: { "__op":"Increment", "amount":1 } }))
18+
Counter('put', '/classes/Counter/' + counter.objectId, JSON.stringify({ time: { '__op': 'Increment', 'amount': 1 } }))
2019
{% if not theme.leancloud_visitors.betterPerformance %}
21-
.done(function () {
22-
var $element = $(document.getElementById(url));
23-
$element.find('.leancloud-visitors-count').text(counter.time + 1);
24-
})
20+
.done(function() {
21+
var $element = $(document.getElementById(url));
22+
$element.find('.leancloud-visitors-count').text(counter.time + 1);
23+
})
2524
{% endif %}
26-
.fail(function ({ responseJSON }) {
25+
.fail(function({ responseJSON }) {
2726
console.log('Failed to save Visitor num, with error message: ' + responseJSON.error);
28-
})
27+
})
2928
} else {
3029
{% if theme.leancloud_visitors.security %}
3130
var $element = $(document.getElementById(url));
32-
$element.find('.leancloud-visitors-count').text('Counter not initialized! See more at console err msg.');
33-
console.error('ATTENTION! LeanCloud counter has security bug, see here how to solve it: https://github.com/theme-next/hexo-leancloud-counter-security. \n But you also can use LeanCloud without security, by set \'security\' option to \'false\'.');
31+
$element.find('.leancloud-visitors-count').text('Counter not initialized! More info at console err msg.');
32+
console.error('ATTENTION! LeanCloud counter has security bug, see how to solve it here: https://github.com/theme-next/hexo-leancloud-counter-security. \n However, you can still use LeanCloud without security, by setting `security` option to `false`.');
3433
{% else %}
35-
Counter('post', '/classes/Counter', JSON.stringify({ title: title, url: url, time: 1}))
36-
.done(function () {
34+
Counter('post', '/classes/Counter', JSON.stringify({ title: title, url: url, time: 1 }))
35+
.done(function() {
3736
var $element = $(document.getElementById(url));
3837
$element.find('.leancloud-visitors-count').text(1);
3938
})
40-
.fail(function () {
39+
.fail(function() {
4140
console.log('Failed to create');
4241
});
4342
{% endif %}
4443
}
4544
})
46-
.fail(function ({ responseJSON }) {
47-
console.log('LeanCloud Counter Error:' + responseJSON.code + " " + responseJSON.error);
45+
.fail(function({ responseJSON }) {
46+
console.log('LeanCloud Counter Error: ' + responseJSON.code + ' ' + responseJSON.error);
4847
});
4948
}
5049
{% else %}
5150
function showTime(Counter) {
5251
var entries = [];
53-
var $visitors = $(".leancloud_visitors");
52+
var $visitors = $('.leancloud_visitors');
5453

55-
$visitors.each(function () {
56-
entries.push( $(this).attr("id").trim() );
54+
$visitors.each(function() {
55+
entries.push( $(this).attr('id').trim() );
5756
});
5857

59-
Counter('get', '/classes/Counter', { where: JSON.stringify({ url: { "$in": entries } }) })
60-
.done(function ({ results }) {
58+
Counter('get', '/classes/Counter', { where: JSON.stringify({ url: { '$in': entries } }) })
59+
.done(function({ results }) {
6160
var COUNT_CONTAINER_REF = '.leancloud-visitors-count';
6261

6362
if (results.length === 0) {
@@ -73,44 +72,44 @@
7372

7473
$(element).find(COUNT_CONTAINER_REF).text(time);
7574
}
76-
for(var i = 0; i < entries.length; i++) {
75+
for (var i = 0; i < entries.length; i++) {
7776
var url = entries[i];
7877
var element = document.getElementById(url);
7978
var countSpan = $(element).find(COUNT_CONTAINER_REF);
80-
if( countSpan.text() == '') {
79+
if (countSpan.text() == '') {
8180
countSpan.text(0);
8281
}
8382
}
8483
})
85-
.fail(function ({ responseJSON }) {
86-
console.log("LeanCloud Counter Error: " + responseJSON.code + " " + responseJSON.error);
84+
.fail(function({ responseJSON }) {
85+
console.log('LeanCloud Counter Error: ' + responseJSON.code + ' ' + responseJSON.error);
8786
});
8887
}
8988
{% endif %}
9089

9190
$(function() {
92-
$.get('https://app-router.leancloud.cn/2/route?appId=' + "{{theme.leancloud_visitors.app_id}}")
93-
.done(function ({ api_server }) {
94-
var Counter = function (method, url, data) {
91+
$.get('https://app-router.leancloud.cn/2/route?appId=' + '{{ theme.leancloud_visitors.app_id }}')
92+
.done(function({ api_server }) {
93+
var Counter = function(method, url, data) {
9594
return $.ajax({
9695
method: method,
97-
url: `https://${api_server}/1.1${url}`,
96+
url: 'https://' + api_server + '/1.1' + url,
9897
headers: {
99-
'X-LC-Id': "{{theme.leancloud_visitors.app_id}}",
100-
'X-LC-Key': "{{theme.leancloud_visitors.app_key}}",
98+
'X-LC-Id': '{{ theme.leancloud_visitors.app_id }}',
99+
'X-LC-Key': '{{ theme.leancloud_visitors.app_key }}',
101100
'Content-Type': 'application/json',
102101
},
103-
data: data,
102+
data: data
104103
});
105104
};
106105
{% if page.layout === 'post' %}
107-
addCount(Counter);
106+
addCount(Counter);
108107
{% else %}
109-
if ($('.post-title-link').length >= 1) {
110-
showTime(Counter);
111-
}
108+
if ($('.post-title-link').length >= 1) {
109+
showTime(Counter);
110+
}
112111
{% endif %}
113-
})
112+
});
114113
});
115114
</script>
116115

layout/_third-party/comments/valine.swig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
<script src="{{ leancloud_uri }}"></script>
1111
<script src="{{ valine_uri }}"></script>
1212
<script>
13-
var GUEST = ['nick','mail','link'];
13+
var GUEST = ['nick', 'mail', 'link'];
1414
var guest = '{{ theme.valine.guest_info }}';
15-
guest = guest.split(',').filter(function (item) {
15+
guest = guest.split(',').filter(function(item) {
1616
return GUEST.indexOf(item) > -1;
1717
});
1818
new Valine({
19-
el: '#comments' ,
19+
el: '#comments',
2020
verify: {{ theme.valine.verify }},
2121
notify: {{ theme.valine.notify }},
2222
appId: '{{ theme.valine.appid }}',
2323
appKey: '{{ theme.valine.appkey }}',
2424
placeholder: '{{ theme.valine.placeholder }}',
2525
avatar: '{{ theme.valine.avatar }}',
26-
meta:guest,
26+
meta: guest,
2727
pageSize: '{{ theme.valine.pageSize }}' || 10,
2828
visitor: {{ theme.valine.visitor }}
2929
});

0 commit comments

Comments
 (0)