richtypo4 v1.0.2
Richtypo: HTML typography enhancer for Node.js
Richtypo is a tool to prepare your texts to publication on web: apply typography rules like quotes ("
→ “”
), dashes (-
→ —
) and non-breaking spaces (to make text easier to read).
Richtypo comes with typography rules for English, French and Russian, but we encourage you to customize them and make your own rules — they are just JavaScript functions.
Features
- Works with plain text, Markdown or HTML
- Takes care of your HTML tags
- English, French and Russian rules
- Run only rules you need
- Add your own rules
- Works server-side
- No big dependencies
Installation
npm install richtypo
You will probably need to install a rule package for your language, like this:
npm install richtypo-rules-en
But it’s not required, see how to create your own rules.
Rule packages
- richtypo-rules-en: English
- richtypo-rules-fr: French
- richtypo-rules-ru: Russian
Usage
Basic usage
import richtypo from 'richtypo';
import rules from 'richtypo-rules-en';
const text =
'There are 1000 "rules" to enrich your text with Richtypo.';
richtypo(rules, text);
Will produce something like that:
There are 1,000 “rules” to enrich your text with Richtypo.
Note: The default export of
richtypo-rules-en
contains recommended rules but you can import each rule separately, see below.Note:
is actually rendered by Richtypo as the Unicode character for non-breaking-space\xA0
which plays well with any modern browser.Note: Richtypo works better if the input text is cleared from special characters. You can use the he package and its decode function as in
he.decode(text)
for that purpose.
Currying
Richtypo can be curried and used as below:
import richtypo from 'richtypo';
import rules from 'richtypo-rules-en';
const rt = richtypo(rules);
const text =
'There are 1000 "rules" to enrich your text with Richtypo.';
// Will produce the same output as in the previous section
rt(text);
Composition
You can run only rules you need by importing them separately:
import richtypo from 'richtypo';
import { spaces, quotes } from 'richtypo-rules-en';
const text =
'There are 1000 "rules" to enrich your text with RichTypo.';
// this will only run spaces and quotes rules
richtypo([spaces, quotes], text);
Note: Have a look at the example page and its source.
Custom rules
Rules are JavaScript functions that take a string and return a transformed string. For example, a rule that replaces three dots (...
) with an ellipsis (…
) can look like this:
const ellipsis = text => text.replace(new RegExp(/\.{2,}/gim), '…');
And then you use is as any other rule:
import richtypo from 'richtypo';
richtypo(ellipsis, 'Typography everywhere...');
// -> Typography everywhere…
Common rules package
richtypo-rules-common package contains common typography rules that you can use or extend in your own rules.
Definitions
The common rules package exports definitions
, convenience constants that you can use in your own rules. For example, definitions.quotes
is set as '["“”«»‘’]'
, which allows you to write:
import { definitions } from 'richtypo-rules-common';
const quoteToUnderscore = text =>
text.replace(new RegExp(`${definitions.quotes}`, 'gm'), '_');
export default {
quoteToUnderscore
};
Common rules
The common rules package also exports rules that your package can re-use as is.
For example, the ellipsis
rule replaces ...
with …
symbol. Rather than you having to rewrite that rule, you can reexport it as part of your rules.
import { ellipsis } from 'richtypo-rules-common';
// ...
export default {
quoteToUnderscore,
ellipsis
};
Some rules such as the quotes
rule are factory rules and need to be “configured”.
import { ellipsis, quotesFactory } from 'richtypo-rules-common';
// ...
export default {
ellipsis,
quotes: quotesFactory({ openingQuote: '«', closingQuote: '»' })
};
For the complete list of common rules, check out the Readme page of the common rule package.
Testing
Don’t forget to test your rules. Have a look at the English rules package to see how tests are done.
Change log
The change log can be found on the Releases page.
License
The MIT License, see the included License.md file.