2015-07-15 14:00:11 +09:30
|
|
|
/**
|
|
|
|
* Truncate a string to the given length, appending ellipses if necessary.
|
|
|
|
*
|
|
|
|
* @param {String} string
|
|
|
|
* @param {Number} length
|
|
|
|
* @param {Number} [start=0]
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
export function truncate(string, length, start = 0) {
|
2020-04-17 11:57:55 +02:00
|
|
|
return (start > 0 ? '...' : '') + string.substring(start, start + length) + (string.length > start + length ? '...' : '');
|
2015-07-15 14:00:11 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a slug out of the given string. Non-alphanumeric characters are
|
|
|
|
* converted to hyphens.
|
|
|
|
*
|
2020-01-24 17:42:14 +01:00
|
|
|
* NOTE: This method does not use the comparably sophisticated transliteration
|
|
|
|
* mechanism that is employed in the backend. Therefore, it should only be used
|
|
|
|
* to *suggest* slugs that can be overridden by the user.
|
|
|
|
*
|
2015-07-15 14:00:11 +09:30
|
|
|
* @param {String} string
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
export function slug(string) {
|
2020-04-17 11:57:55 +02:00
|
|
|
return string
|
|
|
|
.toLowerCase()
|
2015-07-15 14:00:11 +09:30
|
|
|
.replace(/[^a-z0-9]/gi, '-')
|
|
|
|
.replace(/-+/g, '-')
|
2018-02-08 20:52:50 +00:00
|
|
|
.replace(/-$|^-/g, '');
|
2015-07-15 14:00:11 +09:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strip HTML tags and quotes out of the given string, replacing them with
|
|
|
|
* meaningful punctuation.
|
|
|
|
*
|
|
|
|
* @param {String} string
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
export function getPlainContent(string) {
|
2020-04-17 11:57:55 +02:00
|
|
|
const html = string.replace(/(<\/p>|<br>)/g, '$1 ').replace(/<img\b[^>]*>/gi, ' ');
|
2016-05-27 07:34:31 +09:30
|
|
|
|
|
|
|
const dom = $('<div/>').html(html);
|
2015-08-06 13:28:26 +09:30
|
|
|
|
|
|
|
dom.find(getPlainContent.removeSelectors.join(',')).remove();
|
|
|
|
|
2016-05-27 07:34:31 +09:30
|
|
|
return dom.text().replace(/\s+/g, ' ').trim();
|
2015-05-06 08:32:33 +09:30
|
|
|
}
|
2015-07-20 18:12:08 +09:30
|
|
|
|
2015-08-31 10:49:24 +09:30
|
|
|
/**
|
|
|
|
* An array of DOM selectors to remove when getting plain content.
|
|
|
|
*
|
|
|
|
* @type {Array}
|
|
|
|
*/
|
|
|
|
getPlainContent.removeSelectors = ['blockquote', 'script'];
|
2015-08-06 13:28:26 +09:30
|
|
|
|
2015-07-20 18:12:08 +09:30
|
|
|
/**
|
|
|
|
* Make a string's first character uppercase.
|
|
|
|
*
|
|
|
|
* @param {String} string
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
export function ucfirst(string) {
|
|
|
|
return string.substr(0, 1).toUpperCase() + string.substr(1);
|
|
|
|
}
|