UNPKG

4.95 kBMarkdownView Raw
1# 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
18import slugify from '@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
53import slugify from '@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
73import slugify from '@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
90import slugify from '@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
115import slugify from '@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
128import slugify from '@sindresorhus/slugify';
129
130slugify('foo@unicorn', {
131 customReplacements: [
132 ['@', ' at ']
133 ]
134});
135//=> 'foo-at-unicorn'
136```
137
138Another example:
139
140```js
141import slugify from '@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
161import slugify from '@sindresorhus/slugify';
162
163slugify('_foo_bar');
164//=> 'foo-bar'
165
166slugify('_foo_bar', {preserveLeadingUnderscore: true});
167//=> '_foo-bar'
168```
169
170##### preserveTrailingDash
171
172Type: `boolean`\
173Default: `false`
174
175If your string ends with a dash, it will be preserved in the slugified string.
176
177For example, using slugify on an input field would allow for validation while not preventing the user from writing a slug.
178
179```js
180import slugify from '@sindresorhus/slugify';
181
182slugify('foo-bar-');
183//=> 'foo-bar'
184
185slugify('foo-bar-', {preserveTrailingDash: true});
186//=> 'foo-bar-'
187```
188
189##### preserveCharacters
190
191Type: `string[]`\
192Default: `[]`
193
194Preserve certain characters.
195
196It cannot contain the `separator`.
197
198For example, if you want to slugify URLs, but preserve the HTML fragment `#` character.
199
200```js
201import slugify from '@sindresorhus/slugify';
202
203slugify('foo_bar#baz', {preserveCharacters: ['#']});
204//=> 'foo-bar#baz'
205```
206
207### slugifyWithCounter()
208
209Returns a new instance of `slugify(string, options?)` with a counter to handle multiple occurrences of the same string.
210
211#### Example
212
213```js
214import {slugifyWithCounter} from '@sindresorhus/slugify';
215
216const slugify = slugifyWithCounter();
217
218slugify('foo bar');
219//=> 'foo-bar'
220
221slugify('foo bar');
222//=> 'foo-bar-2'
223
224slugify.reset();
225
226slugify('foo bar');
227//=> 'foo-bar'
228```
229
230#### Use-case example of counter
231
232If, for example, you have a document with multiple sections where each subsection has an example.
233
234```md
235## Section 1
236
237### Example
238
239## Section 2
240
241### Example
242```
243
244You can then use `slugifyWithCounter()` to generate unique HTML `id`'s to ensure anchors will link to the right headline.
245
246### slugify.reset()
247
248Reset the counter
249
250#### Example
251
252```js
253import {slugifyWithCounter} from '@sindresorhus/slugify';
254
255const slugify = slugifyWithCounter();
256
257slugify('foo bar');
258//=> 'foo-bar'
259
260slugify('foo bar');
261//=> 'foo-bar-2'
262
263slugify.reset();
264
265slugify('foo bar');
266//=> 'foo-bar'
267```
268
269## Related
270
271- [slugify-cli](https://github.com/sindresorhus/slugify-cli) - CLI for this module
272- [transliterate](https://github.com/sindresorhus/transliterate) - Convert Unicode characters to Latin characters using transliteration
273- [filenamify](https://github.com/sindresorhus/filenamify) - Convert a string to a valid safe filename