2022-03-05 22:57:23 +04:00
|
|
|
|
import translateString from './translation';
|
2019-03-13 12:25:43 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert text to URL-like string
|
|
|
|
|
* Example: "What is <mark>clean data</mark>" -> "what-is-clean-data"
|
|
|
|
|
*
|
|
|
|
|
* @param {string} string - source string with HTML
|
|
|
|
|
* @returns {string} alias-like string
|
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
|
export default function urlify(string: string): string {
|
2019-03-13 12:25:43 +03:00
|
|
|
|
// strip tags
|
|
|
|
|
string = string.replace(/(<([^>]+)>)/ig, '');
|
|
|
|
|
|
|
|
|
|
// remove nbsp
|
|
|
|
|
string = string.replace(/ /g, ' ');
|
|
|
|
|
|
|
|
|
|
// remove all symbols except chars
|
|
|
|
|
string = string.replace(/[^a-zA-Z0-9А-Яа-яЁё ]/g, ' ');
|
|
|
|
|
|
|
|
|
|
// remove whitespaces
|
|
|
|
|
string = string.replace(/ +/g, ' ').trim();
|
|
|
|
|
|
|
|
|
|
// lowercase
|
|
|
|
|
string = string.toLowerCase();
|
|
|
|
|
|
|
|
|
|
// join words with hyphens
|
|
|
|
|
string = string.split(' ').join('-');
|
|
|
|
|
|
|
|
|
|
// translate
|
|
|
|
|
string = translateString(string);
|
|
|
|
|
|
|
|
|
|
return string;
|
2022-03-05 22:57:23 +04:00
|
|
|
|
}
|