0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/core/server/helpers/reading_time.js
Katharina Irrgang 4f35f86713
Moved word-count utility to helpers/utils.js (#9328)
refs #9178

- each package/module has a local utility (e.g. api, helpers, adapters)
- these are very small utility functions which are only used from this package
- they don't belong into the global lib/utils
2017-12-13 14:05:53 +01:00

41 lines
1.1 KiB
JavaScript

// # Reading Time Helper
//
// Usage: `{{reading_time}}`
//
// Returns estimated reading time for post
var proxy = require('./proxy'),
schema = require('../data/schema').checks,
SafeString = proxy.SafeString,
localUtils = proxy.localUtils;
module.exports = function reading_time() {// eslint-disable-line camelcase
var html,
wordsPerMinute = 275,
wordsPerSecond = wordsPerMinute / 60,
wordCount,
imageCount,
readingTimeSeconds,
readingTime;
// only calculate reading time for posts
if (!schema.isPost(this)) {
return null;
}
html = this.html;
imageCount = this.feature_image ? 1 : 0;
wordCount = localUtils.wordCount(html);
readingTimeSeconds = wordCount / wordsPerSecond;
// add 12 seconds to reading time if feature image is present
readingTimeSeconds = imageCount ? readingTimeSeconds + 12 : readingTimeSeconds;
if (readingTimeSeconds < 60) {
readingTime = '< 1 min read';
} else {
readingTime = `${Math.round(readingTimeSeconds / 60)} min read`;
}
return new SafeString(readingTime);
};