Ghost/ghost/admin/app/helpers/gh-count-down-characters.js
Kevin Ansfield b1c94c6397 🐛 correctly count multibyte chars in character counters (#487)
closes https://github.com/TryGhost/Ghost/issues/7739

- use es6's `Array.from` to convert the string to an array to get a symbol count rather than a byte count
2017-01-11 14:45:22 +01:00

32 lines
772 B
JavaScript

import {helper} from 'ember-helper';
import {htmlSafe} from 'ember-string';
export function countDownCharacters(params) {
if (!params || params.length < 2) {
return;
}
let el = document.createElement('span');
let [content, maxCharacters] = params;
// convert to array so that we get accurate symbol counts for multibyte chars
// this will still count emoji+modifer as two chars
let {length} = Array.from(content || '');
el.className = 'word-count';
if (length > maxCharacters) {
el.style.color = '#E25440';
} else {
el.style.color = '#9FBB58';
}
el.innerHTML = length;
return htmlSafe(el.outerHTML);
}
export default helper(function (params) {
return countDownCharacters(params);
});