Using common slugify.

This commit is contained in:
DebaucheryLibrarian 2026-01-09 01:54:10 +01:00
parent 7a452db2f8
commit e67ec5eca4
4 changed files with 36 additions and 76 deletions

2
common

@ -1 +1 @@
Subproject commit c45852d6937abe25e1205af0b9b96d49970901c8
Subproject commit ee23dc0358677a66b27a20b8d56c4b69782cec9b

32
src/tools/slugify-test.js Normal file
View File

@ -0,0 +1,32 @@
import slugify from '../utils/slugify.js';
function init() {
const cases = [
'Brave, New World',
'Jœrgenbahn Straße',
'Partêrre',
'Ápres ski.',
'very 😀 true 😃',
'a véééry long piece of text that should not result in a very long slug, even for $100',
'don\'t you, forget about me',
'Pneumonoultramicroscopicsilicovolcanoconiosis',
'this (old) spicemen[sic]',
'contact@example.com',
'!@#$%',
'',
' ',
['this is', '2026-01-01', 'an array', '', ' ', 'test'],
];
cases.forEach((item) => console.log(item, '-->', slugify(item, '-', { limit: 20 })));
cases.forEach((item) => console.log(item, '-->', slugify(item, '-', {
lower: true,
accents: false,
punctuation: false,
symbols: 'split',
limit: 50,
})));
}
init();

View File

@ -1,75 +1,3 @@
const substitutes = {
à: 'a',
á: 'a',
ä: 'a',
å: 'a',
ã: 'a',
æ: 'ae',
ç: 'c',
è: 'e',
é: 'e',
ë: 'e',
: 'e',
ì: 'i',
í: 'i',
ï: 'i',
ĩ: 'i',
ǹ: 'n',
ń: 'n',
ñ: 'n',
ò: 'o',
ó: 'o',
ö: 'o',
õ: 'o',
ø: 'o',
œ: 'oe',
ß: 'ss',
ù: 'u',
ú: 'u',
ü: 'u',
ũ: 'u',
: 'y',
ý: 'y',
ÿ: 'y',
: 'y',
};
import slugify from '../../common/slugify.cjs';
export default function slugify(strings, delimiter = '-', {
encode = false,
removeAccents = true,
removePunctuation = false,
limit = 1000,
} = {}) {
if (!strings || (typeof strings !== 'string' && !Array.isArray(strings))) {
return strings;
}
const slugComponents = []
.concat(strings)
.filter(Boolean)
.flatMap((string) => string
.trim()
.toLowerCase()
.replace(removePunctuation && /[.,:;'"_-]/g, '')
.match(/[A-Za-zÀ-ÖØ-öø-ÿ0-9]+/g));
if (!slugComponents) {
return '';
}
const slug = slugComponents.reduce((acc, component, index) => {
const accSlug = `${acc}${index > 0 ? delimiter : ''}${component}`;
if (accSlug.length < limit) {
if (removeAccents) {
return accSlug.replace(/[à-ÿ]/g, (match) => substitutes[match] || '');
}
return accSlug;
}
return acc;
}, '');
return encode ? encodeURI(slug) : slug;
}
export default slugify;

2
static

@ -1 +1 @@
Subproject commit c3a287bfae05a7bb92d028c4036ec418b7efe5c5
Subproject commit 50a262da611d6435a0bf77662f47d0a9126615f0