mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-07-23 23:29:41 +02:00
34 lines
792 B
JavaScript
34 lines
792 B
JavaScript
|
const translateString = require('./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
|
|||
|
*/
|
|||
|
module.exports = function urlify(string) {
|
|||
|
// 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;
|
|||
|
};
|