diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2024-03-14 06:38:35 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2024-03-14 06:46:48 -0300 |
commit | 316814c80848e5b7ea1e8772d14d3f5c23cdb94f (patch) | |
tree | fc8eda957a1839213e9711b605b0c9af16cdacfb | |
parent | a26a913841bba73f44182bb5e1725f91573395a9 (diff) |
util: add much more custom behavior to getKebabCase
-rw-r--r-- | src/util/wiki-data.js | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js index 10668742..f8ab3ef3 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(); } |