Skip to content

Instantly share code, notes, and snippets.

@michaelpumo
Created July 28, 2017 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelpumo/fecf088d4bfcd73327c94d501d50b5ef to your computer and use it in GitHub Desktop.
Save michaelpumo/fecf088d4bfcd73327c94d501d50b5ef to your computer and use it in GitHub Desktop.
A utility function for turning a string into a slugged version.
function slugify (text) {
const special = 'ÁÄÂÀÃÅČÇĆĎÉĚËÈÊẼĔȆÍÌÎÏŇÑÓÖÒÔÕØŘŔŠŤÚŮÜÙÛÝŸŽáäâàãåčçćďéěëèêẽĕȇíìîïňñóöòôõøðřŕšťúůüùûýÿžþÞĐđßÆa·/_,:;'
const ordinary = 'AAAAAACCCDEEEEEEEEIIIINNOOOOOORRSTUUUUUYYZaaaaaacccdeeeeeeeeiiiinnooooooorrstuuuuuyyzbBDdBAa------'
const p = new RegExp(special.split('').join('|'), 'g')
return text.toString().toLowerCase()
.replace(/\s+/g, '-')
.replace(p, c => ordinary.charAt(special.indexOf(c)))
.replace(/&/g, '-and-')
.replace(/[^\w-]+/g, '')
.replace(/--+/g, '-')
.replace(/^-+/, '')
.replace(/-+$/, '')
}
export { slugify }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment