43 lines
1.3 KiB
PHP
43 lines
1.3 KiB
PHP
<script>
|
|
(() => {
|
|
const slugInput = document.querySelector('input[name="slug"]');
|
|
if (!(slugInput instanceof HTMLInputElement)) {
|
|
return;
|
|
}
|
|
|
|
const sourceInput = document.querySelector('input[name="name"]') || document.querySelector('input[name="title"]');
|
|
if (!(sourceInput instanceof HTMLInputElement)) {
|
|
return;
|
|
}
|
|
|
|
const slugify = (value) => {
|
|
return String(value || '')
|
|
.normalize('NFKD')
|
|
.toLowerCase()
|
|
.replace(/[\u0300-\u036f]/g, '')
|
|
.replace(/[^a-z0-9\u4e00-\u9fa5\s-]/g, '')
|
|
.trim()
|
|
.replace(/[\s_]+/g, '-')
|
|
.replace(/-+/g, '-')
|
|
.replace(/^-|-$/g, '');
|
|
};
|
|
|
|
let manualEdited = slugInput.value.trim() !== '';
|
|
slugInput.addEventListener('input', () => {
|
|
manualEdited = slugInput.value.trim() !== '';
|
|
});
|
|
|
|
sourceInput.addEventListener('input', () => {
|
|
if (manualEdited && slugInput.value.trim() !== '') {
|
|
return;
|
|
}
|
|
|
|
const suggested = slugify(sourceInput.value);
|
|
if (suggested !== '') {
|
|
slugInput.value = suggested;
|
|
}
|
|
});
|
|
})();
|
|
</script>
|
|
|