|
| 1 | +/* global hexo */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const Util = require('@next-theme/utils'); |
| 6 | +const utils = new Util(hexo, __dirname); |
| 7 | + |
| 8 | +function iconText(icon, key, defaultValue) { |
| 9 | + if (!defaultValue) { |
| 10 | + defaultValue = capitalize(key); |
| 11 | + } |
| 12 | + return ` |
| 13 | + <span class="post-meta-item-icon"> |
| 14 | + <i class="${icon}"></i> |
| 15 | + </span> |
| 16 | + {%- set post_meta_comment = __('post.comments.${key}') %} |
| 17 | + {%- if post_meta_comment == 'post.comments.${key}' %} |
| 18 | + {%- set post_meta_comment = '${defaultValue}' %} |
| 19 | + {%- endif %} |
| 20 | + <span class="post-meta-item-text">{{ post_meta_comment + __('symbol.colon') }}</span> |
| 21 | + `; |
| 22 | +} |
| 23 | + |
| 24 | +function warning(...args) { |
| 25 | + hexo.log.warn(`Since ${args[0]} is turned on, the ${args[1]} is disabled to avoid potential hazards.`); |
| 26 | +}; |
| 27 | + |
| 28 | +// Add comment |
| 29 | +hexo.extend.filter.register('theme_inject', injects => { |
| 30 | + |
| 31 | + const config = utils.defaultConfigFile('valine', 'default.yaml'); |
| 32 | + if (!config.enable || !config.appId || !config.appKey) return; |
| 33 | + |
| 34 | + injects.comment.raw('valine', '<div class="comments" id="valine-comments"></div>', {}, { cache: true }); |
| 35 | + |
| 36 | + injects.bodyEnd.raw('valine', utils.getFileContent('valine.njk')); |
| 37 | + |
| 38 | +}); |
| 39 | + |
| 40 | +// Add post_meta |
| 41 | +hexo.extend.filter.register('theme_inject', injects => { |
| 42 | + |
| 43 | + const config = utils.defaultConfigFile('valine', 'default.yaml'); |
| 44 | + if (!config.enable || !config.appId || !config.appKey) return; |
| 45 | + |
| 46 | + injects.postMeta.raw('valine', ` |
| 47 | + {% if post.comments and (is_post() or theme.valine.comment_count) %} |
| 48 | + <span class="post-meta-item"> |
| 49 | + ${iconText('far fa-comment', 'valine')} |
| 50 | + <a title="valine" href="{{ url_for(post.path) }}#valine-comments" itemprop="discussionUrl"> |
| 51 | + <span class="post-comments-count valine-comment-count" data-xid="{{ url_for(post.path) }}" itemprop="commentCount"></span> |
| 52 | + </a> |
| 53 | + </span> |
| 54 | + {% endif %} |
| 55 | + `, {}, {}); |
| 56 | + |
| 57 | + if (config.visitor) { |
| 58 | + if (hexo.theme.config.leancloud_visitors && hexo.theme.config.leancloud_visitors.enable) { |
| 59 | + warning('valine.visitor', 'leancloud_visitors'); |
| 60 | + hexo.theme.config.leancloud_visitors.enable = false; |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + injects.postMeta.raw('valine', ` |
| 65 | + <span id="{{ url_for(post.path) }}" class="post-meta-item leancloud_visitors" data-flag-title="{{ post.title }}" title="{{ __('post.views') }}"> |
| 66 | + <span class="post-meta-item-icon"> |
| 67 | + <i class="far fa-eye"></i> |
| 68 | + </span> |
| 69 | + <span class="post-meta-item-text">{{ __('post.views') + __('symbol.colon') }}</span> |
| 70 | + <span class="leancloud-visitors-count"></span> |
| 71 | + </span> |
| 72 | + `, {}, {}); |
| 73 | + } |
| 74 | + |
| 75 | +}); |
0 commit comments