Skip to content

Commit

Permalink
init hexo-next-twikoo
Browse files Browse the repository at this point in the history
  • Loading branch information
imaegoo committed Jan 8, 2021
1 parent c8fa54e commit d98becd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 110 deletions.
37 changes: 14 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@
# Hexo NexT Valine
# Hexo NexT Twikoo

![Theme Version](https://img.shields.io/badge/NexT-v7.3.0+-blue?style=flat-square)
![Package Version](https://img.shields.io/github/package-json/v/next-theme/hexo-next-valine?style=flat-square)
![Package Version](https://img.shields.io/github/package-json/v/imaegoo/hexo-next-twikoo?style=flat-square)

Valine comment system for NexT. Valine is a fast, simple & efficient Leancloud based no back end comment system.
Twikoo comment system for NexT. Twikoo is a simple, safe, free comment system.

## Install

```bash
npm install next-theme/hexo-next-valine
npm install hexo-next-twikoo
```

## Register

1. Create an account or log into [LeanCloud](https://www.leancloud.cn/dashboard/login.html#/signin), and then click on the bottom left corner to [create the application](https://www.leancloud.cn/dashboard/applist.html#/newapp) in [dashboard](https://www.leancloud.cn/dashboard/applist.html#/apps).
![Valine](https://theme-next.js.org/images/valine-1.png)
2. Go to the application you just created, select `Settings → App Keys` in the lower left corner, and you will see your APP ID and APP Key.
![Valine](https://theme-next.js.org/images/valine-2.png)
Please refer to https://twikoo.js.org/quick-start.html

## Configure

Set the value `enable` to `true`, add the obtained APP ID (`appId`) and APP Key (`appKey`), and edit other configurations in `valine` section in the config file as following. You can config those in both **hexo** or **theme** `_config.yml`:
Set the value `enable` to `true`, add the obtained env ID (`envId`), and edit other configurations in `twikoo` section in the config file as following. You can config those in both **hexo** or **theme** `_config.yml`:

```yml next/_config.yml
# Valine
# For more information: https://valine.js.org, https://github.com/xCss/Valine
valine:
enable: false
appId: # your leancloud application appid
appKey: # your leancloud application appkey
serverURLs: # When the custom domain name is enabled, fill it in here
placeholder: Just go go # comment box placeholder
avatar: mm # gravatar style
meta: [nick, mail, link] # Custom comment header
pageSize: 10 # pagination size
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
comment_count: true # If false, comment count will only be displayed in post page, not in home page
recordIP: false # Whether to record the commenter IP
# Twikoo
# For more information: https://twikoo.js.org, https://github.com/imaegoo/twikoo
twikoo:
enable: true
visitor: true
envId: twikoo-1gs9l0fb17e7897a # 环境 ID,搭建教程:https://twikoo.js.org/quick-start.html
# region: ap-guangzhou # 环境地域,默认为 ap-shanghai
# path: 'window.location.pathname' # 自定义文章路径
```
24 changes: 7 additions & 17 deletions default.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
# Valine
# For more information: https://valine.js.org, https://github.com/xCss/Valine
valine:
# Twikoo
# For more information: https://twikoo.js.org, https://github.com/imaegoo/twikoo
twikoo:
enable: false
appId: # Your leancloud application appid
appKey: # Your leancloud application appkey
serverURLs: # When the custom domain name is enabled, fill it in here
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 `valine.visitor` and `leancloud_visitors`.
visitor: false # Article reading statistic
comment_count: true # If false, comment count will only be displayed in post page, not in home page
recordIP: false # Whether to record the commenter IP
enableQQ: false # Whether to enable the Nickname box to automatically get QQ Nickname and QQ Avatar
requiredFields: [] # Set required fields: [nick] | [nick, mail]
visitor: false
envId: twikoo-1gs9l0fb17e7897a # 环境 ID,搭建教程:https://twikoo.js.org/quick-start.html
# region: ap-guangzhou # 环境地域,默认为 ap-shanghai
# path: 'window.location.pathname' # 自定义文章路径
60 changes: 9 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,31 @@
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('valine', 'default.yaml');
if (!config.enable || !config.appId || !config.appKey) return;
const config = utils.defaultConfigFile('twikoo', 'default.yaml');
if (!config.enable || !config.envId) return;

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

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

});

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

const config = utils.defaultConfigFile('valine', 'default.yaml');
if (!config.enable || !config.appId || !config.appKey) return;

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

const config = utils.defaultConfigFile('twikoo', 'default.yaml');
if (!config.enable || !config.envId) return;
if (config.visitor) {
if (hexo.theme.config.leancloud_visitors && hexo.theme.config.leancloud_visitors.enable) {
warning('valine.visitor', 'leancloud_visitors');
hexo.theme.config.leancloud_visitors.enable = false;
return;
}

injects.postMeta.raw('valine', `
<span id="{{ url_for(post.path) }}" class="post-meta-item leancloud_visitors" data-flag-title="{{ post.title }}" title="{{ __('post.views') }}">
injects.postMeta.raw('twikoo', `
<span id="{{ url_for(post.path) }}" class="post-meta-item twikoo_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 id="twikoo_visitors"></span>
</span>
`, {}, {});
}
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{
"name": "hexo-next-valine",
"name": "hexo-next-twikoo",
"version": "1.0.0",
"description": "Valine comment system for NexT.",
"description": "Twikoo comment system for NexT.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "next-theme/hexo-next-valine",
"repository": "https://github.com/imaegoo/hexo-next-twikoo",
"keywords": [
"Hexo",
"NexT"
"NexT",
"twikoo"
],
"author": "Mimi <stevenjoezhang@gmail.com> (https://zhangshuqiao.org)",
"author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
"license": "LGPL-3.0",
"bugs": {
"url": "https://github.com/next-theme/hexo-next-valine/issues"
"url": "https://github.com/imaegoo/hexo-next-twikoo/issues"
},
"homepage": "https://github.com/next-theme/hexo-next-valine#readme",
"homepage": "https://github.com/imaegoo/hexo-next-twikoo#readme",
"dependencies": {
"@next-theme/utils": "^1.1.0"
}
Expand Down
9 changes: 9 additions & 0 deletions twikoo.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
NexT.utils.loadComments('#twikoo-comments', () => {
NexT.utils.getScript('https://cdn.jsdelivr.net/npm/twikoo/dist/twikoo.all.min.js', () => {
twikoo.init(Object.assign({{ config.twikoo | safedump }}, {
el: '#twikoo-comments'
}));
}, window.twikoo);
});
</script>
12 changes: 0 additions & 12 deletions valine.njk

This file was deleted.

0 comments on commit d98becd

Please sign in to comment.