-
-
Save Pfaufisch/67d41bdc99c9a5130723a4aedfa1bd60 to your computer and use it in GitHub Desktop.
smol.pub Auto-Slugger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name smol.pub - Auto Slugger | |
// @namespace Violentmonkey Scripts | |
// @match https://smol.pub/* | |
// @grant none | |
// @version 1.0 | |
// @author - | |
// @description 1.9.2023, 00:12:49 | |
// ==/UserScript== | |
(function () { | |
const titleInput = document.querySelector("#title"); | |
titleInput.addEventListener("keyup", generateSlug); | |
function generateSlug() { | |
const title = titleInput.value; | |
const slug = slugger(title); | |
const slugInput = document.querySelector("#slug"); | |
slugInput.value = slug; | |
} | |
})(); | |
function slugger(title) { | |
const normalizedTitle = title | |
.normalize("NFD") // Normalize to decomposed form | |
.replace(/[\u0300-\u036f]/g, ""); // Remove diacritics | |
return normalizedTitle | |
.toLowerCase() | |
.replace(/\s+/g, '-') // Replace spaces with hyphens | |
.replace(/[^\w-]+/g, '') // Remove non-word characters except hyphens | |
.replace(/--+/g, '-') // Replace consecutive hyphens with a single hyphen | |
.trim(); // Remove leading and trailing spaces/hyphens | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment