提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

422 lines
12 KiB

  1. SpeakingURL
  2. ===========
  3. [![Build Status](https://travis-ci.org/pid/speakingurl.svg)](https://travis-ci.org/pid/speakingurl)
  4. [![NPM version](https://badge.fury.io/js/speakingurl.svg)](https://badge.fury.io/js/speakingurl)
  5. [![Bower version](https://badge.fury.io/bo/speakingurl.svg)](https://badge.fury.io/bo/speakingurl)
  6. [![Gem Version](https://badge.fury.io/rb/speakingurl-rails.svg)](https://badge.fury.io/rb/speakingurl-rails)
  7. [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/pid/speakingurl?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
  8. [![Flattr](https://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/thing/1418477/pidspeakingurl-on-GitHub)
  9. > Generate a slug with a lot of options; create a so-called [Semantic URL](https://en.wikipedia.org/wiki/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.
  10. For use in browser and server - no dependencies!
  11. ##### NPM Badge
  12. [![Module Status](https://nodei.co/npm/speakingurl.png?downloads=true&stars=true)](https://npmjs.org/package/speakingurl)
  13. Installation
  14. ------------
  15. #### [npm](https://npmjs.org/package/speakingurl)
  16. ```shell
  17. npm install speakingurl --save
  18. ```
  19. #### [yarn](https://yarnpkg.com)
  20. ```shell
  21. yarn add speakingurl --dev
  22. ```
  23. #### [Bower](https://bower.io)
  24. ```shell
  25. bower install --save speakingurl
  26. ```
  27. #### [Component](https://github.com/component/component)
  28. ```shell
  29. component install pid/speakingurl
  30. ```
  31. #### [Ruby on Rails](http://rubyonrails.org/)
  32. ```ruby
  33. # Add to Gemfile
  34. gem 'speakingurl-rails'
  35. ```
  36. #### [Download Package](https://github.com/pid/speakingurl/releases)
  37. copy the file speakingurl.min.js to your script directory
  38. #### [CDN/cloudflare](https://www.cloudflare.com/)
  39. available versions:
  40. - [www.cdnjs.com/libraries/speakingurl/](https://cdnjs.com/libraries/speakingurl/)
  41. - use [//cdnjs.cloudflare.com/ajax/libs/speakingurl/13.0.0/speakingurl.min.js](https://cdnjs.cloudflare.com/ajax/libs/speakingurl/13.0.0/speakingurl.min.js)
  42. #### [CDN/maxcdn](https://www.maxcdn.com/)
  43. available versions:
  44. - [www.jsdelivr.com/#!speakingurl](https://www.jsdelivr.com/#!speakingurl)
  45. - use [//cdn.jsdelivr.net/speakingurl/13.0.0/speakingurl.min.js](https://cdn.jsdelivr.net/speakingurl/13.0.0/speakingurl.min.js)
  46. Usage
  47. -----
  48. ### getSlug(input, [options]);
  49. `input`: {string} to convert
  50. `options` {object|string} config object or separator string (see below)
  51. - `options` {object}
  52. - `separator` {string} default: '-'
  53. - char that replaces the whitespaces
  54. - `lang` {string|boolean} default: 'en' // ISO 639-1 Codes
  55. - language specific transliteration (
  56. - 'ar' // Arabic
  57. - 'az' // Azerbaijani\*
  58. - 'cs' // Czech
  59. - 'de' // German
  60. - 'dv' // Divehi
  61. - 'en' // English
  62. - 'es' // Spanish
  63. - 'fa' // Persian
  64. - 'fi' // Finnish
  65. - 'fr' // French
  66. - 'ge' // Georgian
  67. - 'gr' // Greek\*
  68. - 'hu' // Hungarian
  69. - 'it' // Italian
  70. - 'lt' // Lithuanian\*
  71. - 'lv' // Latvian
  72. - 'my' // Burmese
  73. - 'mk' // Macedonian\*
  74. - 'nl' // Dutch
  75. - 'pl' // Polish
  76. - 'pt' // Portuguese
  77. - 'ro' // Romanian
  78. - 'ru' // Russian
  79. - 'sv' // Swedish
  80. - 'sk' // Slovak
  81. - 'sr' // Serbian\*
  82. - 'tr' // Turkish
  83. - 'uk' // Ukranian
  84. - 'vn' // Vietnamese
  85. - `symbols` {boolean} default: true
  86. - false -> don't convert symbols
  87. - true -> convert symbols according to the 'lang' setting
  88. - `maintainCase` {boolean} default: false
  89. - true -\> maintain case chars
  90. - false -\> convert all chars to lower case
  91. - `titleCase` {boolean|array} default: false
  92. - true -\> convert input string to title-case
  93. - array -\> titlecase = true, but omit the words from in the array
  94. - `truncate` {number} default: 0
  95. - 0 -\> don't trim length
  96. - \>= 1 -\> trim to max length while not breaking any words
  97. - `uric` {boolean} default: false
  98. - true -\> additionally allow chars: ";", "?", ":", "@", "&", "=", "+", "\$", ",", "/"
  99. - false
  100. - `uricNoSlash` {boolean} default: false
  101. - true -\> additionally allow chars: ";", "?", ":", "@", "&", "=", "+", "\$", ","
  102. - `mark` {boolean} default: false
  103. - true -\> additionally allow chars: "-", "\_", ".", "!", "~", "\*", "'", "(", ")"
  104. - `custom` {object|array} default: {}
  105. - object -\> custom map for translation, overwrites all i.e. { '&': '\#', '\*': ' star ' }
  106. - array -\> add chars to allowed charMap (see example)
  107. - `options` {string} separator
  108. 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.
  109. ##### Node.js
  110. ```js
  111. var getSlug = require('speakingurl');
  112. ```
  113. ##### Browser
  114. ```html
  115. <script src="bower_components/speakingurl/speakingurl.min.js"></script>
  116. ```
  117. #### Ruby on Rails
  118. ```ruby
  119. # Add to application.js
  120. //= require speakingurl
  121. ```
  122. #### Examples
  123. ```js
  124. var slug;
  125. slug = getSlug("Schöner Titel läßt grüßen!? Bel été !");
  126. console.log(slug); // Output: schoener-titel-laesst-gruessen-bel-ete
  127. slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", '*');
  128. console.log(slug); // Output: schoener*titel*laesst*gruessen*bel*ete
  129. slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {
  130. separator: '_'
  131. });
  132. console.log(slug); // Output: schoener_titel_laesst_gruessen_bel_ete
  133. slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {
  134. uric: true
  135. });
  136. console.log(slug); // Output: schoener-titel-laesst-gruessen?-bel-ete
  137. slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {
  138. uricNoSlash: true
  139. });
  140. console.log(slug); // Output: schoener-titel-laesst-gruessen?-bel-ete
  141. slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {
  142. mark: true
  143. });
  144. console.log(slug); // Output: schoener-titel-laesst-gruessen!-bel-ete-!
  145. slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {
  146. truncate: 20
  147. });
  148. console.log(slug); // Output: schoener-titel
  149. slug = getSlug("Schöner Titel läßt grüßen!? Bel été !", {
  150. maintainCase: true
  151. });
  152. console.log(slug); // Output: Schoener-Titel-laesst-gruessen-Bel-ete
  153. slug = getSlug("Äpfel & Birnen!", {
  154. lang: 'de'
  155. });
  156. console.log(slug); // Output: aepfel-und-birnen
  157. slug = getSlug("မြန်မာ သာဓက", {
  158. lang: 'my'
  159. });
  160. console.log(slug); // Output: myanma-thadak
  161. slug = getSlug('މިއަދަކީ ހދ ރީތި ދވހކވ', {
  162. lang: 'dv'
  163. });
  164. console.log(slug); // Output: miadhakee hd reethi dvhkv
  165. slug = getSlug("Apple & Pear!", {
  166. lang: 'en' // lang: "en" is default, just to clarify
  167. });
  168. console.log(slug); // Output: apple-and-pear
  169. slug = getSlug('Foo & Bar * Baz', {
  170. custom: {
  171. '&': ' doo '
  172. },
  173. uric:true
  174. });
  175. console.log(slug); // Output: foo-doo-bar-baz
  176. slug = getSlug('Foo ♥ Bar');
  177. console.log(slug); // Output: foo-love-bar
  178. slug = getSlug('Foo & Bar | (Baz) * Doo', {
  179. custom: {
  180. '*': 'Boo'
  181. },
  182. mark:true
  183. });
  184. console.log(slug); // Output: foo-and-bar-or-(baz)-boo-doo
  185. slug = getSlug('Foo and Bar or Baz', {
  186. custom: {
  187. 'and': 'und',
  188. 'or': ''
  189. }
  190. });
  191. console.log(slug); // Output: foo-und-bar-baz
  192. slug = getSlug('[Knöpfe]', {
  193. custom: [
  194. '[',
  195. ']'
  196. ]
  197. });
  198. console.log(slug); // Output: [knoepfe]
  199. slug = getSlug('NEXUS4 only $299');
  200. console.log(slug); // Output: nexus-4-only-usd299
  201. slug = getSlug('NEXUS4 only €299', {
  202. maintainCase: true
  203. });
  204. console.log(slug); // Output: NEXUS-4-only-EUR299
  205. slug = getSlug('Don\'t drink and drive', {
  206. titleCase: true
  207. });
  208. console.log(slug); // Output: Don-t-Drink-And-Drive
  209. slug = getSlug('Don\'t drink and drive', {
  210. titleCase: ['and']
  211. });
  212. console.log(slug); // Output: Don-t-Drink-and-Drive
  213. slug = getSlug('Foo & Bar ♥ Foo < Bar', {
  214. lang: false
  215. });
  216. console.log(slug); // Output: foo-bar-foo-bar
  217. slug = getSlug('Foo & Bar ♥ Foo < Bar', {
  218. symbols: false
  219. });
  220. console.log(slug); // Output: foo-bar-foo-bar
  221. slug = getSlug('ä♥ä', {
  222. lang: 'tr',
  223. symbols: false
  224. });
  225. console.log(slug); // Output: a
  226. ```
  227. ### createSlug([options])
  228. `options`: {object|string} config object or separator string (see above)
  229. Create your own specially configured function.
  230. ```js
  231. var options = {
  232. maintainCase: true,
  233. separator: '_'
  234. };
  235. var mySlug = require('speakingurl').createSlug(options);
  236. // in browser:
  237. // var mySlug = createSlug(options);
  238. var slug = mySlug("Schöner Titel läßt grüßen!? Bel été !");
  239. console.log(slug); // Output: Schoener_Titel_laesst_gruessen_Bel_ete
  240. ```
  241. Create your own specially configured function with title-case feature.
  242. ```js
  243. var options = {
  244. titleCase: [
  245. "a","an","and","as","at","but",
  246. "by","en","for","if","in","nor",
  247. "of","on","or","per","the","to","vs"
  248. ]
  249. };
  250. var mySlug = require('speakingurl').createSlug(options);
  251. // in browser:
  252. // var mySlug = createSlug(options);
  253. var slug = mySlug('welcome to the jungle');
  254. console.log(slug); // Output: Welcome-to-the-Jungle
  255. ```
  256. Changelog
  257. ---------
  258. see [CHANGELOG.md](https://raw.github.com/pid/speakingurl/master/CHANGELOG.md)
  259. Tests
  260. -----
  261. [![Build Status](https://travis-ci.org/pid/speakingurl.svg)](https://travis-ci.org/pid/speakingurl)
  262. ```shell
  263. npm test
  264. ```
  265. Contribution
  266. ------------
  267. ```shell
  268. # fork pid/speakingurl on Github
  269. $ git clone git@github.com:<YOUR_USER>/speakingurl.git
  270. $ cd speakingurl
  271. $ npm install
  272. # add your stuff
  273. # add tests
  274. # add example for new feature
  275. # add release info to CHANGELOG.md
  276. # add description/example to README.md
  277. $ gulp
  278. $ commit files (speakingurl.min.js,...)
  279. # if everything works fine, commit, push to your repository
  280. # create pull request
  281. ```
  282. Release
  283. -------
  284. ```shell
  285. $ gulp bumpup --patch # --minor # --major
  286. $ gulp
  287. $ gulp release
  288. ```
  289. Release to RubyGems.org
  290. -----------------------
  291. ```shell
  292. $ gulp
  293. $ gem build speakingurl-rails.gemspec
  294. $ gem push speakingurl-rails-x.x.x.gem
  295. ```
  296. References
  297. ----------
  298. - https://tools.ietf.org/html/rfc3986
  299. - https://en.wikipedia.org/wiki/Transliteration
  300. Use in other environments
  301. -------------------------
  302. - [SpeakingURL with AngularJS](https://github.com/zappan/angular-speakingurl)
  303. - [SpeakingURL with Meteor](https://github.com/ongoworks/meteor-speakingurl)
  304. - [SpeakingURL with Ruby on Rails](https://rubygems.org/gems/speakingurl-rails)
  305. Ports
  306. -----
  307. - Java https://github.com/Weltraumschaf/speakingurl thanks to[@Weltraumschaf](https://github.com/Weltraumschaf/)
  308. Credits
  309. -------
  310. - [@dypsilon](https://github.com/dypsilon/)
  311. - [@simov](https://github.com/simov/)
  312. - [@henrikjoreteg](https://github.com/henrikjoreteg/)
  313. [License](https://raw.github.com/pid/speakingurl/master/LICENSE)
  314. ----------------------------------------------------------------
  315. The BSD 3-Clause License (BSD3)
  316. Copyright (c) 2013-2017 Sascha Droste pid@posteo.net All rights reserved.
  317. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  318. - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  319. - 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.
  320. - 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.
  321. 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.