diff --git a/core/server/helpers/reading_time.js b/core/server/helpers/reading_time.js index f4ede3c564..aaba3e3a80 100644 --- a/core/server/helpers/reading_time.js +++ b/core/server/helpers/reading_time.js @@ -10,11 +10,12 @@ // // Returns estimated reading time for post -var proxy = require('./proxy'), - _ = require('lodash'), - schema = require('../data/schema').checks, - SafeString = proxy.SafeString, - localUtils = proxy.localUtils; +const proxy = require('./proxy'); +const _ = require('lodash'); +const schema = require('../data/schema').checks; +const SafeString = proxy.SafeString; +const countImages = require('@tryghost/helpers').utils.countImages; +const countWords = require('@tryghost/helpers').utils.countWords; module.exports = function reading_time(options) {// eslint-disable-line camelcase options = options || {}; @@ -38,8 +39,8 @@ module.exports = function reading_time(options) {// eslint-disable-line camelcas html = this.html; imageCount = this.feature_image ? 1 : 0; - imageCount += localUtils.imageCount(html); - wordCount = localUtils.wordCount(html); + imageCount += countImages(html); + wordCount = countWords(html); readingTimeSeconds = wordCount / wordsPerSecond; for (var i = 12; i > 12 - imageCount; i -= 1) { diff --git a/core/server/helpers/utils.js b/core/server/helpers/utils.js index fc4c6fd14b..e2b0201805 100644 --- a/core/server/helpers/utils.js +++ b/core/server/helpers/utils.js @@ -1,45 +1,5 @@ const _ = require('lodash'); -/** - * Word count Utility - * @param {string} html - * @returns {integer} word count - * @description Takes an HTML string and returns the number of words after sanitizing the html - * This code is taken from https://github.com/sparksuite/simplemde-markdown-editor/blob/6abda7ab68cc20f4aca870eb243747951b90ab04/src/js/simplemde.js#L1054-L1067 - * with extra diacritics character matching. - **/ -module.exports.wordCount = function wordCount(html) { - if (!html) { - return 0; - } - html = html.replace(/<(.|\n)*?>/g, ' '); // strip tags - - const pattern = /[a-zA-ZÀ-ÿ0-9_\u0392-\u03c9\u0410-\u04F9]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af]+/g; - const match = html.match(pattern); - let count = 0; - if (match === null) { - return count; - } - for (var i = 0; i < match.length; i += 1) { - if (match[i].charCodeAt(0) >= 0x4E00) { - count += match[i].length; - } else { - count += 1; - } - } - return count; -}; - -/** - * Image count Utility - * @param {string} html - * @returns {integer} image count - * @description Takes an HTML string and returns the number of images - **/ -module.exports.imageCount = function wordCount(html) { - return html ? (html.match(//g) || []).length : 0; -}; - module.exports.findKey = function findKey(key /* ...objects... */) { let objects = Array.prototype.slice.call(arguments, 1); diff --git a/core/test/unit/helpers/utils_spec.js b/core/test/unit/helpers/utils_spec.js index 6842835f95..81a61c2746 100644 --- a/core/test/unit/helpers/utils_spec.js +++ b/core/test/unit/helpers/utils_spec.js @@ -1,39 +1,39 @@ -var should = require('should'), - helperUtils = require('../../../server/helpers/utils'); +const should = require('should'); +const helperUtils = require('@tryghost/helpers').utils; describe('Helpers Utils', function () { describe('Word Count', function () { it('[success] can count words', function () { var html = 'Some words here', - result = helperUtils.wordCount(html); + result = helperUtils.countWords(html); result.should.equal(3); }); it('[success] sanitized HTML tags', function () { var html = '

This is a text example! Count me in ;)

', - result = helperUtils.wordCount(html); + result = helperUtils.countWords(html); result.should.equal(8); }); it('[success] sanitized non alpha-numeric characters', function () { var html = '

This is a text example! I love Döner. Especially number 875.

', - result = helperUtils.wordCount(html); + result = helperUtils.countWords(html); result.should.equal(11); }); it('[success] counted Chinese characters', function () { var html = '

我今天在家吃了好多好多好吃的,现在的我非常开心非常满足

', - result = helperUtils.wordCount(html); + result = helperUtils.countWords(html); result.should.equal(26); }); it('[success] sanitized white space correctly', function () { var html = '

This is a text example!\n Count me in ;)

', - result = helperUtils.wordCount(html); + result = helperUtils.countWords(html); result.should.equal(8); }); @@ -42,7 +42,7 @@ describe('Helpers Utils', function () { describe('Image Count', function () { it('[success] can count images', function () { var html = '

This is a text example! Count me in ;)

', - result = helperUtils.imageCount(html); + result = helperUtils.countImages(html); result.should.equal(2); }); diff --git a/package.json b/package.json index 9f975aabce..daae43c832 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ }, "dependencies": { "@nexes/nql": "0.2.1", + "@tryghost/helpers": "1.1.1", "ajv": "6.8.1", "amperize": "0.3.8", "analytics-node": "3.3.0", diff --git a/yarn.lock b/yarn.lock index 19f6342468..596e510cbd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -91,6 +91,13 @@ mkdirp "0.5.0" yauzl "2.4.1" +"@tryghost/helpers@1.1.1": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@tryghost/helpers/-/helpers-1.1.1.tgz#d242c44bd5cdf01f39acb929bf9dd345d4ede7b5" + integrity sha512-AzFYznuesabUREQhu3VEeotn44QNdRUWcHCWhJD4ktvqS49hfDyTELl4JCpdtBhiflaGXtCD9vykUlr/nFjM8w== + dependencies: + lodash-es "^4.17.11" + "@tryghost/html-to-mobiledoc@0.2.4": version "0.2.4" resolved "https://registry.yarnpkg.com/@tryghost/html-to-mobiledoc/-/html-to-mobiledoc-0.2.4.tgz#cafbb673a52ae9539ae29e491252dcee46a4ee1a" @@ -3673,6 +3680,11 @@ load-json-file@^1.0.0: pinkie-promise "^2.0.0" strip-bom "^2.0.0" +lodash-es@^4.17.11: + version "4.17.11" + resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.11.tgz#145ab4a7ac5c5e52a3531fb4f310255a152b4be0" + integrity sha512-DHb1ub+rMjjrxqlB3H56/6MXtm1lSksDp2rA2cNWjG8mlDUYFhUj3Di2Zn5IwSU87xLv8tNIQ7sSwE/YOX/D/Q== + lodash.assign@^4.2.0: version "4.2.0" resolved "https://registry.yarnpkg.com/lodash.assign/-/lodash.assign-4.2.0.tgz#0d99f3ccd7a6d261d19bdaeb9245005d285808e7"