What is slugify?
The slugify npm package is designed to convert strings into URL-friendly slugs. This is particularly useful for creating readable and SEO-friendly URLs from titles, names, or any text that may contain special characters, spaces, or uppercase letters. Slugify simplifies the process of generating slugs by replacing spaces and special characters with hyphens or other user-defined characters, making strings safe for URLs.
What are slugify's main functionalities?
Basic slugification
This feature demonstrates the basic usage of slugify to convert a string into a lowercase slug, replacing spaces and special characters with hyphens.
"use strict"; const slugify = require('slugify'); const title = 'Some Article Title!'; const slug = slugify(title); console.log(slug); // Output: 'some-article-title'
Custom replacement character
This feature shows how to use a custom replacement character (in this case, an underscore) instead of the default hyphen.
"use strict"; const slugify = require('slugify'); const title = 'Some Article Title!'; const slug = slugify(title, '_'); console.log(slug); // Output: 'some_article_title'
Locale-specific slugification
This feature illustrates the ability to handle locale-specific characters appropriately, converting them based on the specified locale.
"use strict"; const slugify = require('slugify'); const title = 'I ♥ Dogs'; const slug = slugify(title, { locale: 'de' }); console.log(slug); // Output: 'i-liebe-dogs'
Removing characters not allowed in URLs
This feature demonstrates the removal of specific characters not allowed or desired in the final slug, using a regular expression.
"use strict"; const slugify = require('slugify'); const title = 'New! Improved! Slugify!'; const slug = slugify(title, { remove: /[!]/g }); console.log(slug); // Output: 'new-improved-slugify'
Other packages similar to slugify
speakingurl
SpeakingURL is a package similar to slugify that generates human-readable, SEO-friendly URLs. It offers extensive language support and customization options, making it a strong alternative. However, slugify tends to be simpler to use for basic slugification needs.
limax
Limax is another alternative that focuses on creating slugs from strings with support for multiple languages, including Chinese, Japanese, and Korean. It provides more extensive language support compared to slugify but might be more complex for simple use cases.
url-slug
url-slug is a package that offers similar functionality to slugify, allowing for customizable and URL-friendly string transformation. It differs in its API and customization options, providing a different approach to slug generation that some users might prefer.
slugify
var slugify = require('slugify')
slugify('some string')
slugify('some string', '_')
- Vanilla ES5 JavaScript
- No dependencies
- Coerces foreign symbols to their English equivalent (check out the charMap for more details)
- Works in the browser (window.slugify) and AMD/CommonJS-flavored module loaders
Options
slugify('some string', {
replacement: '-',
remove: undefined,
lower: false,
strict: false,
})
For example, to remove *+~.()'"!:@
from the result slug, you can use slugify('..', {remove: /[*+~.()'"!:@]/g})
.
Extend
Out of the box slugify
comes with support for a handful of Unicode symbols. For example the ☢
(radioactive) symbol is not defined in the charMap
and therefore it will be stripped by default:
slugify('unicode ♥ is ☢')
However you can extend the supported symbols, or override the existing ones with your own:
slugify.extend({'☢': 'radioactive'})
slugify('unicode ♥ is ☢')
Keep in mind that the extend
method extends/overrides the default charMap
for the entire process. In case you need a fresh instance of the slugify's charMap
object you have to clean up the module cache first:
delete require.cache[require.resolve('slugify')]
var slugify = require('slugify')
Contribute
- Add chars to
charmap.json
- Run tests
npm test
- The tests will build the charmap in
index.js
and will sort the charmap.json
- Commit all modified files
This module was originally a vanilla javascript port of node-slug.
Note that the original slug module has been ported to vanilla javascript too.
One major difference between the two modules is that slugify
does not depend on the external unicode module.