Naive Slugify Function
I'm sure there is a better way to do this, but this works almost no matter how picky your slug is:
Only allows english letters, numbers and dashes. Replaces spaces with dashes.
/**
* Creates URI compatible slug from text
*
* Only allows english characters, 0-9 and dashes
*
* @param {string} text text to slugify
* @return {string} slug
*/
function slugify(text) {
return encodeURIComponent(
text
.replace(/[^a-zA-Z0-9 -]/g, '')
.replace(/ /g, '-')
.toLowerCase()
);
}
export default slugify;
This may be handy for GatsbyJS or other frameworks that are picky about slugs.