parent
cf6b7a2932
commit
74d6dcbabb
7 changed files with 189 additions and 3 deletions
@ -0,0 +1,111 @@ |
||||
# Hexo-WordCount |
||||
|
||||
[![npm](https://img.shields.io/npm/v/hexo-wordcount.svg?style=plastic)](https://npmjs.org/package/hexo-wordcount) [![npm](https://img.shields.io/npm/dm/hexo-wordcount.svg?style=plastic)](https://npmjs.org/package/hexo-wordcount) [![npm](https://img.shields.io/npm/dt/hexo-wordcount.svg?style=plastic)](https://npmjs.org/package/hexo-wordcount) |
||||
|
||||
## Installation |
||||
|
||||
```bash |
||||
yarn add hexo-wordcount |
||||
# or |
||||
npm i --save hexo-wordcount |
||||
``` |
||||
|
||||
## Usage |
||||
|
||||
### 字数统计 WordCount |
||||
|
||||
|
||||
```js |
||||
wordcount(post.content) |
||||
``` |
||||
|
||||
### 阅读时长预计 Min2Read |
||||
|
||||
```js |
||||
min2read(post.content) |
||||
``` |
||||
|
||||
设置阅读速度 Set Reading Speed: |
||||
|
||||
```js |
||||
min2read(post.content, {cn: 300, en: 160}) |
||||
// p.s. (v3.0.0 added) |
||||
``` |
||||
|
||||
### 总字数统计 TotalCount |
||||
|
||||
```js |
||||
totalcount(site) |
||||
``` |
||||
|
||||
## Demo |
||||
|
||||
### Swig |
||||
|
||||
Post Count: |
||||
|
||||
```swig |
||||
<span class="post-count">{{ wordcount(post.content) }}</span> |
||||
``` |
||||
|
||||
Post Minutes to Read: |
||||
|
||||
```swig |
||||
<span class="post-count">{{ min2read(post.content) }}</span> |
||||
``` |
||||
|
||||
Total Count: |
||||
|
||||
```swig |
||||
<span class="post-count">{{ totalcount(site) }}</span> |
||||
``` |
||||
|
||||
### Ejs |
||||
|
||||
Post Count: |
||||
|
||||
```ejs |
||||
<span class="post-count"><%= wordcount(post.content) %></span> |
||||
``` |
||||
|
||||
Post Minutes to Read: |
||||
|
||||
```ejs |
||||
<span class="post-count"><%= min2read(post.content) %></span> |
||||
``` |
||||
|
||||
Total Count: |
||||
|
||||
```ejs |
||||
<span class="post-count"><%= totalcount(site) %></span> |
||||
``` |
||||
|
||||
### Jade |
||||
|
||||
Post Count: |
||||
|
||||
```jade |
||||
span.post-count= wordcount(post.content) |
||||
``` |
||||
|
||||
Post Minutes to Read: |
||||
|
||||
```jade |
||||
span.post-count= min2read(post.content) |
||||
``` |
||||
|
||||
|
||||
Total Count: |
||||
|
||||
```swig |
||||
span.post-count= totalcount(site) |
||||
``` |
||||
|
||||
|
||||
## LICENSE |
||||
|
||||
MIT |
||||
|
||||
Alipay Donation(通过支付宝捐赠): |
||||
|
||||
![qr](https://cloud.githubusercontent.com/assets/1890238/15489630/fccbb9cc-2193-11e6-9fed-b93c59d6ef37.png) |
@ -0,0 +1,36 @@ |
||||
var util = require('hexo-util'); |
||||
var stripHTML = util.stripHTML; |
||||
|
||||
var counter = function (content) { |
||||
content = stripHTML(content); |
||||
const cn = (content.match(/[\u4E00-\u9FA5]/g) || []).length; |
||||
const en = (content.replace(/[\u4E00-\u9FA5]/g, '').match(/[a-zA-Z0-9_\u0392-\u03c9\u0400-\u04FF]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af\u0400-\u04FF]+|[\u00E4\u00C4\u00E5\u00C5\u00F6\u00D6]+|\w+/g) || []).length; |
||||
return [cn, en]; |
||||
}; |
||||
|
||||
hexo.extend.helper.register('min2read', function (content, { cn = 300, en = 160 } = {}) { |
||||
var len = counter(content); |
||||
var readingTime = len[0] / cn + len[1] / en; |
||||
return readingTime < 1 ? '1' : parseInt(readingTime, 10); |
||||
}); |
||||
|
||||
hexo.extend.helper.register('wordcount', function (content) { |
||||
var len = counter(content); |
||||
var count = len[0] + len[1]; |
||||
if (count < 1000) { |
||||
return count; |
||||
} |
||||
return Math.round(count / 100) / 10 + 'k'; |
||||
}); |
||||
|
||||
hexo.extend.helper.register('totalcount', function (site) { |
||||
var count = 0; |
||||
site.posts.forEach(function (post) { |
||||
var len = counter(post.content); |
||||
count += len[0] + len[1]; |
||||
}); |
||||
if (count < 1000) { |
||||
return count; |
||||
} |
||||
return Math.round(count / 100) / 10 + 'k'; |
||||
}); |
@ -0,0 +1,22 @@ |
||||
{ |
||||
"name": "hexo-wordcount", |
||||
"version": "6.0.1", |
||||
"description": "Post Word Count Plugin of Hexo.", |
||||
"main": "index.js", |
||||
"repository": { |
||||
"type": "git", |
||||
"url": "git+https://github.com/willin/hexo-wordcount.git" |
||||
}, |
||||
"homepage": "https://github.com/willin/hexo-wordcount", |
||||
"keywords": [ |
||||
"hexo", |
||||
"wordcount", |
||||
"count" |
||||
], |
||||
"author": { |
||||
"name": "Willin Wang", |
||||
"email": "willin@willin.org", |
||||
"url": "http://willin.wang" |
||||
}, |
||||
"license": "MIT" |
||||
} |
Loading…
Reference in new issue