2017-11-06 21:40:07 +03:00
|
|
|
// # Reading Time Helper
|
|
|
|
//
|
|
|
|
// Usage: `{{reading_time}}`
|
2018-01-09 16:50:57 +03:00
|
|
|
// or for translatable themes, with (t) translation helper's subexpressions:
|
|
|
|
// `{{reading_time seconds=(t "< 1 min read") minute=(t "1 min read") minutes=(t "% min read")}}`
|
|
|
|
// and in the theme translation file, for example Spanish es.json:
|
|
|
|
// "< 1 min read": "< 1 min de lectura",
|
|
|
|
// "1 min read": "1 min de lectura",
|
|
|
|
// "% min read": "% min de lectura",
|
2017-11-06 21:40:07 +03:00
|
|
|
//
|
|
|
|
// Returns estimated reading time for post
|
|
|
|
|
2021-09-28 17:06:33 +03:00
|
|
|
const {checks} = require('../services/proxy');
|
|
|
|
const {SafeString} = require('../services/rendering');
|
2021-09-26 23:01:13 +03:00
|
|
|
|
2021-09-28 17:06:33 +03:00
|
|
|
const {readingTime: calculateReadingTime} = require('@tryghost/helpers');
|
2017-11-06 21:40:07 +03:00
|
|
|
|
2018-01-09 16:50:57 +03:00
|
|
|
module.exports = function reading_time(options) {// eslint-disable-line camelcase
|
|
|
|
options = options || {};
|
|
|
|
options.hash = options.hash || {};
|
|
|
|
|
2017-11-06 21:40:07 +03:00
|
|
|
// only calculate reading time for posts
|
2020-03-30 23:23:02 +03:00
|
|
|
if (!checks.isPost(this)) {
|
2017-11-06 21:40:07 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-05-07 06:10:36 +03:00
|
|
|
let readingTime = calculateReadingTime(this, options.hash);
|
2017-11-06 21:40:07 +03:00
|
|
|
|
|
|
|
return new SafeString(readingTime);
|
|
|
|
};
|