UNPKG

4.41 kBMarkdownView Raw
1# slugify [![Build Status](https://travis-ci.com/sindresorhus/slugify.svg?branch=master)](https://travis-ci.com/github/sindresorhus/slugify)
2
3> Slugify a string
4
5Useful for URLs, filenames, and IDs.
6
7It handles most major languages, including [German (umlauts)](https://en.wikipedia.org/wiki/Germanic_umlaut), Vietnamese, Arabic, Russian, [and more](https://github.com/sindresorhus/transliterate#supported-languages).
8
9## Install
10
11```
12$ npm install @sindresorhus/slugify
13```
14
15## Usage
16
17```js
18const slugify = require('@sindresorhus/slugify');
19
20slugify('I ♥ Dogs');
21//=> 'i-love-dogs'
22
23slugify(' Déjà Vu! ');
24//=> 'deja-vu'
25
26slugify('fooBar 123 $#%');
27//=> 'foo-bar-123'
28
29slugify('я люблю единорогов');
30//=> 'ya-lyublyu-edinorogov'
31```
32
33## API
34
35### slugify(string, options?)
36
37#### string
38
39Type: `string`
40
41String to slugify.
42
43#### options
44
45Type: `object`
46
47##### separator
48
49Type: `string`\
50Default: `'-'`
51
52```js
53const slugify = require('@sindresorhus/slugify');
54
55slugify('BAR and baz');
56//=> 'bar-and-baz'
57
58slugify('BAR and baz', {separator: '_'});
59//=> 'bar_and_baz'
60
61slugify('BAR and baz', {separator: ''});
62//=> 'barandbaz'
63```
64
65##### lowercase
66
67Type: `boolean`\
68Default: `true`
69
70Make the slug lowercase.
71
72```js
73const slugify = require('@sindresorhus/slugify');
74
75slugify('Déjà Vu!');
76//=> 'deja-vu'
77
78slugify('Déjà Vu!', {lowercase: false});
79//=> 'Deja-Vu'
80```
81
82##### decamelize
83
84Type: `boolean`\
85Default: `true`
86
87Convert camelcase to separate words. Internally it does `fooBar``foo bar`.
88
89```js
90const slugify = require('@sindresorhus/slugify');
91
92slugify('fooBar');
93//=> 'foo-bar'
94
95slugify('fooBar', {decamelize: false});
96//=> 'foobar'
97```
98
99##### customReplacements
100
101Type: `Array<string[]>`\
102Default: `[
103 ['&', ' and '],
104 ['🦄', ' unicorn '],
105 ['♥', ' love ']
106]`
107
108Add your own custom replacements.
109
110The replacements are run on the original string before any other transformations.
111
112This only overrides a default replacement if you set an item with the same key, like `&`.
113
114```js
115const slugify = require('@sindresorhus/slugify');
116
117slugify('Foo@unicorn', {
118 customReplacements: [
119 ['@', 'at']
120 ]
121});
122//=> 'fooatunicorn'
123```
124
125Add a leading and trailing space to the replacement to have it separated by dashes:
126
127```js
128const slugify = require('@sindresorhus/slugify');
129
130slugify('foo@unicorn', {
131 customReplacements: [
132 ['@', ' at ']
133 ]
134});
135//=> 'foo-at-unicorn'
136```
137
138Another example:
139
140```js
141const slugify = require('@sindresorhus/slugify');
142
143slugify('I love 🐶', {
144 customReplacements: [
145 ['🐶', 'dogs']
146 ]
147});
148//=> 'i-love-dogs'
149```
150
151##### preserveLeadingUnderscore
152
153Type: `boolean`\
154Default: `false`
155
156If your string starts with an underscore, it will be preserved in the slugified string.
157
158Sometimes leading underscores are intentional, for example, filenames representing hidden paths on a website.
159
160```js
161const slugify = require('@sindresorhus/slugify');
162
163slugify('_foo_bar');
164//=> 'foo-bar'
165
166slugify('_foo_bar', {preserveLeadingUnderscore: true});
167//=> '_foo-bar'
168```
169
170### slugify.counter()
171
172Returns a new instance of `slugify(string, options?)` with a counter to handle multiple occurences of the same string.
173
174#### Example
175
176```js
177const slugify = require('@sindresorhus/slugify');
178
179const countableSlugify = slugify.counter();
180
181countableSlugify('foo bar');
182//=> 'foo-bar'
183
184countableSlugify('foo bar');
185//=> 'foo-bar-2'
186
187countableSlugify.reset();
188
189countableSlugify('foo bar');
190//=> 'foo-bar'
191```
192
193#### Use-case example of counter
194
195If, for example, you have a document with multiple sections where each subsection has an example.
196
197```md
198## Section 1
199
200### Example
201
202## Section 2
203
204### Example
205```
206
207You can then use `slugify.counter()` to generate unique HTML `id`'s to ensure anchors will link to the right headline.
208
209### slugify.reset()
210
211Reset the counter
212
213#### Example
214
215```js
216const slugify = require('@sindresorhus/slugify');
217
218const countableSlugify = slugify.counter();
219
220countableSlugify('foo bar');
221//=> 'foo-bar'
222
223countableSlugify('foo bar');
224//=> 'foo-bar-2'
225
226countableSlugify.reset();
227
228countableSlugify('foo bar');
229//=> 'foo-bar'
230```
231
232## Related
233
234- [slugify-cli](https://github.com/sindresorhus/slugify-cli) - CLI for this module
235- [transliterate](https://github.com/sindresorhus/transliterate) - Convert Unicode characters to Latin characters using transliteration
236- [filenamify](https://github.com/sindresorhus/filenamify) - Convert a string to a valid safe filename