Skip to content

Instantly share code, notes, and snippets.

@faustoct1
Created October 23, 2022 08:52
Show Gist options
  • Save faustoct1/0ce06cbae858b1e60159ef8c401ba97e to your computer and use it in GitHub Desktop.
Save faustoct1/0ce06cbae858b1e60159ef8c401ba97e to your computer and use it in GitHub Desktop.
Converter qualquer string em SEO friendly
const toSeoUrl = (name) => {
return name.toString() // Convert to string
.normalize('NFD') // Change diacritics
.replace(/[\u0300-\u036f]/g,'') // Remove illegal characters
.replace(/\s+/g,'-') // Change whitespace to dashes
.toLowerCase() // Change to lowercase
.replace(/&/g,'-and-') // Replace ampersand
// eslint-disable-next-line
.replace(/[^a-z0-9\-]/g,'') // Remove anything that is not a letter, number or dash
.replace(/-+/g,'-') // Remove duplicate dashes
.replace(/^-*/,'') // Remove starting dashes
.replace(/-*$/,'') // Remove trailing dashes
.normalize("NFD") // Remove accents
.replace(/\p{Diacritic}/gu, "") // Remove accents
}
//output http://localhost:5000/essa-string-nao-e-mas-vai-ser-seo-friendly
console.log(`http://localhost:5000/${toSeoUrl('Essa string não é mas vai ser seo friendly!!!')}`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment