Skip to content

Commit

Permalink
feat(hexo/next): add hexo-next-waline plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Dec 6, 2020
1 parent 35aba63 commit 1364169
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 2 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

Valine
Copyright (C) 2017-2018 xCss
Waline
Copyright (C) 2020-2021 lizheming

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
13 changes: 13 additions & 0 deletions packages/hexo-next/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Waline
# For more information: https://waline.js.org, https://github.com/lizheming/waline
waline:
enable: false
serverURL: https://waline.vercel.app # Waline server address url
placeholder: Just go go # Comment box placeholder
avatar: mm # Gravatar style
meta: [nick, mail, link] # Custom comment header
pageSize: 10 # Pagination size
lang: # Language, available values: en, zh-cn
# Warning: Do not enable both `waline.visitor` and `leancloud_visitors`.
visitor: false # Article reading statistic
requiredFields: [] # Set required fields: [nick] | [nick, mail]
80 changes: 80 additions & 0 deletions packages/hexo-next/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/* global hexo */

'use strict';

const Util = require('@next-theme/utils');
const utils = new Util(hexo, __dirname);

function capitalize(input) {
return input.toString().charAt(0).toUpperCase() + input.toString().substr(1);
}

function iconText(icon, key, defaultValue) {
if (!defaultValue) {
defaultValue = capitalize(key);
}
return `
<span class="post-meta-item-icon">
<i class="${icon}"></i>
</span>
{%- set post_meta_comment = __('post.comments.${key}') %}
{%- if post_meta_comment == 'post.comments.${key}' %}
{%- set post_meta_comment = '${defaultValue}' %}
{%- endif %}
<span class="post-meta-item-text">{{ post_meta_comment + __('symbol.colon') }}</span>
`;
}

function warning(...args) {
hexo.log.warn(`Since ${args[0]} is turned on, the ${args[1]} is disabled to avoid potential hazards.`);
};

// Add comment
hexo.extend.filter.register('theme_inject', injects => {

const config = utils.defaultConfigFile('waline', 'default.yaml');
if (!config.enable || !config.serverURL) return;

injects.comment.raw('waline', '<div class="comments" id="waline-comments"></div>', {}, { cache: true });

injects.bodyEnd.raw('waline', utils.getFileContent('waline.njk'));

});

// Add post_meta
hexo.extend.filter.register('theme_inject', injects => {

const config = utils.defaultConfigFile('waline', 'default.yaml');
if (!config.enable || !config.serverURL) return;

injects.postMeta.raw('waline', `
{% if post.comments and (is_post() or theme.waline.comment_count) %}
<span class="post-meta-item">
${iconText('far fa-comment', 'waline')}
<a title="waline" href="{{ url_for(post.path) }}#waline-comments" itemprop="discussionUrl">
<span class="post-comments-count waline-comment-count" id="{{ url_for(post.path) }}" data-xid="{{ url_for(post.path) }}" itemprop="commentCount"></span>
</a>
</span>
{% endif %}
`, {}, {});

if (config.visitor) {
if (hexo.theme.config.leancloud_visitors && hexo.theme.config.leancloud_visitors.enable) {
warning('waline.visitor', 'leancloud_visitors');
hexo.theme.config.leancloud_visitors.enable = false;
return;
}


injects.postMeta.raw('waline', `
<span id="{{ url_for(post.path) }}" class="post-meta-item leancloud_visitors" data-flag-title="{{ post.title }}" title="{{ __('post.views') }}">
<span class="post-meta-item-icon">
<i class="far fa-eye"></i>
</span>
<span class="post-meta-item-text">{{ __('post.views') + __('symbol.colon') }}</span>
<span class="leancloud-visitors-count"></span>
</span>
`, {}, {});
}

});
21 changes: 21 additions & 0 deletions packages/hexo-next/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@waline/hexo-next",
"version": "1.0.0",
"description": "Waline comment system for NexT.",
"main": "index.js",
"repository": "lizheming/waline",
"keywords": [
"Hexo",
"NexT",
"Waline"
],
"author": "lizheming <i@imnerd.org>",
"license": "MIT",
"bugs": {
"url": "https://github.com/lizheming/waline/issues"
},
"homepage": "https://github.com/lizheming/waline/packages/#readme",
"dependencies": {
"@next-theme/utils": "^1.1.0"
}
}
5 changes: 5 additions & 0 deletions packages/hexo-next/renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"config:base"
]
}
10 changes: 10 additions & 0 deletions packages/hexo-next/waline.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
NexT.utils.loadComments('#waline-comments', () => {
NexT.utils.getScript('https://cdn.jsdelivr.net/npm/@waline/client/dist/Waline.min.js', () => {
new Waline(Object.assign({{ config.waline | safedump }}, {
el: '#waline-comments',
path: {{ url_for(page.path) | replace(r/index\.html$/, '') | safedump }}
}));
}, window.Waline);
});
</script>

0 comments on commit 1364169

Please sign in to comment.