├── .gitignore ├── .jshintignore ├── .gitattributes ├── .travis.yml ├── .editorconfig ├── test.js ├── .jshintrc ├── package.json ├── readme.md ├── license └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.jshintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | test.js 3 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '5' 4 | - '4' 5 | - '0.12' 6 | - '0.10' 7 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import fn from './'; 3 | 4 | test('some beautiful marks', t => { 5 | t.is(fn('a -- b'), 'a \u2013 b'); 6 | t.is(fn('a -- b'), 'a \u2013 b'); 7 | t.is(fn('a --- b (tm) c'), 'a \u2014 b ™ c'); 8 | t.is(fn('a --- "b (tm) c"'), 'a \u2014 “b ™ c”'); 9 | t.is(fn('a --- \'b (tm) c\''), 'a \u2014 ‘b ™ c’'); 10 | t.is(fn('he\'s neat'), 'he\u2019s neat'); 11 | }); 12 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "curly": true, 3 | "eqeqeq": true, 4 | "newcap": true, 5 | "noarg": true, 6 | "noempty": true, 7 | "nonew": true, 8 | "sub": true, 9 | "undef": true, 10 | "unused": true, 11 | "trailing": true, 12 | "boss": true, 13 | "eqnull": true, 14 | "strict": true, 15 | "immed": true, 16 | "expr": true, 17 | "latedef": "nofunc", 18 | "quotmark": "single", 19 | "validthis": true, 20 | "indent": 2, 21 | "node": true, 22 | "browser": true 23 | } 24 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "beautify-text", 3 | "version": "1.0.0", 4 | "description": "Automated typographic quotation and punctuation marks", 5 | "license": "MIT", 6 | "repository": "bevacqua/beautify-text", 7 | "author": { 8 | "name": "Nicolas Bevacqua", 9 | "email": "nicolasbevacqua@gmail.com", 10 | "url": "ponyfoo.com" 11 | }, 12 | "engines": { 13 | "node": ">=0.10.0" 14 | }, 15 | "scripts": { 16 | "test": "jshint . --reporter node_modules/jshint-stylish && ava" 17 | }, 18 | "dependencies": {}, 19 | "devDependencies": { 20 | "ava": "^0.11.0", 21 | "jshint": "2.9.1", 22 | "jshint-stylish": "2.1.0", 23 | "xo": "^0.12.1" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # beautify-text [![Build Status](https://travis-ci.org/bevacqua/beautify-text.svg?branch=master)](https://travis-ci.org/bevacqua/beautify-text) 2 | 3 | > Automated typographic quotation and punctuation marks 4 | 5 | ## install 6 | 7 | ``` 8 | $ npm install --save beautify-text 9 | ``` 10 | 11 | ## features 12 | 13 | Prettifies several typographic marks, some cases are outlined below. 14 | 15 | - Single and double quotes 16 | - Apostrophes 17 | - Marks like `(tm)`, `(c)`, `(r)` and `(p)` into `™`, `©`, `®` and `§` 18 | - Long dashes, like `---` into `—` 19 | - `..`, `...`, etc into `…` (but `?..`, `!..` aren't transformed) 20 | - `+-` into `±` 21 | 22 | ## `beautifyText(text)` 23 | 24 | ```js 25 | beautifyText('a --- "b (tm) c" -- d'); 26 | // -> 'a — “b ™ c” – d' 27 | ``` 28 | 29 | ## license 30 | 31 | MIT © [Nicolas Bevacqua](https://ponyfoo.com) 32 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Nicolas Bevacqua (ponyfoo.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var rquotes = /['"\u2019]/; 4 | var rpunctuation = /\+-|\.\.|\?\?\?\?|!!!!|,,|--/; 5 | var rscoped = /\((c|tm|r|p)\)/ig; 6 | var scoped = { 7 | c: '©', 8 | r: '®', 9 | p: '§', 10 | tm: '™' 11 | }; 12 | 13 | function beautifyQuotes (text) { 14 | if (!rquotes.test(text)) { 15 | return text; 16 | } 17 | return text 18 | .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018') // opening singles 19 | .replace(/'/g, '\u2019') // closing singles & apostrophes 20 | .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c') // opening doubles 21 | .replace(/"/g, '\u201d'); // closing doubles 22 | } 23 | 24 | function replaceScoped (match, name) { 25 | return scoped[name.toLowerCase()]; 26 | } 27 | 28 | function beautifyScoped (text) { 29 | return text.replace(rscoped, replaceScoped); 30 | } 31 | 32 | function beautifyPunctuation (text) { 33 | if (!rpunctuation.test(text)) { 34 | return text; 35 | } 36 | return text 37 | .replace(/\+-/g, '±') 38 | .replace(/\.{2,}/g, '…') // .., ..., ....... -> … 39 | .replace(/([?!])…/g, '$1..') // ?..... & !..... -> ?.. & !.. 40 | .replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ',') 41 | .replace(/(^|[^-])---([^-]|$)/mg, '$1\u2014$2') // em-dash 42 | .replace(/(^|\s)--(\s|$)/mg, '$1\u2013$2') // en-dash 43 | .replace(/(^|[^-\s])--([^-\s]|$)/mg, '$1\u2013$2'); // en-dash 44 | } 45 | 46 | function beautifyText (text) { 47 | return beautifyQuotes(beautifyPunctuation(beautifyScoped(text))); 48 | } 49 | 50 | module.exports = beautifyText; 51 | --------------------------------------------------------------------------------