FEATURE optmizing a sanitize title function to match what wordpress sanitize title does
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
73a2c6a0c0
commit
24379c992c
|
|
@ -4,89 +4,23 @@ export function handleSmoothScrollToTitle(targetId: string): void {
|
||||||
|
|
||||||
targetElement.scrollIntoView({ behavior: 'smooth' });
|
targetElement.scrollIntoView({ behavior: 'smooth' });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fonction équivalente à sanitize_title() de WordPress
|
// Fonction équivalente à sanitize_title() de WordPress
|
||||||
function sanitizeTitle(title: string): string {
|
function sanitizeTitle(title: string): string {
|
||||||
// Convertir en minuscules
|
// Convertir en minuscules
|
||||||
let slug = title.toLowerCase();
|
let slug = title.toLowerCase();
|
||||||
|
|
||||||
// Remplacer les caractères spéciaux français et autres
|
// Normaliser les caractères (supprimer les accents) - comme WordPress
|
||||||
const replacements: { [key: string]: string } = {
|
slug = slug.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
|
||||||
à: 'a',
|
|
||||||
á: 'a',
|
|
||||||
â: 'a',
|
|
||||||
ã: 'a',
|
|
||||||
ä: 'a',
|
|
||||||
å: 'a',
|
|
||||||
è: 'e',
|
|
||||||
é: 'e',
|
|
||||||
ê: 'e',
|
|
||||||
ë: 'e',
|
|
||||||
ì: 'i',
|
|
||||||
í: 'i',
|
|
||||||
î: 'i',
|
|
||||||
ï: 'i',
|
|
||||||
ò: 'o',
|
|
||||||
ó: 'o',
|
|
||||||
ô: 'o',
|
|
||||||
õ: 'o',
|
|
||||||
ö: 'o',
|
|
||||||
ù: 'u',
|
|
||||||
ú: 'u',
|
|
||||||
û: 'u',
|
|
||||||
ü: 'u',
|
|
||||||
ý: 'y',
|
|
||||||
ÿ: 'y',
|
|
||||||
ñ: 'n',
|
|
||||||
ç: 'c',
|
|
||||||
œ: 'oe',
|
|
||||||
æ: 'ae',
|
|
||||||
'«': '',
|
|
||||||
'»': '',
|
|
||||||
'"': '',
|
|
||||||
'"': '',
|
|
||||||
'?': '',
|
|
||||||
'!': '',
|
|
||||||
':': '',
|
|
||||||
';': '',
|
|
||||||
'.': '',
|
|
||||||
',': '',
|
|
||||||
'(': '',
|
|
||||||
')': '',
|
|
||||||
'[': '',
|
|
||||||
']': '',
|
|
||||||
'{': '',
|
|
||||||
'}': '',
|
|
||||||
'/': '-',
|
|
||||||
'\\': '-',
|
|
||||||
'|': '-',
|
|
||||||
'&': 'et',
|
|
||||||
'@': 'at',
|
|
||||||
'#': '',
|
|
||||||
$: '',
|
|
||||||
'%': '',
|
|
||||||
'*': '',
|
|
||||||
'+': '',
|
|
||||||
'=': '',
|
|
||||||
'<': '',
|
|
||||||
'>': '',
|
|
||||||
'~': '',
|
|
||||||
'`': '',
|
|
||||||
'^': '',
|
|
||||||
'°': '',
|
|
||||||
'…': '',
|
|
||||||
'—': '-',
|
|
||||||
'–': '-',
|
|
||||||
'−': '-',
|
|
||||||
' ': '-',
|
|
||||||
'\t': '-',
|
|
||||||
'\n': '-',
|
|
||||||
'\r': '-',
|
|
||||||
};
|
|
||||||
|
|
||||||
// Appliquer les remplacements
|
// Remplacer les points par des tirets (comme WordPress)
|
||||||
for (const [char, replacement] of Object.entries(replacements)) {
|
slug = slug.replace(/\./g, '-');
|
||||||
slug = slug.replace(new RegExp(char.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), replacement);
|
|
||||||
}
|
// Remplacer les caractères spéciaux
|
||||||
|
slug = slug.replace(/[«»"'?!:;,()[\]{}/\\|&@#$%*+=<>~`^°…—–−]/g, '');
|
||||||
|
|
||||||
|
// Remplacer leespaces et caractères de contrôle par des tirets
|
||||||
|
slug = slug.replace(/[\s\t\n\r]+/g, '-');
|
||||||
|
|
||||||
// Supprimer les caractères non alphanumériques restants (sauf tirets)
|
// Supprimer les caractères non alphanumériques restants (sauf tirets)
|
||||||
slug = slug.replace(/[^a-z0-9\-]/g, '');
|
slug = slug.replace(/[^a-z0-9\-]/g, '');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user