2020-05-06 10:30:15 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { autop } from '@wordpress/autop';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove HTML tags from a string.
|
|
|
|
*
|
|
|
|
* @param {string} htmlString String to remove tags from.
|
|
|
|
* @return {string} Plain text string.
|
|
|
|
*/
|
2023-06-07 09:29:15 +00:00
|
|
|
export const removeTags = ( htmlString: string ) => {
|
2020-05-06 10:30:15 +00:00
|
|
|
const tagsRegExp = /<\/?[a-z][^>]*?>/gi;
|
|
|
|
return htmlString.replace( tagsRegExp, '' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove trailing punctuation and append some characters to a string.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {string} text Text to append to.
|
|
|
|
* @param {string} moreText Text to append.
|
2020-05-06 10:30:15 +00:00
|
|
|
* @return {string} String with appended characters.
|
|
|
|
*/
|
2023-06-07 09:29:15 +00:00
|
|
|
export const appendMoreText = ( text: string, moreText: string ) => {
|
2020-05-06 10:30:15 +00:00
|
|
|
return text.replace( /[\s|\.\,]+$/i, '' ) + moreText;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Limit words in string and returned trimmed version.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {string} text Text to trim.
|
2020-05-06 10:30:15 +00:00
|
|
|
* @param {number} maxLength Number of countType to limit to.
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {string} moreText Appended to the trimmed string.
|
2023-06-07 09:29:15 +00:00
|
|
|
* @param {string} useAutop Whether to format with autop before returning.
|
2020-05-06 10:30:15 +00:00
|
|
|
* @return {string} Trimmed string.
|
|
|
|
*/
|
2023-06-07 09:29:15 +00:00
|
|
|
export const trimWords = (
|
|
|
|
text: string,
|
|
|
|
maxLength: number,
|
|
|
|
moreText = '…',
|
|
|
|
useAutop = true
|
|
|
|
) => {
|
2020-05-06 10:30:15 +00:00
|
|
|
const textToTrim = removeTags( text );
|
|
|
|
const trimmedText = textToTrim
|
|
|
|
.split( ' ' )
|
|
|
|
.splice( 0, maxLength )
|
|
|
|
.join( ' ' );
|
|
|
|
|
2023-06-07 09:29:15 +00:00
|
|
|
if ( trimmedText === textToTrim ) {
|
|
|
|
return useAutop ? autop( textToTrim ) : textToTrim;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! useAutop ) {
|
|
|
|
return appendMoreText( trimmedText, moreText );
|
|
|
|
}
|
|
|
|
|
2020-05-06 10:30:15 +00:00
|
|
|
return autop( appendMoreText( trimmedText, moreText ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Limit characters in string and returned trimmed version.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {string} text Text to trim.
|
|
|
|
* @param {number} maxLength Number of countType to limit to.
|
2020-05-06 10:30:15 +00:00
|
|
|
* @param {boolean} includeSpaces Should spaces be included in the count.
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {string} moreText Appended to the trimmed string.
|
2023-06-07 09:29:15 +00:00
|
|
|
* @param {string} useAutop Whether to format with autop before returning.
|
2020-05-06 10:30:15 +00:00
|
|
|
* @return {string} Trimmed string.
|
|
|
|
*/
|
2023-06-07 09:29:15 +00:00
|
|
|
export const trimCharacters = (
|
|
|
|
text: string,
|
|
|
|
maxLength: number,
|
2020-05-06 10:30:15 +00:00
|
|
|
includeSpaces = true,
|
2023-06-07 09:29:15 +00:00
|
|
|
moreText = '…',
|
|
|
|
useAutop = true
|
2020-05-06 10:30:15 +00:00
|
|
|
) => {
|
|
|
|
const textToTrim = removeTags( text );
|
|
|
|
const trimmedText = textToTrim.slice( 0, maxLength );
|
|
|
|
|
2023-06-07 09:29:15 +00:00
|
|
|
if ( trimmedText === textToTrim ) {
|
|
|
|
return useAutop ? autop( textToTrim ) : textToTrim;
|
|
|
|
}
|
|
|
|
|
2020-05-06 10:30:15 +00:00
|
|
|
if ( includeSpaces ) {
|
|
|
|
return autop( appendMoreText( trimmedText, moreText ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
const matchSpaces = trimmedText.match( /([\s]+)/g );
|
|
|
|
const spaceCount = matchSpaces ? matchSpaces.length : 0;
|
|
|
|
const trimmedTextExcludingSpaces = textToTrim.slice(
|
|
|
|
0,
|
|
|
|
maxLength + spaceCount
|
|
|
|
);
|
|
|
|
|
2023-06-07 09:29:15 +00:00
|
|
|
if ( ! useAutop ) {
|
|
|
|
return appendMoreText( trimmedTextExcludingSpaces, moreText );
|
|
|
|
}
|
|
|
|
|
2020-05-06 10:30:15 +00:00
|
|
|
return autop( appendMoreText( trimmedTextExcludingSpaces, moreText ) );
|
|
|
|
};
|