Updated to use count words/images helper from SDK (#1159)

refs https://github.com/TryGhost/Ghost/issues/10618

- Updated use of `countWords`, `countImages` and `stripTags` helper from Ghost SDK
- Removed existing local utils copy
This commit is contained in:
Rishabh Garg 2019-04-16 12:50:59 +05:30 committed by GitHub
parent 151b7a2844
commit bf6c446f72
11 changed files with 38 additions and 68 deletions

View File

@ -1,14 +1,15 @@
import Component from '@ember/component';
import Ember from 'ember';
import countWords from '../utils/count-words';
import layout from '../templates/components/koenig-card-code';
import {computed} from '@ember/object';
import {utils as ghostHelperUtils} from '@tryghost/helpers';
import {htmlSafe} from '@ember/string';
import {isBlank} from '@ember/utils';
import {run} from '@ember/runloop';
import {set} from '@ember/object';
const {Handlebars} = Ember;
const {countWords} = ghostHelperUtils;
export default Component.extend({
layout,

View File

@ -1,15 +1,17 @@
/* global noframe */
import Component from '@ember/component';
import countWords, {stripTags} from '../utils/count-words';
import layout from '../templates/components/koenig-card-embed';
import {NO_CURSOR_MOVEMENT} from './koenig-editor';
import {computed} from '@ember/object';
import {utils as ghostHelperUtils} from '@tryghost/helpers';
import {isBlank} from '@ember/utils';
import {run} from '@ember/runloop';
import {inject as service} from '@ember/service';
import {set} from '@ember/object';
import {task} from 'ember-concurrency';
const {countWords} = ghostHelperUtils;
export default Component.extend({
ajax: service(),
ghostPaths: service(),
@ -38,7 +40,7 @@ export default Component.extend({
counts: computed('payload.{html,caption}', function () {
return {
imageCount: this.payload.html ? 1 : 0,
wordCount: countWords(stripTags(this.payload.caption))
wordCount: countWords(this.payload.caption)
};
}),

View File

@ -1,12 +1,12 @@
import $ from 'jquery';
import Component from '@ember/component';
import EmberObject, {computed, set} from '@ember/object';
import countWords, {stripTags} from '../utils/count-words';
import layout from '../templates/components/koenig-card-gallery';
import {
IMAGE_EXTENSIONS,
IMAGE_MIME_TYPES
} from 'ghost-admin/components/gh-image-uploader';
import {utils as ghostHelperUtils} from '@tryghost/helpers';
import {htmlSafe} from '@ember/string';
import {isEmpty} from '@ember/utils';
import {run} from '@ember/runloop';
@ -14,6 +14,7 @@ import {inject as service} from '@ember/service';
const MAX_IMAGES = 9;
const MAX_PER_ROW = 3;
const {countWords} = ghostHelperUtils;
export default Component.extend({
koenigDragDropHandler: service(),
@ -51,7 +52,7 @@ export default Component.extend({
let imageCount = this.payload.images.length;
if (this.payload.caption) {
wordCount += countWords(stripTags(this.payload.caption));
wordCount += countWords(this.payload.caption);
}
return {wordCount, imageCount};

View File

@ -1,11 +1,13 @@
import Component from '@ember/component';
import countWords, {countImages, stripTags} from '../utils/count-words';
import layout from '../templates/components/koenig-card-html';
import {computed} from '@ember/object';
import {utils as ghostHelperUtils} from '@tryghost/helpers';
import {isBlank} from '@ember/utils';
import {run} from '@ember/runloop';
import {set} from '@ember/object';
const {countWords, countImages} = ghostHelperUtils;
export default Component.extend({
layout,
@ -25,7 +27,7 @@ export default Component.extend({
counts: computed('payload.html', function () {
return {
wordCount: countWords(stripTags(this.payload.html)),
wordCount: countWords(this.payload.html),
imageCount: countImages(this.payload.html)
};
}),

View File

@ -1,17 +1,19 @@
import $ from 'jquery';
import Component from '@ember/component';
import countWords, {stripTags} from '../utils/count-words';
import layout from '../templates/components/koenig-card-image';
import {
IMAGE_EXTENSIONS,
IMAGE_MIME_TYPES
} from 'ghost-admin/components/gh-image-uploader';
import {computed, set, setProperties} from '@ember/object';
import {utils as ghostHelperUtils} from '@tryghost/helpers';
import {htmlSafe} from '@ember/string';
import {isEmpty} from '@ember/utils';
import {run} from '@ember/runloop';
import {inject as service} from '@ember/service';
const {countWords} = ghostHelperUtils;
export default Component.extend({
ui: service(),
layout,
@ -57,7 +59,7 @@ export default Component.extend({
}
if (this.payload.caption) {
wordCount += countWords(stripTags(this.payload.caption));
wordCount += countWords(this.payload.caption);
}
return {wordCount, imageCount};

View File

@ -1,14 +1,15 @@
import Component from '@ember/component';
import countWords, {countImages, stripTags} from '../utils/count-words';
import formatMarkdown from 'ghost-admin/utils/format-markdown';
import layout from '../templates/components/koenig-card-markdown';
import {computed} from '@ember/object';
import {utils as ghostHelperUtils} from '@tryghost/helpers';
import {htmlSafe} from '@ember/string';
import {isBlank} from '@ember/utils';
import {run} from '@ember/runloop';
import {set} from '@ember/object';
import {task, timeout} from 'ember-concurrency';
const {countWords, countImages} = ghostHelperUtils;
const MIN_HEIGHT = 130;
export default Component.extend({
@ -34,7 +35,7 @@ export default Component.extend({
counts: computed('renderedMarkdown', function () {
return {
wordCount: countWords(stripTags(this.renderedMarkdown)),
wordCount: countWords(this.renderedMarkdown),
imageCount: countImages(this.renderedMarkdown)
};
}),

View File

@ -9,7 +9,6 @@ import EmberObject, {computed, get} from '@ember/object';
import Key from 'mobiledoc-kit/utils/key';
import MobiledocRange from 'mobiledoc-kit/utils/cursor/range';
import calculateReadingTime from '../utils/reading-time';
import countWords from '../utils/count-words';
import defaultAtoms from '../options/atoms';
import defaultCards, {CARD_COMPONENT_MAP, CARD_ICON_MAP} from '../options/cards';
import formatMarkdown from 'ghost-admin/utils/format-markdown';
@ -26,12 +25,14 @@ import {getContentFromPasteEvent} from 'mobiledoc-kit/utils/parse-utils';
import {getLinkMarkupFromRange} from '../utils/markup-utils';
import {getOwner} from '@ember/application';
import {getParent} from '../lib/dnd/utils';
import {utils as ghostHelperUtils} from '@tryghost/helpers';
import {guidFor} from '@ember/object/internals';
import {isBlank} from '@ember/utils';
import {run} from '@ember/runloop';
import {inject as service} from '@ember/service';
import {svgJar} from 'ghost-admin/helpers/svg-jar';
const {countWords} = ghostHelperUtils;
const UNDO_DEPTH = 100;
export const ADD_CARD_HOOK = 'addComponent';

View File

@ -1,51 +0,0 @@
/**
* Word count Utility
* @param {string} html
* @returns {integer} word count
* @description Takes a string and returns the number of words
* 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. It's the same code as used in
* Ghost's {{reading_time}} helper
**/
export default function countWords(text = '') {
// protect against Handlebars.SafeString
if (text.hasOwnProperty('string')) {
text = text.string;
}
let pattern = /[a-zA-ZÀ-ÿ0-9_\u0392-\u03c9\u0410-\u04F9]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af]+/g;
let match = text.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;
}
export function countImages(html = '') {
// protect against Handlebars.SafeString
if (html.hasOwnProperty('string')) {
html = html.string;
}
return (html.match(/<img(.|\n)*?>/g) || []).length;
}
export function stripTags(html = '') {
// protect against Handlebars.SafeString
if (html.hasOwnProperty('string')) {
html = html.string;
}
return html.replace(/<(.|\n)*?>/g, ' ');
}

View File

@ -12,6 +12,8 @@
],
"dependencies": {
"ember-cli-babel": "^7.5.0",
"ember-cli-htmlbars": "^3.0.1"
"ember-cli-htmlbars": "^3.0.1",
"@tryghost/helpers": "^1.1.1",
"ember-auto-import": "^1.2.21"
}
}

View File

@ -29,6 +29,7 @@
"@ember/jquery": "0.6.0",
"@ember/optional-features": "0.7.0",
"@html-next/vertical-collection": "1.0.0-beta.13",
"@tryghost/helpers": "^1.1.1",
"@tryghost/mobiledoc-kit": "0.11.1-ghost.7",
"@tryghost/timezone-data": "0.2.0",
"autoprefixer": "9.5.1",

View File

@ -818,6 +818,13 @@
resolved "https://registry.yarnpkg.com/@sinonjs/text-encoding/-/text-encoding-0.7.1.tgz#8da5c6530915653f3a1f38fd5f101d8c3f8079c5"
integrity sha512-+iTbntw2IZPb/anVDbypzfQa+ay64MW0Zo8aJ8gZPWMMK6/OubMVb6lUPMagqjOPnmtauXnFCACVl3O7ogjeqQ==
"@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/mobiledoc-kit@0.11.1-ghost.7":
version "0.11.1-ghost.7"
resolved "https://registry.yarnpkg.com/@tryghost/mobiledoc-kit/-/mobiledoc-kit-0.11.1-ghost.7.tgz#79b9ef018dc8c8829864e30602079ea7f4ef3cb6"
@ -4983,7 +4990,6 @@ ember-lifeline@^3.0.1:
"ember-light-table@https://github.com/kevinansfield/ember-light-table#bump-ember-in-viewport":
version "1.13.2"
uid a757399504755ad8dde5cb4196576201881b93d4
resolved "https://github.com/kevinansfield/ember-light-table#a757399504755ad8dde5cb4196576201881b93d4"
dependencies:
"@html-next/vertical-collection" "^1.0.0-beta.12"
@ -6467,7 +6473,6 @@ gonzales-pe@^3.4.7:
"google-caja-bower@https://github.com/acburdine/google-caja-bower#ghost":
version "6011.0.0"
uid "275cb75249f038492094a499756a73719ae071fd"
resolved "https://github.com/acburdine/google-caja-bower#275cb75249f038492094a499756a73719ae071fd"
got@^8.0.1:
@ -7590,7 +7595,6 @@ just-extend@^4.0.2:
"keymaster@https://github.com/madrobby/keymaster.git":
version "1.6.3"
uid f8f43ddafad663b505dc0908e72853bcf8daea49
resolved "https://github.com/madrobby/keymaster.git#f8f43ddafad663b505dc0908e72853bcf8daea49"
keyv@3.0.0:
@ -7755,6 +7759,11 @@ locate-path@^3.0.0:
p-locate "^3.0.0"
path-exists "^3.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._baseassign@^3.0.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/lodash._baseassign/-/lodash._baseassign-3.2.0.tgz#8c38a099500f215ad09e59f1722fd0c52bfe0a4e"
@ -10759,7 +10768,6 @@ simple-swizzle@^0.2.2:
"simplemde@https://github.com/kevinansfield/simplemde-markdown-editor.git#ghost":
version "1.11.2"
uid "7afb50dcdd63d3a1f6ccad682b70acbd7bc52eb9"
resolved "https://github.com/kevinansfield/simplemde-markdown-editor.git#7afb50dcdd63d3a1f6ccad682b70acbd7bc52eb9"
sinon@^7.1.1: