From 316814c80848e5b7ea1e8772d14d3f5c23cdb94f Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 14 Mar 2024 06:38:35 -0300 Subject: util: add much more custom behavior to getKebabCase --- src/util/wiki-data.js | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'src/util/wiki-data.js') diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js index 1066874..f8ab3ef 100644 --- a/src/util/wiki-data.js +++ b/src/util/wiki-data.js @@ -13,12 +13,45 @@ export {filterMultipleArrays} from './sugar.js'; export function getKebabCase(name) { return name + + // Spaces to dashes .split(' ') .join('-') - .replace(/&/g, 'and') - .replace(/[^a-zA-Z0-9-]/g, '') + + // Punctuation as words + .replace(/&/g, '-and-') + .replace(/\+/g, '-plus-') + .replace(/%/g, '-percent-') + + // Punctuation which only divides words, not single characters + .replace(/(\b[^\s-.]{2,})\./g, '$1-') + .replace(/\.([^\s-.]{2,})\b/g, '-$1') + + // Punctuation which doesn't divide a number following a non-number + .replace(/(?<=[0-9])\^/g, '-') + .replace(/\^(?![0-9])/g, '-') + + // General punctuation which always separates surrounding words + .replace(/[/@#$%*()_=,[\]{}|\\;:<>?`~]/g, '-') + + // Accented characters + .replace(/[áâäàå]/gi, 'a') + .replace(/[çč]/gi, 'c') + .replace(/[éêëè]/gi, 'e') + .replace(/[íîïì]/gi, 'i') + .replace(/[óôöò]/gi, 'o') + .replace(/[úûüù]/gi, 'u') + + // Strip other characters + .replace(/[^a-z0-9-]/gi, '') + + // Combine consecutive dashes .replace(/-{2,}/g, '-') + + // Trim dashes on boundaries .replace(/^-+|-+$/g, '') + + // Always lowercase .toLowerCase(); } -- cgit 1.3.0-6-gf8a5