UNPKG

4.18 kBMarkdownView Raw
1
2# slugify
3
4[![npm-version]][npm] [![coveralls-status]][coveralls]
5
6```js
7var slugify = require('slugify')
8
9slugify('some string') // some-string
10
11// if you prefer something other than '-' as separator
12slugify('some string', '_') // some_string
13```
14
15- Vanilla ES2015 JavaScript
16 - If you need to use Slugify with older browsers, consider using [version 1.4.7](https://github.com/simov/slugify/releases/tag/v1.4.7)
17- No dependencies
18- Coerces foreign symbols to their English equivalent (check out the [charMap][charmap] for more details)
19- Works in the browser (window.slugify) and AMD/CommonJS-flavored module loaders
20
21## Options
22
23```js
24slugify('some string', {
25 replacement: '-', // replace spaces with replacement character, defaults to `-`
26 remove: undefined, // remove characters that match regex, defaults to `undefined`
27 lower: false, // convert to lower case, defaults to `false`
28 strict: false, // strip special characters except replacement, defaults to `false`
29 locale: 'vi', // language code of the locale to use
30 trim: true // trim leading and trailing replacement chars, defaults to `true`
31})
32```
33
34## Remove
35
36For example, to remove `*+~.()'"!:@` from the result slug, you can use `slugify('..', {remove: /[*+~.()'"!:@]/g})`.
37
38* If the value of `remove` is a regular expression, it should be a
39 [character class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Character_Classes)
40 and only a character class. It should also use the
41 [global flag](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global).
42 (For example: `/[*+~.()'"!:@]/g`.) Otherwise, the `remove` option might not
43 work as expected.
44* If the value of `remove` is a string, it should be a single character.
45 Otherwise, the `remove` option might not work as expected.
46
47## Locales
48
49The 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.
50
51You can get the correct language code of your language from [here](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes).
52
53## Extend
54
55Out of the box `slugify` comes with support for a handful of Unicode symbols. For example the `☢` (radioactive) symbol is not defined in the [`charMap`][charmap] and therefore it will be stripped by default:
56
57```js
58slugify('unicode ♥ is ☢') // unicode-love-is
59```
60
61However you can extend the supported symbols, or override the existing ones with your own:
62
63```js
64slugify.extend({'☢': 'radioactive'})
65slugify('unicode ♥ is ☢') // unicode-love-is-radioactive
66```
67
68Keep 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:
69
70```js
71delete require.cache[require.resolve('slugify')]
72var slugify = require('slugify')
73```
74
75## Contribute
76
771. Add chars to `charmap.json`
782. Run tests `npm test`
793. The tests will build the charmap in `index.js` and will sort the `charmap.json`
804. Commit **all** modified files
81
82---
83
84> Originally this was a vanilla javascript port of [node-slug][node-slug].<br>
85> Note that the original [slug][slug] module has been ported to vanilla javascript too.
86
87
88 [npm-version]: https://img.shields.io/npm/v/slugify.svg?style=flat-square (NPM Package Version)
89 [coveralls-status]: https://img.shields.io/coveralls/simov/slugify.svg?style=flat-square (Test Coverage - Coveralls)
90
91 [npm]: https://www.npmjs.com/package/slugify
92 [coveralls]: https://coveralls.io/r/simov/slugify?branch=master
93
94 [node-slug]: https://github.com/dodo/node-slug
95 [slug]: https://www.npmjs.com/package/slug
96 [unicode]: https://www.npmjs.com/package/unicode
97 [index]: https://github.com/simov/slugify/blob/master/index.js
98 [charmap]: https://github.com/simov/slugify/blob/master/config/charmap.json