From a36be60772a1c75282283384348968679ddc93bd Mon Sep 17 00:00:00 2001 From: jiangtj <116749895@qq.com> Date: Tue, 23 Jul 2019 19:04:25 +0800 Subject: [PATCH 1/6] Refactor comments --- layout/_macro/post.swig | 52 +++---------------- layout/_partials/comments.swig | 50 +++--------------- layout/_third-party/comments/disqusjs.swig | 2 + layout/_third-party/comments/gitalk.swig | 2 + layout/_third-party/comments/index.swig | 17 ------ layout/_third-party/comments/livere.swig | 2 + layout/_third-party/index.swig | 1 - scripts/filters/comment/changyan.js | 48 +++++++++++++++++ scripts/filters/comment/common.js | 12 +++++ scripts/filters/comment/disqus.js | 46 ++++++++++++++++ scripts/filters/comment/disqusjs.js | 24 +++++++++ .../comment/facebook-comments-plugin.js | 43 +++++++++++++++ scripts/filters/comment/gitalk.js | 16 ++++++ scripts/filters/comment/livere.js | 20 +++++++ scripts/filters/comment/valine.js | 38 ++++++++++++++ scripts/filters/comment/vkontakte.js | 12 +++++ scripts/filters/comment/warn.js | 19 +++++++ scripts/injects-point.js | 2 +- 18 files changed, 297 insertions(+), 109 deletions(-) delete mode 100644 layout/_third-party/comments/index.swig create mode 100644 scripts/filters/comment/changyan.js create mode 100644 scripts/filters/comment/common.js create mode 100644 scripts/filters/comment/disqus.js create mode 100644 scripts/filters/comment/disqusjs.js create mode 100644 scripts/filters/comment/facebook-comments-plugin.js create mode 100644 scripts/filters/comment/gitalk.js create mode 100644 scripts/filters/comment/livere.js create mode 100644 scripts/filters/comment/valine.js create mode 100644 scripts/filters/comment/vkontakte.js create mode 100644 scripts/filters/comment/warn.js diff --git a/layout/_macro/post.swig b/layout/_macro/post.swig index 512512a9ed..8dd6feaaba 100644 --- a/layout/_macro/post.swig +++ b/layout/_macro/post.swig @@ -133,50 +133,6 @@ {% endif %} - {% if post.comments %} - {% macro comments() %} - - - - - {% endmacro %} - {% if theme.facebook_comments_plugin.enable %} - {{ comments() }} - {{ __('post.comments_count') + __('symbol.colon') }} - - 0 - - - {% elif theme.disqus.enable and theme.disqus.count %} - {{ comments() }} - {{ __('post.comments_count') + __('symbol.colon') }} - - - - - {% elif theme.changyan.enable and theme.changyan.appid and theme.changyan.appkey %} - {{ comments() }} - {% if is_post() %} - {{ __('post.comments_count') + __('symbol.colon') }} - - - - {% else %} - {{ __('post.comments_count') + __('symbol.colon') }} - - - - {% endif %} - {% elif (is_post() or theme.valine.comment_count) and theme.valine.enable and theme.valine.appid and theme.valine.appkey %} - {{ comments() }} - {{ __('post.comments_count') + __('symbol.colon') }} - - - - - {% endif %} - {% endif %} - {# LeanCloud PageView #} {% if theme.leancloud_visitors.enable or (theme.valine.enable and theme.valine.appid and theme.valine.appkey and theme.valine.visitor) %} @@ -202,9 +158,13 @@ {% endif %} {% for inject_item in theme.injects.postMeta %} - + {% if inject_item.locals.disableDefaultLayout %} {{ partial(inject_item.layout, inject_item.locals, inject_item.options) }} - + {% else %} + + {{ partial(inject_item.layout, inject_item.locals, inject_item.options) }} + + {% endif %} {% endfor %} {% if theme.symbols_count_time.separated_meta %}
{% endif %} diff --git a/layout/_partials/comments.swig b/layout/_partials/comments.swig index 6508fb4f5e..e6f16d7a3a 100644 --- a/layout/_partials/comments.swig +++ b/layout/_partials/comments.swig @@ -1,47 +1,9 @@ {% if page.comments %} - - {% if theme.facebook_sdk.enable and theme.facebook_comments_plugin.enable %} -
-
-
-
- - {% elif theme.vkontakte_api.enable and theme.vkontakte_api.comments %} -
-
-
- - {% elif theme.disqus.enable or (theme.disqusjs.enable and theme.disqusjs.apikey and theme.disqusjs.shortname) %} -
-
- -
-
- - {% elif theme.livere_uid %} -
-
-
- - {% elif theme.changyan.enable and theme.changyan.appid and theme.changyan.appkey %} -
-
-
- - {% elif theme.valine.enable and theme.valine.appid and theme.valine.appkey %} -
-
- - {% elif theme.gitalk.enable %} -
-
- + {% if theme.injects.comment.length == 1 %} + {% set inject_item = theme.injects.comment[0] %} + {{ partial(inject_item.layout, inject_item.locals, inject_item.options) }} {% endif %} - +{# {% for inject_item in theme.injects.comment %} + {{ partial(inject_item.layout, inject_item.locals, inject_item.options) }} +{% endfor %} #} {% endif %} diff --git a/layout/_third-party/comments/disqusjs.swig b/layout/_third-party/comments/disqusjs.swig index c7b2f1fb72..ae512f8584 100644 --- a/layout/_third-party/comments/disqusjs.swig +++ b/layout/_third-party/comments/disqusjs.swig @@ -1,3 +1,4 @@ +{% if page.comments %} {% set disqusjs_css_url = '//cdn.jsdelivr.net/npm/disqusjs@1/dist/disqusjs.css' %} {% if theme.vendors.disqusjs_css %} {% set disqusjs_css_url = theme.vendors.disqusjs_css %} @@ -30,3 +31,4 @@ window.addEventListener('load', loadDsqJS, false); +{% endif %} diff --git a/layout/_third-party/comments/gitalk.swig b/layout/_third-party/comments/gitalk.swig index 749ec59e9c..aa03b88dd8 100644 --- a/layout/_third-party/comments/gitalk.swig +++ b/layout/_third-party/comments/gitalk.swig @@ -1,3 +1,4 @@ +{% if page.comments %} {% set gitalk_js_url = '//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js' %} {% if theme.vendors.gitalk_js %} {% set gitalk_js_url = theme.vendors.gitalk_js %} @@ -33,3 +34,4 @@ }); gitalk.render('gitalk-container'); +{% endif %} diff --git a/layout/_third-party/comments/index.swig b/layout/_third-party/comments/index.swig deleted file mode 100644 index c3231674ef..0000000000 --- a/layout/_third-party/comments/index.swig +++ /dev/null @@ -1,17 +0,0 @@ -{% if theme.disqus.enable %} - {% include 'disqus.swig' %} -{% elif theme.changyan.enable and theme.changyan.appid and theme.changyan.appkey %} - {% include 'changyan.swig' %} -{% elif theme.valine.enable and theme.valine.appid and theme.valine.appkey %} - {% include 'valine.swig' %} -{% endif %} - -{% if page.comments %} - {% if theme.livere_uid %} - {% include 'livere.swig' %} - {% elif theme.gitalk.enable %} - {% include 'gitalk.swig' %} - {% elif theme.disqusjs.enable and theme.disqusjs.apikey and theme.disqusjs.shortname %} - {% include 'disqusjs.swig' %} - {% endif %} -{% endif %} diff --git a/layout/_third-party/comments/livere.swig b/layout/_third-party/comments/livere.swig index 20610dc45f..f19164850b 100644 --- a/layout/_third-party/comments/livere.swig +++ b/layout/_third-party/comments/livere.swig @@ -1,3 +1,4 @@ +{% if page.comments %} {% set liveRefer = page.path %} {% if not is_post() %} {% set liveRefer = page.path.replace('index.html', '') %} @@ -16,3 +17,4 @@ e.parentNode.insertBefore(j, e); })(document, 'script'); +{% endif %} diff --git a/layout/_third-party/index.swig b/layout/_third-party/index.swig index 8dc20e89de..40578567a8 100644 --- a/layout/_third-party/index.swig +++ b/layout/_third-party/index.swig @@ -2,7 +2,6 @@ {% include 'quicklink.swig' %} {% include 'bookmark.swig' %} {% include 'schedule.swig' %} -{% include 'comments/index.swig' %} {% include 'analytics/analytics-with-jquery.swig' %} {% include 'search/index.swig' %} {% include 'chat/index.swig' %} diff --git a/scripts/filters/comment/changyan.js b/scripts/filters/comment/changyan.js new file mode 100644 index 0000000000..f6c7b92bee --- /dev/null +++ b/scripts/filters/comment/changyan.js @@ -0,0 +1,48 @@ +/* global hexo */ + +'use strict'; + +const path = require('path'); + +const {iconText} = require('./common'); + +// Add comment +hexo.extend.filter.register('theme_inject', function(injects) { + let theme = hexo.theme.config; + if (!theme.changyan.enable || !theme.changyan.appid || !theme.changyan.appkey) return; + + injects.comment.raw('changyan', ` +
+
+
+ `, {}, {cache: true}); + + injects.bodyEnd.file('changyan', path.join(hexo.theme_dir, 'layout/_third-party/comments/changyan.swig')); + +}, hexo.config.inject_priority_changyan); + +// Add post_meta +hexo.extend.filter.register('theme_inject', function(injects) { + let theme = hexo.theme.config; + if (!theme.changyan.enable || !theme.changyan.appid || !theme.changyan.appkey) return; + + injects.postMeta.raw('changyan', ` + {% if post.comments %} + + ${iconText} + {% if is_post() %} + + + + {% else %} + + + + {% endif %} + + {% endif %} + `, { + disableDefaultLayout: true + }); + +}, hexo.config.inject_priority_changyan_post_meta); diff --git a/scripts/filters/comment/common.js b/scripts/filters/comment/common.js new file mode 100644 index 0000000000..e92252b1a1 --- /dev/null +++ b/scripts/filters/comment/common.js @@ -0,0 +1,12 @@ +'use strict'; + +module.exports = { + iconText: ` + + + + {% if theme.post_meta.item_text %} + {{ __('post.comments_count') + __('symbol.colon') }} + {% endif %} + ` +}; diff --git a/scripts/filters/comment/disqus.js b/scripts/filters/comment/disqus.js new file mode 100644 index 0000000000..afb7ca1feb --- /dev/null +++ b/scripts/filters/comment/disqus.js @@ -0,0 +1,46 @@ +/* global hexo */ + +'use strict'; + +const path = require('path'); + +const {iconText} = require('./common'); + +// Add comment +hexo.extend.filter.register('theme_inject', function(injects) { + let theme = hexo.theme.config; + if (!theme.disqus.enable || !theme.disqus.shortname) return; + + injects.comment.raw('disqus', ` +
+
+ +
+
+ `, {}, {cache: true}); + + injects.bodyEnd.file('disqus', path.join(hexo.theme_dir, 'layout/_third-party/comments/disqus.swig')); + +}, hexo.config.inject_priority_disqus); + +// Add post_meta +hexo.extend.filter.register('theme_inject', function(injects) { + let theme = hexo.theme.config; + if (!theme.disqus.enable || !theme.disqus.shortname || !theme.disqus.count) return; + + injects.postMeta.raw('disqus', ` + {% if post.comments %} + + ${iconText} + + + + + {% endif %} + `, { + disableDefaultLayout: true + }); + +}, hexo.config.inject_priority_disqus_post_meta); diff --git a/scripts/filters/comment/disqusjs.js b/scripts/filters/comment/disqusjs.js new file mode 100644 index 0000000000..feccb7c271 --- /dev/null +++ b/scripts/filters/comment/disqusjs.js @@ -0,0 +1,24 @@ +/* global hexo */ + +'use strict'; + +const path = require('path'); + +// Add comment +hexo.extend.filter.register('theme_inject', function(injects) { + let theme = hexo.theme.config; + if (!theme.disqusjs.enable || !theme.disqusjs.shortname || !theme.disqusjs.apikey) return; + + injects.comment.raw('disqusjs', ` +
+
+ +
+
+ `, {}, {cache: true}); + + injects.bodyEnd.file('disqusjs', path.join(hexo.theme_dir, 'layout/_third-party/comments/disqusjs.swig')); + +}, hexo.config.inject_priority_disqusjs); diff --git a/scripts/filters/comment/facebook-comments-plugin.js b/scripts/filters/comment/facebook-comments-plugin.js new file mode 100644 index 0000000000..48db18b3f4 --- /dev/null +++ b/scripts/filters/comment/facebook-comments-plugin.js @@ -0,0 +1,43 @@ +/* global hexo */ + +'use strict'; + +const {iconText} = require('./common'); + +// Add comment +hexo.extend.filter.register('theme_inject', function(injects) { + let theme = hexo.theme.config; + if (!theme.facebook_sdk.enable || !theme.facebook_comments_plugin.enable) return; + + injects.comment.raw('facebook-comments-plugin', ` +
+
+
+
+ `); + +}, hexo.config.inject_priority_facebook_comments_plugin); + +// Add post_meta +hexo.extend.filter.register('theme_inject', function(injects) { + let theme = hexo.theme.config; + if (!theme.facebook_sdk.enable || !theme.facebook_comments_plugin.enable) return; + + injects.postMeta.raw('facebook-comments-plugin', ` + {% if post.comments %} + + ${iconText} + + 0 + + + {% endif %} + `, { + disableDefaultLayout: true + }); + +}, hexo.config.inject_priority_facebook_comments_plugin_post_meta); diff --git a/scripts/filters/comment/gitalk.js b/scripts/filters/comment/gitalk.js new file mode 100644 index 0000000000..3b69b87245 --- /dev/null +++ b/scripts/filters/comment/gitalk.js @@ -0,0 +1,16 @@ +/* global hexo */ + +'use strict'; + +const path = require('path'); + +// Add comment +hexo.extend.filter.register('theme_inject', function(injects) { + let theme = hexo.theme.config; + if (!theme.gitalk.enable) return; + + injects.comment.raw('gitalk', `
`, {}, {cache: true}); + + injects.bodyEnd.file('gitalk', path.join(hexo.theme_dir, 'layout/_third-party/comments/gitalk.swig')); + +}, hexo.config.inject_priority_gitalk); diff --git a/scripts/filters/comment/livere.js b/scripts/filters/comment/livere.js new file mode 100644 index 0000000000..d41fcdc912 --- /dev/null +++ b/scripts/filters/comment/livere.js @@ -0,0 +1,20 @@ +/* global hexo */ + +'use strict'; + +const path = require('path'); + +// Add comment +hexo.extend.filter.register('theme_inject', function(injects) { + let theme = hexo.theme.config; + if (!theme.livere_uid) return; + + injects.comment.raw('livere', ` +
+
+
+ `, {}, {cache: true}); + + injects.bodyEnd.file('livere', path.join(hexo.theme_dir, 'layout/_third-party/comments/livere.swig')); + +}, hexo.config.inject_priority_livere); diff --git a/scripts/filters/comment/valine.js b/scripts/filters/comment/valine.js new file mode 100644 index 0000000000..06fc87be8a --- /dev/null +++ b/scripts/filters/comment/valine.js @@ -0,0 +1,38 @@ +/* global hexo */ + +'use strict'; + +const path = require('path'); + +const {iconText} = require('./common'); + +// Add comment +hexo.extend.filter.register('theme_inject', function(injects) { + let theme = hexo.theme.config; + if (!theme.valine.enable || !theme.valine.appid || !theme.valine.appkey) return; + + injects.comment.raw('valine', `
`, {}, {cache: true}); + + injects.bodyEnd.file('valine', path.join(hexo.theme_dir, 'layout/_third-party/comments/valine.swig')); + +}, hexo.config.inject_priority_valine); + +// Add post_meta +hexo.extend.filter.register('theme_inject', function(injects) { + let theme = hexo.theme.config; + if (!theme.valine.enable || !theme.valine.appid || !theme.valine.appkey) return; + + injects.postMeta.raw('valine', ` + {% if post.comments and (is_post() or theme.valine.comment_count) %} + + ${iconText} + + + + + {% endif %} + `, { + disableDefaultLayout: true + }); + +}, hexo.config.inject_priority_valine_post_meta); diff --git a/scripts/filters/comment/vkontakte.js b/scripts/filters/comment/vkontakte.js new file mode 100644 index 0000000000..99d5301f98 --- /dev/null +++ b/scripts/filters/comment/vkontakte.js @@ -0,0 +1,12 @@ +/* global hexo */ + +'use strict'; + +// Add comment +hexo.extend.filter.register('theme_inject', function(injects) { + let theme = hexo.theme.config; + if (!theme.vkontakte_api.enable || !theme.vkontakte_api.comments) return; + + injects.comment.raw('vkontakte-comments', '
', {}, {cache: true}); + +}, hexo.config.inject_priority_vkontakte_comments_plugin); diff --git a/scripts/filters/comment/warn.js b/scripts/filters/comment/warn.js new file mode 100644 index 0000000000..08b155cb52 --- /dev/null +++ b/scripts/filters/comment/warn.js @@ -0,0 +1,19 @@ +/* global hexo */ + +'use strict'; + +hexo.extend.filter.register('theme_inject', function(injects) { + if (injects.comment.raws.length > 1) { + hexo.log.warn(`It is currently not supported to launch mutli-comments systems at the same time, so stay tuned for subsequent versions of iteration.`); + hexo.log.warn(`Please keep one of the following.`); + injects.comment.raws.forEach(element => { + let commentName = element.name; + // List comment system + hexo.log.warn(' - ' + commentName); + // Close comment system + injects.postMeta.raw(commentName, '', {disableDefaultLayout: true}, {cache: true}); + injects.comment.raw(commentName, '', {}, {cache: true}); + injects.bodyEnd.raw(commentName, '', {}, {cache: true}); + }); + } +}, 999); diff --git a/scripts/injects-point.js b/scripts/injects-point.js index 2b44fbc4f0..c0f1af8f46 100644 --- a/scripts/injects-point.js +++ b/scripts/injects-point.js @@ -1,6 +1,6 @@ 'use strict'; module.exports = { - views: ['head', 'header', 'sidebar', 'postMeta', 'postBodyEnd', 'footer', 'bodyEnd'], + views: ['head', 'header', 'sidebar', 'postMeta', 'postBodyEnd', 'footer', 'bodyEnd', 'comment'], styles: ['variable', 'mixin', 'style'] }; From 3c2e080abe04b63c8537fd8ab78ccf3a96015adb Mon Sep 17 00:00:00 2001 From: jiangtj <116749895@qq.com> Date: Tue, 23 Jul 2019 19:14:44 +0800 Subject: [PATCH 2/6] Use partial insteal of macro --- layout/_macro/post.swig | 4 ---- layout/index.swig | 3 +-- layout/post.swig | 3 +-- 3 files changed, 2 insertions(+), 8 deletions(-) diff --git a/layout/_macro/post.swig b/layout/_macro/post.swig index 8dd6feaaba..66ffb41e08 100644 --- a/layout/_macro/post.swig +++ b/layout/_macro/post.swig @@ -1,5 +1,3 @@ -{% macro render(post, is_index, post_extra_class) %} - {% set headlessPost = Array.prototype.indexOf.call(['quote', 'picture'], post.type) > -1 %} {% set post_class = 'post post-type-' + post.type | default('normal') %} @@ -406,5 +404,3 @@ {### END POST BLOCK ###} {######################} - -{% endmacro %} diff --git a/layout/index.swig b/layout/index.swig index 3b25cd5cb3..acc4a05923 100644 --- a/layout/index.swig +++ b/layout/index.swig @@ -1,5 +1,4 @@ {% extends '_layout.swig' %} -{% import '_macro/post.swig' as post_template %} {% import '_macro/sidebar.swig' as sidebar_template %} {% block title %}{{ title }}{% if theme.index_with_subtitle and subtitle %} - {{ subtitle }}{% endif %}{% endblock %} @@ -11,7 +10,7 @@ {% block content %}
{% for post in page.posts %} - {{ post_template.render(post, true) }} + {{ partial('_macro/post.swig', {is_index: true}) }} {% endfor %}
diff --git a/layout/post.swig b/layout/post.swig index a4b825f70a..9e20dfc057 100644 --- a/layout/post.swig +++ b/layout/post.swig @@ -1,5 +1,4 @@ {% extends '_layout.swig' %} -{% import '_macro/post.swig' as post_template %} {% import '_macro/sidebar.swig' as sidebar_template %} {% block title %}{{ page.title }} | {{ title }}{% endblock %} @@ -9,7 +8,7 @@ {% block content %}
- {{ post_template.render(page) }} + {{ partial('_macro/post.swig', {post: page}) }}
{% endblock %} From 1e92753e5bcc9614ce10ed3fbcde682702313686 Mon Sep 17 00:00:00 2001 From: jiangtj <116749895@qq.com> Date: Wed, 24 Jul 2019 10:03:35 +0800 Subject: [PATCH 3/6] Fix QA --- scripts/filters/comment/changyan.js | 4 ++-- scripts/filters/comment/disqus.js | 4 ++-- scripts/filters/comment/disqusjs.js | 2 +- scripts/filters/comment/facebook-comments-plugin.js | 2 +- scripts/filters/comment/gitalk.js | 4 ++-- scripts/filters/comment/livere.js | 2 +- scripts/filters/comment/valine.js | 6 +++--- scripts/filters/comment/vkontakte.js | 2 +- scripts/filters/comment/warn.js | 4 ++-- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/scripts/filters/comment/changyan.js b/scripts/filters/comment/changyan.js index f6c7b92bee..cac2c00fc6 100644 --- a/scripts/filters/comment/changyan.js +++ b/scripts/filters/comment/changyan.js @@ -10,7 +10,7 @@ const {iconText} = require('./common'); hexo.extend.filter.register('theme_inject', function(injects) { let theme = hexo.theme.config; if (!theme.changyan.enable || !theme.changyan.appid || !theme.changyan.appkey) return; - + injects.comment.raw('changyan', `
@@ -25,7 +25,7 @@ hexo.extend.filter.register('theme_inject', function(injects) { hexo.extend.filter.register('theme_inject', function(injects) { let theme = hexo.theme.config; if (!theme.changyan.enable || !theme.changyan.appid || !theme.changyan.appkey) return; - + injects.postMeta.raw('changyan', ` {% if post.comments %}