1 | # [slug](https://github.com/Trott/node-slug)
|
2 |
|
3 | Slugifies strings, even when they contain Unicode.
|
4 |
|
5 | Make strings URL-safe.
|
6 |
|
7 | - Respects [RFC 3986](https://tools.ietf.org/html/rfc3986)
|
8 | - No dependencies (except the Unicode table)
|
9 | - Works in browser (window.slug) and AMD/CommonJS-flavoured module loaders (except the Unicode symbols unless you use browserify but who wants to download a ~2mb js file, right?)
|
10 |
|
11 | ```
|
12 | npm install slug
|
13 | ```
|
14 |
|
15 | ## Example
|
16 |
|
17 | ```javascript
|
18 | var slug = require('slug')
|
19 | var print = console.log.bind(console, '>')
|
20 |
|
21 | print(slug('i ♥ unicode'))
|
22 | // > i-love-unicode
|
23 |
|
24 | print(slug('unicode ♥ is ☢')) // yes!
|
25 | // > unicode-love-is-radioactive
|
26 |
|
27 | print(slug('i ♥ unicode', '_')) // If you prefer something else than `-` as separator
|
28 | // > i_love_unicode
|
29 |
|
30 | slug.charmap['♥'] = 'freaking love' // change default charmap or use option {charmap:{…}} as 2. argument
|
31 | print(slug('I ♥ UNICODE'))
|
32 | // > I-freaking-love-UNICODE
|
33 |
|
34 | print(slug('☏-Number', {lower: true})) // If you prefer lower case
|
35 | // > telephone-number
|
36 |
|
37 | print(slug('i <3 unicode'))
|
38 | // > i-love-unicode
|
39 | ```
|
40 |
|
41 | ## options
|
42 |
|
43 | ```javascript
|
44 | // options is either object or replacement (sets options.replacement)
|
45 | slug('string', [{options} || 'replacement']);
|
46 | ```
|
47 |
|
48 | ```javascript
|
49 | slug.defaults.mode ='pretty';
|
50 | slug.defaults.modes['rfc3986'] = {
|
51 | replacement: '-', // replace spaces with replacement
|
52 | symbols: true, // replace unicode symbols or not
|
53 | remove: null, // (optional) regex to remove characters
|
54 | lower: true, // result in lower case
|
55 | charmap: slug.charmap, // replace special characters
|
56 | multicharmap: slug.multicharmap // replace multi-characters
|
57 | };
|
58 | slug.defaults.modes['pretty'] = {
|
59 | replacement: '-',
|
60 | symbols: true,
|
61 | remove: /[.]/g,
|
62 | lower: false,
|
63 | charmap: slug.charmap,
|
64 | multicharmap: slug.multicharmap
|
65 | };
|
66 | ```
|
67 |
|
68 | ## browser
|
69 |
|
70 | When using browserify you might want to remove the symbols table from your bundle by using `--ignore` similar to this:
|
71 | ```bash
|
72 | # generates a standalone slug browser bundle:
|
73 | browserify slug.js --detect-globals false --ignore unicode/category/So -s slug > slug-browser.js
|
74 | ```
|
75 |
|
76 | [![Build Status](https://secure.travis-ci.org/dodo/node-slug.png)](http://travis-ci.org/dodo/node-slug)
|