├── .gitignore
├── .nvmrc
├── README.md
├── index.js
├── lib
└── reading-time.js
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules/
2 | npm-debug.log
3 | .sass-cache/
4 | .DS_Store
5 | build/
6 |
--------------------------------------------------------------------------------
/.nvmrc:
--------------------------------------------------------------------------------
1 | v10.0.0
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Reading time plugin for Eleventy
2 |
3 | A non-dependency reading time plugin for [Eleventy](https://www.11ty.io/) static site generator. This plugin provides a template tag which prints the number of minutes or seconds required to read the given text.
4 |
5 | ## Install
6 |
7 | ```shell
8 | npm install --save eleventy-plugin-reading-time
9 | ```
10 |
11 | ## Usage
12 |
13 | In your Eleventy config file (defaults to `.eleventy.js`):
14 |
15 | ```js
16 | const readingTime = require('eleventy-plugin-reading-time');
17 |
18 | module.exports = (eleventyConfig) => {
19 | eleventyConfig.addPlugin(readingTime);
20 | };
21 | ```
22 |
23 | Now you can use the `readingTime` filter in your Nunjuck templates:
24 |
25 | ```html
26 | About {{ someTextContent | readingTime } reading time}
27 | ```
28 |
29 | prints
30 |
31 | ```html
32 | About 6 min reading time
33 | ```
34 |
35 | Example `post.njk` template:
36 |
37 | ```html
38 |
39 |
45 |
46 |
47 | {{ content | safe }}
48 |
49 |
50 | ```
51 |
52 | If you're in a collection loop, this filter accepts a collection object too:
53 |
54 | ```html
55 | {% for post in posts %}
56 |
57 | {{post.title}}
58 | About {{ post | readingTime }} reading time.
59 |
60 | {% endfor %}
61 | ```
62 |
63 | ## To Do
64 |
65 | - [ ] Support more template engines.
66 |
67 | ## License
68 |
69 | MIT.
70 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | const readingTime = require('./lib/reading-time');
2 |
3 | module.exports = function(eleventyConfig, pluginNamespace) {
4 | eleventyConfig.namespace(pluginNamespace, () => {
5 | eleventyConfig.addNunjucksFilter('readingTime', readingTime);
6 | });
7 | };
8 |
--------------------------------------------------------------------------------
/lib/reading-time.js:
--------------------------------------------------------------------------------
1 | module.exports = function readingTime(
2 | postOrContent,
3 | { printSeconds = false, raw = false, speed = 300 } = {}
4 | ) {
5 | const htmlContent =
6 | typeof postOrContent === 'string'
7 | ? postOrContent
8 | : postOrContent.templateContent;
9 |
10 | if (!htmlContent) {
11 | return `0 ${printSeconds ? 'seconds' : 'minutes'}`;
12 | }
13 |
14 | const content = htmlContent.replace(/(<([^>]+)>)/gi, '');
15 | const matches = content.match(/[\u0400-\u04FF]+|\S+\s*/g);
16 | const count = matches !== null ? matches.length : 0;
17 |
18 | let est = '';
19 |
20 | if (printSeconds === true) {
21 | const min = Math.floor(count / speed);
22 | const sec = Math.floor((count % speed) / (speed / 60));
23 |
24 | if (raw === false) {
25 | const mins = min + ' minute' + (min === 1 ? '' : 's') + ', ';
26 | const secs = sec + ' second' + (sec === 1 ? '' : 's');
27 | est = min > 0 ? mins + secs : secs;
28 | } else {
29 | est = min * 60 + sec;
30 | }
31 | } else {
32 | const min = Math.ceil(count / speed);
33 |
34 | if (raw === false) {
35 | est = min + ' min';
36 | } else {
37 | est = min;
38 | }
39 | }
40 |
41 | return est;
42 | };
43 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "eleventy-plugin-reading-time",
3 | "version": "0.0.1",
4 | "description": "Add the reading time for a text as template tag.",
5 | "main": "index.js",
6 | "scripts": {
7 | "lint": "eslint .",
8 | "test": "npm run lint"
9 | },
10 | "repository": {
11 | "type": "git",
12 | "url": "git+https://github.com/johanbrook/eleventy-plugin-reading-time.git"
13 | },
14 | "keywords": [
15 | "eleventy",
16 | "plugin",
17 | "reading",
18 | "time",
19 | "word",
20 | "count",
21 | "11ty"
22 | ],
23 | "author": "Johan Brook ",
24 | "license": "MIT",
25 | "bugs": {
26 | "url": "https://github.com/johanbrook/eleventy-plugin-reading-time/issues"
27 | },
28 | "homepage": "https://github.com/johanbrook/eleventy-plugin-reading-time#readme",
29 | "devDependencies": {
30 | "eslint": "^5.6.0"
31 | },
32 | "prettier": {
33 | "trailingComma": "es5",
34 | "singleQuote": true,
35 | "arrowParens": "always"
36 | },
37 | "eslintConfig": {
38 | "extends": "eslint:recommended",
39 | "env": {
40 | "commonjs": true,
41 | "node": true
42 | },
43 | "parserOptions": {
44 | "ecmaVersion": 6,
45 | "sourceType": "module"
46 | }
47 | }
48 | }
49 |
--------------------------------------------------------------------------------