• Stars
    star
    1,346
  • Rank 34,902 (Top 0.7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 12 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Slugifies a string

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.

More Repositories

1

grant

OAuth Proxy
JavaScript
3,925
star
2

express-admin

MySQL, MariaDB, PostgreSQL, SQLite admin for Node.js
JavaScript
1,148
star
3

markdown-viewer

Markdown Viewer / Browser Extension
JavaScript
853
star
4

purest

REST API Client Library
JavaScript
537
star
5

simplr-smoothscroll

Smooth scrolling in all browsers
HTML
209
star
6

screenshot-capture

Screenshot Capture / Browser Extension
JavaScript
158
star
7

request-compose

Composable HTTP Client
JavaScript
88
star
8

express-admin-examples

Express Admin Examples
JavaScript
74
star
9

stars

GitHub Stars History and Stats
JavaScript
45
star
10

native-messaging

Native Messaging Host Protocol for Browser Extensions
JavaScript
44
star
11

chrome-webstore

Google Chrome Web Store HTTP Client
JavaScript
35
star
12

deep-copy

Deep copy objects and arrays
JavaScript
35
star
13

styler

Styler / Browser Extension
JavaScript
22
star
14

fastify-grant

Fastify plugin for Grant OAuth Proxy
JavaScript
22
star
15

async-harmony

Asynchronous Harmonies
JavaScript
18
star
16

grant-aws

AWS Lambda handler for Grant
HCL
13
star
17

purest-providers

REST API provider configuration for the Purest module
JavaScript
10
star
18

oauth-like-a-boss

Once And For All
JavaScript
9
star
19

grant-profile

User profile middleware for Grant
JavaScript
8
star
20

xsql

SQL Query Builder
JavaScript
8
star
21

markdown-syntax

Markdown Syntax examples for Markdown Viewer
8
star
22

stocktwits

StockTwits API wrapper
JavaScript
7
star
23

recursive-fs

Asynchronous recursive file system operations
JavaScript
7
star
24

grant-vercel

Vercel Serverless Function handler for Grant
Makefile
7
star
25

simov.github.io

My place on the WWW! Oh man!
JavaScript
6
star
26

wallhaven-client

wallhaven.cc HTTP Client
JavaScript
5
star
27

loca

WebKit reporter for Mocha
JavaScript
5
star
28

mysql-validator

MySql data type validation
JavaScript
4
star
29

grant-types

3
star
30

grant-oidc

OpenID Connect middleware for Grant
JavaScript
3
star
31

grant-session

JavaScript
2
star
32

grant-gcloud

Google Cloud Function handler for Grant
HCL
2
star
33

hapi-hogan

Hogan.js templating in Hapi.js
JavaScript
2
star
34

facebook-refresh-token

Refresh Facebook OAuth Access Token
JavaScript
2
star
35

request-oauth

OAuth 1.0a support for request-compose
JavaScript
2
star
36

sr-pagination

Simple pagination generator
JavaScript
2
star
37

riotjs-examples

RiotJS Examples
JavaScript
2
star
38

grant-azure

Azure Function handler for Grant
Makefile
2
star
39

request-logs

HTTP debug logs for request-compose
JavaScript
2
star
40

express-admin-tests

Express Admin Tests
JavaScript
1
star
41

electron-socket

JavaScript
1
star
42

lure

Lure people to your public organization
JavaScript
1
star
43

simplr-fade

jQuery Slider Plugin
JavaScript
1
star
44

request-multipart

Multipart body support for request-compose
JavaScript
1
star
45

slack-incoming-facebook

Slack Incoming WebHook for Facebook
JavaScript
1
star
46

rubinho

RubyGems visualization tool
JavaScript
1
star
47

ansi-webkit

Print ANSI escaped colors in a browser console
JavaScript
1
star
48

matrix

JavaScript
1
star
49

http-headers

Caseless HTTP Headers
JavaScript
1
star
50

passport-stocktwits

Stocktwits authentication strategy for Passport and Node.js
JavaScript
1
star
51

template

Node Module Template
1
star
52

bulk-stream

A stream that emits multiple other streams and chunks one after another
JavaScript
1
star
53

browserside-organic

Organic development in the browser.
JavaScript
1
star
54

http-fetch

HTTP Client for the Browser
JavaScript
1
star
55

markdown-themes

Themes for Markdown Viewer / Browser Extension
CSS
1
star
56

grant-wiki

1
star
57

invites

Public invites for Slack and GitHub
JavaScript
1
star