📜 ⬆️ ⬇️

Profit about the printer

There was a time when every web developer wrote his typographer or thought about it. I did not become an exception and wrote a typographer in PHP. But how to make a typographer without the normal support of UTF-8, at the time I did not imagine, eventually abandoned the idea.

Time passed, but the idea of ​​creating a typographer did not leave, and I decided to write it in JavaScript.


I started with the basic requirements:

I quickly came to the modularity of the rules in the printer, one rule - one function - one file, all the rules are divided into groups .
')
Add your rule is very simple:
Typograf.rule({ name: 'common/other/emoji', handler: function (text) { return text.replace(/:-\)/g, '\uD83D\uDE0A'); } }); 

The typographer’s core just runs the rule-functions , passing the processed text from rule to rule.
There are queues, because some rules must be run at the very beginning of word processing, some at the end.
There are indexes for the rules, in which order in the queue to start the rule, this allows to reduce the complexity of regular expressions.

Rules can be turned on and off. Some rules are disabled by default, for example, hanging punctuation.

Fast start


In the browser


 npm i typograf 

 <script src="./node_modules/dist/typograf.min.js"></script> <script> var tp = new Typograf({lang: 'ru'}); alert(tp.execute('  -  !! ')); </script> 

In Node.js


 npm i typograf 

 var Typograf = require('typograf'), tp = new Typograf({lang: 'ru'}); tp.enable('ru/optalign/*'); //    console.log(tp.execute('  -  !! ')); 


CLI


Install:
 npm i typograf -g 


Typing text according to the Russian rules:
 typograf -l ru my_file.txt > my_new_file.txt 


Typography use options:

Each typographer has errors after typography. The more text tests and users, the better the typographer, which means comments and pull requests on Github are welcome.

Source: https://habr.com/ru/post/266563/


All Articles