Package detail

speakingurl

pid6.6mBSD-3-Clause14.0.1

Generate a slug – transliteration with a lot of options

slug, slugify, speakingurl, transliteration

readme

SpeakingURL

Build Status NPM version Bower version Gem Version Gitter Flattr

Generate a slug with a lot of options; create a so-called Semantic URL or 'Clean URL' or 'Pretty URL' or 'nice-looking URL' or 'Speaking URL' or 'user-friendly URL' or 'SEO-friendly URL' from a string. This module aims to transliterate the input string.

For use in browser and server - no dependencies!

NPM Badge

Module Status

Installation

npm

npm install speakingurl --save

yarn

yarn add speakingurl --dev

Bower

bower install --save speakingurl

Component

component install pid/speakingurl

Ruby on Rails

# Add to Gemfile
gem 'speakingurl-rails'

Download Package

copy the file speakingurl.min.js to your script directory

CDN/cloudflare

available versions:

CDN/maxcdn

available versions:

Usage

getSlug(input, [options]);

input: {string} to convert

options {object|string} config object or separator string (see below)

  • options {object}

    • separator {string} default: '-'
      • char that replaces the whitespaces
    • lang {string|boolean} default: 'en' // ISO 639-1 Codes
      • language specific transliteration (
        • 'ar' // Arabic
        • 'az' // Azerbaijani*
        • 'cs' // Czech
        • 'de' // German
        • 'dv' // Divehi
        • 'en' // English
        • 'es' // Spanish
        • 'fa' // Persian
        • 'fi' // Finnish
        • 'fr' // French
        • 'ge' // Georgian
        • 'gr' // Greek*
        • 'hu' // Hungarian
        • 'it' // Italian
        • 'lt' // Lithuanian*
        • 'lv' // Latvian
        • 'my' // Burmese
        • 'mk' // Macedonian*
        • 'nl' // Dutch
        • 'pl' // Polish
        • 'pt' // Portuguese
        • 'ro' // Romanian
        • 'ru' // Russian
        • 'sv' // Swedish
        • 'sk' // Slovak
        • 'sr' // Serbian*
        • 'tr' // Turkish
        • 'uk' // Ukranian
        • 'vn' // Vietnamese
    • symbols {boolean} default: true
      • false -> don't convert symbols
      • true -> convert symbols according to the 'lang' setting
    • maintainCase {boolean} default: false
      • true -> maintain case chars
      • false -> convert all chars to lower case
    • titleCase {boolean|array} default: false
      • true -> convert input string to title-case
      • array -> titlecase = true, but omit the words from in the array
    • truncate {number} default: 0
      • 0 -> don't trim length
      • >= 1 -> trim to max length while not breaking any words
    • uric {boolean} default: false
      • true -> additionally allow chars: ";", "?", ":", "@", "&", "=", "+", "\$", ",", "/"
      • false
    • uricNoSlash {boolean} default: false
      • true -> additionally allow chars: ";", "?", ":", "@", "&", "=", "+", "\$", ","
    • mark {boolean} default: false
      • true -> additionally allow chars: "-", "_", ".", "!", "~", "*", "'", "(", ")"
    • custom {object|array} default: {}
      • object -> custom map for translation, overwrites all i.e. { '&': '#', '*': ' star ' }
      • array -> add chars to allowed charMap (see example)
  • 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 list of allowed characters. The separator-character is always allowed.

Node.js
var getSlug = require('speakingurl');
Browser
<script src="bower_components/speakingurl/speakingurl.min.js"></script>

Ruby on Rails

# Add to application.js
//= require speakingurl

Examples

var slug;

slug = getSlug("Schöner Titel läßt grüßen!? Bel été !");
console.log(slug); // Output: schoener-titel-laesst-gruessen-bel-ete

slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", '*');
console.log(slug); // Output: schoener*titel*laesst*gruessen*bel*ete

slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {
        separator: '_'
    });
console.log(slug); // Output: schoener_titel_laesst_gruessen_bel_ete

slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {
        uric: true
    });
console.log(slug); // Output: schoener-titel-laesst-gruessen?-bel-ete

slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {
        uricNoSlash: true
    });
console.log(slug); // Output: schoener-titel-laesst-gruessen?-bel-ete

slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {
        mark: true
    });
console.log(slug); // Output: schoener-titel-laesst-gruessen!-bel-ete-!

slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {
        truncate: 20
    });
console.log(slug); // Output: schoener-titel

slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {
        maintainCase: true
    });
console.log(slug); // Output: Schoener-Titel-laesst-gruessen-Bel-ete

slug = getSlug("Äpfel & Birnen!", {
        lang: 'de'
    });
console.log(slug); // Output: aepfel-und-birnen

slug = getSlug("မြန်မာ သာဓက", {
        lang: 'my'
    });
console.log(slug); // Output: myanma-thadak

slug = getSlug('މިއަދަކީ ހދ ރީތި ދވހކވ', {
        lang: 'dv'
    });
console.log(slug); // Output: miadhakee hd reethi dvhkv

slug = getSlug("Apple & Pear!", {
        lang: 'en' // lang: "en" is default, just to clarify
    });
console.log(slug); // Output: apple-and-pear

slug = getSlug('Foo & Bar * Baz', {
        custom: {
            '&': ' doo '
        },
        uric:true
    });
console.log(slug); // Output: foo-doo-bar-baz

slug = getSlug('Foo ♥ Bar');
console.log(slug); // Output: foo-love-bar

slug = getSlug('Foo & Bar | (Baz) * Doo', {
        custom: {
            '*': 'Boo'
        },
        mark:true
    });
console.log(slug); // Output: foo-and-bar-or-(baz)-boo-doo

slug = getSlug('Foo and Bar or Baz', {
        custom: {
            'and': 'und',
            'or': ''
        }
    });
console.log(slug); // Output: foo-und-bar-baz

slug = getSlug('[Knöpfe]', {
        custom: [
            '[',
            ']'
        ]
    });
console.log(slug); // Output: [knoepfe]

slug = getSlug('NEXUS4 only $299');
console.log(slug); // Output: nexus-4-only-usd299

slug = getSlug('NEXUS4 only €299', {
        maintainCase: true
    });
console.log(slug); // Output: NEXUS-4-only-EUR299

slug = getSlug('Don\'t drink and drive', {
        titleCase: true
    });
console.log(slug); // Output: Don-t-Drink-And-Drive

slug = getSlug('Don\'t drink and drive', {
        titleCase: ['and']
    });
console.log(slug); // Output: Don-t-Drink-and-Drive

slug = getSlug('Foo & Bar ♥ Foo < Bar', {
        lang: false
    });
console.log(slug); // Output: foo-bar-foo-bar

slug = getSlug('Foo & Bar ♥ Foo < Bar', {
        symbols: false
    });
console.log(slug); // Output: foo-bar-foo-bar

slug = getSlug('ä♥ä', {
        lang: 'tr',
        symbols: false
    });
console.log(slug); // Output: a

createSlug([options])

options: {object|string} config object or separator string (see above)

Create your own specially configured function.

var options = {
        maintainCase: true,
        separator: '_'
    };

var mySlug = require('speakingurl').createSlug(options);
// in browser:
// var mySlug = createSlug(options);

var slug = mySlug("Schöner Titel läßt grüßen!? Bel été !");
console.log(slug); // Output: Schoener_Titel_laesst_gruessen_Bel_ete

Create your own specially configured function with title-case feature.

var options = {
        titleCase: [
            "a","an","and","as","at","but",
            "by","en","for","if","in","nor",
            "of","on","or","per","the","to","vs"
        ]
    };

var mySlug = require('speakingurl').createSlug(options);
// in browser:
// var mySlug = createSlug(options);

var slug = mySlug('welcome to the jungle');
console.log(slug); // Output: Welcome-to-the-Jungle

Changelog

see CHANGELOG.md

Tests

Build Status

npm test

Contribution


# fork pid/speakingurl on Github
$ git clone git@github.com:<YOUR_USER>/speakingurl.git
$ cd speakingurl
$ npm install
# add your stuff
# add tests
# add example for new feature
# add release info to CHANGELOG.md
# add description/example to README.md
$ gulp
$ commit files (speakingurl.min.js,...)
# if everything works fine, commit, push to your repository
# create pull request

Release

$ gulp bumpup --patch  # --minor # --major
$ gulp
$ gulp release

Release to RubyGems.org

$ gulp
$ gem build speakingurl-rails.gemspec
$ gem push speakingurl-rails-x.x.x.gem

References

Use in other environments

Ports

Credits

License

The BSD 3-Clause License (BSD3)

Copyright (c) 2013-2017 Sascha Droste pid@posteo.net 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.

changelog

Changelog

  • v14.0.1 Empty minified file; Thanks to https://github.com/redaxmedia
  • v14.0.0 Add Swedish #101 Thanks to https://github.com/simison
  • v13.0.0 Add Vietnamese missing charMap key #88; Thanks to https://github.com/aduycuong // [Bower] invalid-meta The "main" field cannot contain minified files #91 Thanks to https://github.com/stonio
  • v12.0.0 Georgian Support Added! #95 Thanks to https://github.com/vdanelia and support from https://github.com/redaxmedia // Improved README (yarn install option, https links, etc.) #97 Thanks to https://github.com/apepper // Move static maps outside getSlug (performance) #96 Thanks to https://github.com/kwiatkk1 // Improved docs - lang option can be string or boolean #89 Thanks to https://github.com/stonio
  • v11.0.0 #87 Add Finnish transliteration for symbols; #85 Added new currencies; #84 Update README.md; #83 Various czech language fixes;#82 Add lithuanian transliteration for symbols; Fix: Ω should be transliterated to O
  • v10.0.0 Add persian; Update dependencies
  • v9.0.0 fix of "Title Case error while accent on First Character for each word #72"
  • v8.0.2 fix the fix; problems with release v8.0.1 with building speakingurl-min.js
  • v8.0.1 merge #71 Improve sprockets fix
  • v8.0.0 Cleanup Russian transliteration to ICAO https://en.wikipedia.org/wiki/Romanization_of_Russian
  • v7.0.0 Add Dhivehi translations; thanks to https://github.com/jawish/;
  • v6.0.0 Add Catalan translations #64; thanks to https://github.com/alexandernst/; Add Finnish char map #65; thanks to https://github.com/jmheik/
  • v5.0.1 bugfix #62; thanks to https://github.com/jdiprizio
  • v5.0.0 add Ukranian #60, Latvian #57 symbol translations; update devDependencies
  • v4.0.0 add new 'symbols' option, now you can explicit disable converting symbols, but use 'lang' specific transliteration; add romanian chars/symbol translation #52
  • v3.0.0 add Romanian,Polish,Czech,Serbian,Latvian,Azerbaijani,add support for polish symbols
  • v2.0.0 update charmap with Vietnamese chars) thanks to Lê Anh Tuấn https://github.com/lATAl
  • v1.1.5 cleanup horror
  • v1.1.4 forgot to generate the v1.1.3 minified version, but nevermind, there wasn't any code changes ;-)
  • v1.1.3 cleanup
  • v1.1.2 add Rails asset pipeline support (thanks to @retrorubies / https://github.com/simi\)
  • v1.1.1 cleanup, update dev dependencies
  • v1.1.0 custom config can be an array, all chars are added to allowed chars
  • v1.0.0 fix #47; bumpup to semver ;-)
  • v0.20.0 add support for hungarian #45
  • v0.19.0 add support for burmese #44
  • v0.18.1 cleanup, update dev dependencies
  • v0.18.0 fix #43; add chars to turkish langCharMap; language specific character transliteration
  • v0.17.0 pass global to anonymous function #42
  • v0.16.0 add support for italian
  • v0.15.0 bugfix #38
  • v0.14.0 add support for turkish
  • v0.13.1 after publishing corrected 0.12.5, the 0.13.0 was unpublished WTF?
  • v0.13.0 not published
  • v0.12.5 reverted v0.12.3
  • v0.12.4 deprecated; change to map, mistakenly without bumpup minor version!
  • v0.12.3 switch to gulpjs
  • v0.12.2 corrupt gulpfile, not published to npm registry
  • v0.12.1 update devDep
  • v0.12.0 add support for Czech and Slovak
  • v0.11.0 add support for Vietnamese
  • v0.10.0 changes in greek transliteration mapping table
  • v0.9.1 add support for Dutch
  • v0.9.0 updated dev dependencies; because of dep. we are only compatible with node > 0.10.x
  • v0.8.4 last version compatible with node v0.8.x
  • v0.8.0 add feature to set 'lang' to true or false to deactivate symbol translation
  • v0.7.0 add titleCase feature #26
  • v0.6.0 add symbol translation pt
  • v0.5.0 enhance custom replacement; Add support for "black-listed" words #20 - keep old behavior for single character replacement - add custom replacement for string to string|char|'' // '' ~= remove
  • v0.4.0 add support for Arabic
  • v0.3.0 add Indian Rupee and Pfennig to currency map
  • v0.2.21 fix #17; if symbol at the end of input string
  • v0.2.18 add amd-support