richtypo
TypeScript icon, indicating that this package has built-in type declarations

5.1.3 • Public • Published

Richtypo: HTML typography enhancer for Node.js

npm Build Status Codecov

Richtypo prepares your texts to publication on web: applies 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 you can customize them and create your own rules — each rule is an independent JavaScript function.

Have a look at the example page and its source.

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

These packcages contain recommended typography rules for different languages:

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 works well with any modern browser. We use   in the examples to make it visible.

Note: Richtypo works better when the input text is cleared from special characters. Use the he package and its decode function to remove special characters.

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

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 it 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 you can reuse 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.

Authors and license

Artem Sapegin and contributors.

MIT License, see the included License.md file.

Package Sidebar

Install

npm i richtypo

Weekly Downloads

232

Version

5.1.3

License

MIT

Unpacked Size

9.68 kB

Total Files

5

Last publish

Collaborators

  • sapegin