Skip to content

ovlb/typographizer-js

Repository files navigation

Logo of Typographizer JS

TypographizerJS

Published version Code Coverage Build Status Dependency Status Dev Dependency Status Greenkeeper badge

Typographizer automates everything some things you don’t want (or need) to know about typography.


Acknowledgement

The JS version is inspired by the Typographizer by Frank Rausch, written in Swift. 🙇‍

Usage

Here be dragons 🐉

This package uses RegExp lookbehinds. These are not universally supported. You can use it in Node >=6.4 with enabled --harmony flag, without a flag in Node >=9.11.

You should not use this in the browser (probably). Formatting and reinserting long strings is a mayor performance hit.

In general, this is more of a fun project. So, use with caution.

Installation

npm install typographizer-js

If you are using Yarn:

yarn add typographizer-js

Import the bundle into your project:

const TypographizerJS = require('typographizer-js')

And crate a new instance:

const Typographizer = new TypographizerJS()

Note: Typographizer will preserve inline HTML Tags (a, strong, em), but should not be used to format large bunches of source code.

Once you have your Typographizer instance available you can format strings using the typographize() method.

typographize returns a Promise. So you need to either await the return value or use .then().

const str = `I introduced myself, "Hi, I'm Oscar".`

/**
 * Using await
 */
const formatted = await Typographizer.typographize(str)

console.log(formatted)
// I introduced myself, “Hi, I’m Oscar“.

/**
 * Using .then()
 */
Typographizer.typographize(str)
  .then((str) => {
    console.log(str)
    // I introduced myself, “Hi, I’m Oscar“.
  })

Typographizer uses a bunch of different methods under the hood. All of them can be called on their own. For details, take a look at the API section below.

Options

By default quotes will be formatted to match en_US style. If you need different formatting, pass a language code while initializing.

const Typographizer = new TypographizerJS('fr')

You can find a list of supported languages further down this file.

⚠️ To avoid side effects and confusion, It is not possible to change these options after you created your instance. If you want to format text in multiple languages, you need to create multiple instances.

API

All these methods return Promises. Usage instructions for typographize usage apply.

Methods

Method Description
fixApostroph(str) Replace ', ´ and ` used as an apostroph
fixEllipsis(str) Formats all occurences of ... to use the correct … character
fixAiryPunctuation(str) Finds punctuation that is surrounded by whitespace
formatQuotes(str) Format quotes, but not whitespace or apostrophes
formatOpeningQuotes(str) Format all opening quotes
formatClosingQuotes(str) Format all closing quotes
trimWhitespace(str) Remove whitespace from the beginning and end of a string, replaces multiple spaces with a single one
typographize(str) Takes a string and applies all optimizations

Supported languages

Language support has been ported from the original Typographizer.

Supported languages are:

Language Shortcode
Bosnian bs
Czech cz
Danish da
Dutch, Flemish nl
British English en_GB
American English (default) en_US
Estonian es
Finnish fi
French fr
German de
German (Suisse) de_CH
German (Liechtenstein) de_LI
Hungarian hu
Icelandic is
Lithuanian lt
Latvian lv
Polish pl
Romanian ro
Slovak sk
Slovenian sl
Swedish sw

This list is missing Japanese and Hebrew, which are supported by Typographizer. Hebrew is missing due to complicated support. Japanese requires extended Unicode checks.

About

A script to fix typographic errors

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •