DEV Community

Discussion on: 1 line of code: How to create an URL Slug from a string

Collapse
 
martinkr profile image
Martin Krause

Hey Jon,

thank you for the time to comment.

What are your concerns about the code? Unicode characters can be part of an url, we're just removing some "unnecessary" characters to shorten the string (:;…?!) and replacing the whitespace with '-' to mach the conventions.

What would be your suggestion to enhance the code?

Cheers!

Collapse
 
jonrandy profile image
Jon Randy 🎖️

When you copy a URL containing Unicode characters from the address bar and then, for example, paste it into a chat message - all the unicode characters are now encoded - ruining your nice 'readable' URL.

A lot of the good slug libraries cope with this to varying extents by trying to keep the slug to the standard latin alphabet. Obviously this makes the libraries quite complex as simple character substitutions are not enough when working with languages that use entirely different glyphs (e.g Thai - สวัสดี) - transliteration is required.

Thread Thread
 
martinkr profile image
Martin Krause

Well, a full fledged library is beyond the scope of "these one line functions" :D I'll add a disclaimer that it should be used with the standard latin alphabet.

Thank you for your time,

Martin