Socket
Socket
Sign inDemoInstall

slugify

Package Overview
Dependencies
0
Maintainers
2
Versions
42
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    slugify

Slugifies a String


Version published
Weekly downloads
3.2M
increased by5.1%
Maintainers
2
Install size
20.4 kB
Created
Weekly downloads
 

Package description

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

Changelog

Source

v1.6.6 (2023-03-26)

  • #174 correctly handle empty strings in charmaps (@iliazeus)
  • #169 Add changelog (@simov)
  • #168 chore: document limitations of the remove option (#168) (@Trott)
  • #157 Run CI in Node.js 18 (@stscoundrel)
  • #151 Update README.md (#151) (@lorand-horvath)

Readme

Source

slugify

npm-version coveralls-status

var slugify = require('slugify')

slugify('some string') // some-string

// if you prefer something other than '-' as separator
slugify('some string', '_')  // some_string
  • Vanilla ES2015 JavaScript
    • If you need to use Slugify with older browsers, consider using version 1.4.7
  • 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: '-',  // replace spaces with replacement character, defaults to `-`
  remove: undefined, // remove characters that match regex, defaults to `undefined`
  lower: false,      // convert to lower case, defaults to `false`
  strict: false,     // strip special characters except replacement, defaults to `false`
  locale: 'vi',      // language code of the locale to use
  trim: true         // trim leading and trailing replacement chars, defaults to `true`
})

Remove

For example, to remove *+~.()'"!:@ from the result slug, you can use slugify('..', {remove: /[*+~.()'"!:@]/g}).

  • If the value of remove is a regular expression, it should be a character class and only a character class. It should also use the global flag. (For example: /[*+~.()'"!:@]/g.) Otherwise, the remove option might not work as expected.
  • If the value of remove is a string, it should be a single character. Otherwise, the remove option might not work as expected.

Locales

The main charmap.json file contains all known characters and their transliteration. All new characters should be added there first. In case you stumble upon a character already set in charmap.json, but not transliterated correctly according to your language, then you have to add those characters in locales.json to override the already existing transliteration in charmap.json, but for your locale only.

You can get the correct language code of your language from here.

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 ☢') // unicode-love-is

However you can extend the supported symbols, or override the existing ones with your own:

slugify.extend({'☢': 'radioactive'})
slugify('unicode ♥ is ☢') // unicode-love-is-radioactive

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

  1. Add chars to charmap.json
  2. Run tests npm test
  3. The tests will build the charmap in index.js and will sort the charmap.json
  4. Commit all modified files

Originally this was a vanilla javascript port of node-slug.
Note that the original slug module has been ported to vanilla javascript too.

Keywords

FAQs

Last updated on 26 Mar 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc