Slug Generator: Cleaning Apostrophes, Ampersands and Trailing Hyphens Out of My Blog URLs
Slug Generator: Cleaning Apostrophes, Ampersands and Trailing Hyphens Out of My Blog URLs Last month I moved 312 posts from an old WordPress site to Eleventy. The CMS export gave me titles exactly as the editors typed them: "We're Open — 20% Off This Week & Next", "Don't Panic: A Developer's Guide to Production". My one-liner title.lower().replace(" ", "-") deployed without a single error and…
Moving 312 blog posts from an old WordPress site to Eleventy required cleaning up URLs that contained apostrophes, ampersands, trailing hyphens, and other characters. Last month, the CMS export provided titles exactly as they were typed by editors, which sometimes contained special characters such as —, & and !. To address these issues, the author experimented with three different methods to generate clean slugs for the blog posts.
Method 1: Python used the python-slugify library (version 8.0.0) to generate slugs by normalizing the text using NFKD encoding, removing non-ASCII characters, and replacing unwanted characters with hyphens. This approach handled collisions by appending the post ID to the slug. The resulting slugs were clean, with no curly apostrophes, em dashes, or other special characters. The author discovered that stripping trailing hyphens and using the lower() method contributed to generating a robust set of slugs.
Method 2: Node.js implemented the slugify package (version 1.6.6) to generate slugs from the exported CSV file. The strict flag was enabled to prevent the inclusion of percent-encoded characters like %, &, and @ in the final URLs. This method yielded identical slugs to the Python approach, confirming the reliability of the chosen slugification method.
Method 3: For quick fixes in the browser, the author utilized an online slug generator to generate URLs for one-off titles. This method flagged issues such as double hyphens and allowed for manual adjustments before publishing. However, the author highlighted three potential pitfalls: handling duplicate apostrophes, trailing hyphens, and ensuring that slugs did not contain any characters that might be percent-encoded, as this would result in multiple URLs for the same content.
By employing strict slugification and careful punctuation removal, the author was able to maintain a consistent and clean URL structure across all blog posts.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.