1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-07-23 15:19:41 +02:00
codex.docs/src/backend/utils/urlify.ts

34 lines
800 B
TypeScript
Raw Normal View History

import translateString from './translation';
/**
* 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
*/
export default function urlify(string: string): string {
// strip tags
string = string.replace(/(<([^>]+)>)/ig, '');
// remove nbsp
string = string.replace(/&nbsp;/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;
}