slugify

repository·master·Indexed 23 days ago

https://github.com/simov/slugify

A lightweight, zero-dependency JavaScript library for converting strings into URL-friendly slugs. It supports Unicode symbol transliteration, custom separators, locale-specific overrides for languages such as German, French, and Spanish, and custom character mappings via slugify.extend().

Tokens
1.5K
Snippets
2
Records
8
Agent score
33%

What's inside slugify

  1. Use slugify to create slugs

    master

    The slugify function converts a string into a URL-friendly slug. By default, it coerces foreign symbols to their English equivalents and uses a hyphen (-) as a separator. You can also provide a custom separator as a second argument.

    var slugify = require('slugify')
    
    slugify('some string') // some-string
    
    // if you prefer something other than '-' as separator
    slugify('some string', '_')  // some_string
  2. Remove specific characters using the remove option

    master

    To remove specific characters from the resulting slug, use the remove option.

    Requirements for remove:

    • If using a Regular Expression: It must be a character class (e.g., /[*+~.()"'!:@]/g) and it must include the global flag (g).
    • If using a String: It must be a single character.

    Failure to follow these requirements may cause the remove option to behave unexpectedly.

  3. Use locale-specific character mappings

    master

    The slugify function supports specific character mappings for different languages via the locale option. This is useful when certain characters should be translated differently depending on the language context (e.g., & in German might be und while in Spanish it is y).

    Supported locales include:

    • bg (Bulgarian)
    • de (German)
    • es (Spanish)
    • fr (French)
    • pt (Portuguese)
    • uk (Ukrainian)
    • vi (Vietnamese)
    • da (Danish)
    • nb (Norwegian Bokmål)
    • it (Italian)
    • nl (Dutch)
    • sv (Swedish)

    Example

    const slugify = require('slugify');
    
    // German locale: '&' becomes 'und'
    const deSlug = slugify('A & B', { locale: 'de' });
    // Output: 'a-und-b'
  4. Configure slugify with options

    master

    You can pass an options object as the second argument to slugify to customize the transformation process.

    Available options:

    • replacement: Character used to replace spaces. Defaults to '-'.
    • remove: A regular expression or a single character used to remove specific characters from the result. Defaults to undefined.
    • lower: Boolean to convert the result to lower case. Defaults to false.
    • strict: Boolean to strip special characters except the replacement character. Defaults to false.
    • locale: ISO 639-1 language code to use for specific transliterations. Defaults to 'vi'.
    • trim: Boolean to trim leading and trailing replacement characters. Defaults to true.
    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`
    })
  5. Extend slugify with custom symbols

    master

    By default, slugify strips Unicode symbols that are not defined in its internal charMap. You can use slugify.extend() to add new symbol mappings or override existing ones.

    Note: extend modifies the charMap for the entire process. If you are in a Node.js environment and need a fresh instance with the original charMap, you must clear the module cache before requiring slugify again.

  6. Configure slugify options

    master

    The slugify function accepts an optional options object to customize the transformation process:

    OptionTypeDefaultDescription
    replacementstring'-'The character used to replace spaces.
    localestringundefinedA locale key (e.g., 'de', 'fr', 'es') to use locale-specific character mappings.
    trimbooleantrueWhether to trim whitespace from the beginning and end of the string.
    lowerbooleanfalseWhether to convert the resulting slug to lowercase.
    strictbooleanfalseIf true, removes everything except basic Latin letters (A-Za-z), numbers (0-9), and whitespace.
    removeRegExp/[^\w\s$*_+~.()'"!\-:@]+/gA regular expression used to remove characters that are not allowed in the slug.

    Example with options

    const slugify = require('slugify');
    
    const slug = slugify('Hello World!', {
      replacement: '_',
      lower: true,
      strict: true
    });
    // Output: 'hello_world'
  7. Extend character mappings with slugify.extend()

    master

    You can add custom character mappings to the global charMap used by slugify using the extend method. This allows you to define how specific symbols or characters should be converted into text across all subsequent calls.

    const slugify = require('slugify');
    
    // Add a custom mapping for a specific symbol
    slugify.extend({
      '🚀': 'rocket'
    });
    
    const slug = slugify('Launch 🚀');
    // Output: 'launch-rocket'
  8. Use slugify() to convert strings to slugs

    master

    The slugify function converts a string into a URL-friendly slug. It handles character normalization (e.g., converting accented characters to their base forms), character mapping (e.g., converting & to and), and configurable replacements for spaces and special characters.

    Basic Usage

    By default, slugify replaces spaces with hyphens (-), trims whitespace, and uses a default regex to remove non-alphanumeric characters.

    const slugify = require('slugify');
    
    const slug = slugify('Hello World!');
    // Output: 'hello-world'