Skip to content

Instantly share code, notes, and snippets.

@Pfaufisch
Created August 31, 2023 22:17
Show Gist options
  • Save Pfaufisch/67d41bdc99c9a5130723a4aedfa1bd60 to your computer and use it in GitHub Desktop.
Save Pfaufisch/67d41bdc99c9a5130723a4aedfa1bd60 to your computer and use it in GitHub Desktop.
smol.pub Auto-Slugger
// ==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