What is speakingurl?
The 'speakingurl' npm package is used to generate slugs from strings. It is particularly useful for creating URL-friendly strings from titles or other text inputs. The package supports multiple languages and offers various customization options.
What are speakingurl's main functionalities?
Basic Slug Generation
This feature allows you to convert a string into a URL-friendly slug. Special characters are removed, and spaces are replaced with hyphens.
const getSlug = require('speakingurl');
const slug = getSlug('Hello World!');
console.log(slug); // Output: 'hello-world'
Custom Separator
You can customize the separator used in the slug. In this example, an underscore is used instead of the default hyphen.
const getSlug = require('speakingurl');
const slug = getSlug('Hello World!', { separator: '_' });
console.log(slug); // Output: 'hello_world'
Language Support
The package supports multiple languages, allowing you to generate slugs that are appropriate for different linguistic contexts.
const getSlug = require('speakingurl');
const slug = getSlug('你好,世界', { lang: 'zh' });
console.log(slug); // Output: 'ni-hao-shi-jie'
Custom Transliteration
You can define custom transliterations for specific characters, giving you more control over the slug generation process.
const getSlug = require('speakingurl');
const slug = getSlug('Hello World!', { custom: { 'H': 'h', 'W': 'w' } });
console.log(slug); // Output: 'hello-world'
Other packages similar to speakingurl
slugify
'slugify' is another popular package for generating URL-friendly slugs. It offers similar functionality to 'speakingurl' but is often considered simpler and more lightweight. It also supports custom replacements and different character sets.
limax
'limax' is a slug generator that focuses on Unicode support and transliteration. It is highly configurable and supports a wide range of languages, making it a strong alternative to 'speakingurl' for internationalization needs.
url-slug
'url-slug' is a package designed to create slugs from strings with a focus on simplicity and ease of use. It provides basic slug generation features and is a good choice for straightforward use cases.
Speaking URL
Generate of so called "static" or "nice-looking" or "SpeakingURL" or "slug" from a string.
For use in browser and server.
Installation
$ npm install speakingurl
Usage
getSlug(input, [options]);
input
: {string} to convert; options
: {object|string} config object or separator string (see below)
-
separator
{string} default: '-'
- char that replace the whitespaces
-
lang
{string} default: 'en'
- language for symbol translation ('en, 'de', more coming soon)
-
maintainCase
{boolean} default: false
- true => maintain case chars
- false => convert all chars to lower case
-
uric
{boolean} default: false
- true => additionally allow chars: ";", "?", ":", "@", "&", "=", "+", "$", ",", "/"
- false => only Base64 chars allowed (/A-Za-z0-9-_/)
-
uricNoSlash
{boolean} default: false
- true => additionally allow chars: ";", "?", ":", "@", "&", "=", "+", "$", ","
- false => only Base64 chars allowed (/A-Za-z0-9-_/)
-
mark
{boolean} default: false
- true => additionally allow chars: "-", "_", ".", "!", "~", "*", "'", "(", ")"
- false => only Base64 chars allowed (/A-Za-z0-9-_/)
-
custom
{object} default: {}
- custom map for translation, overwrites all i.e. { '&': '#', '*': ' star ' }
-
truncate
{number} default: 0
- 0 => don't trim length
-
0 => trim to max length while not breaking any words
-
options
{string} separator
notes: default only Base64 chars are allowed (/A-Za-z0-9_-/), setting uric
, uricNoSlash
or/and mark
to true
will add the specified chars to the allowed chars. The separator-character is always allowed.
Examples
var getSlug = require('speakingurl'),
slug;
slug = getSlug("Schöner Titel läßt grüßen!? Bel été !");
console.log(slug);
slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", "*");
console.log(slug);
slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {separator: "_"});
console.log(slug);
slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {uric: true});
console.log(slug);
slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {uricNoSlash: true});
console.log(slug);
slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {mark: true});
console.log(slug);
slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {truncate: 20});
console.log(slug);
slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {maintainCase: true});
console.log(slug);
slug = getSlug("Apfel & Birne!", {lang: 'de'});
console.log(slug);
slug = getSlug('Foo & Bar * Baz', {custom: {'&': ' doo '}, uric:true } );
console.log(slug);
slug = getSlug('Foo ♥ Bar');
console.log(slug);
slug = getSlug('Foo & Bar | Baz * Doo', {custom:{'*': "Boo"},mark:true});
console.log(slug);
Tests
$ npm test
Credits
Informations
http://tools.ietf.org/html/rfc3986
License
BSD
The BSD 3-Clause License (BSD3)
Copyright (c) 2013 Sascha Droste sascha.droste@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.