This module provides helper functions to check for and fix various punctuations or typographic mistakes and enforce proper use of non-breaking spaces in a text.
This is neither a spell-checker nor a hyphenation helper. While it may impact how line will breaks it don't do anything beyond adding non-breaking spaces here and there.
What about HTML entities
Typography-Fixer doesn't use HTML entities, it relies solely on unicode characters. The main reason is that, except for XML reserved characters (
&), no one should no longer care about special characters when using a UTF8 charset. If you don't use UTF8 (seriously?) there's a bunch of modules for that purpose.
The only exception is for the
& entity in html enhancement rules.
npm install --save typography-fixer
When using the package, the two functions you need to care about are
fix. But the packages exposes a total of five methods available using
require('typography-fixer'). On top of that, rulesets can be found at the
typography-fixer/lib/rules path, while ignore sets are located at
const rules = englishRules// returns an array with all the places where rules have been transgressed// or undefined if no rules were broken.const checkResults =// returns the string with all rule violations fixed// in that case it returns: 'Some text “to verify.”'const fixedString =
check function takes an array of rules and ignores, and returns an array of violations to the specified rules. A result is an object with the name of the broken rule and an array representing the range of the match. If a rule matches a part of a text but its application does't change this part then the match is simply ignored.
fix function also takes an array of rules and ignores, and returns the corrected string.
Note that since the
check function operates on a string without modifying it, some fixes won't be detected are they would only be applied after a first batch of rules was applied, and other times several rules can returns violations at the same place, with some of them that won't be applied as they will become irrelevant after some replacements. One example of the latter is the rule for
etc..., it will returns violations for both
etc followed by an ellipsis and for an ellipsis formed with three periods.
Rules And Ignores
typography-fixer works using two kind of entities,
ignores. These objects can be created using the
ignore functions exposed by the package.
rule(name, expression, replacement)– defines a pattern for a class of errors identified with its
name. It returns an
Objectwith the given name, and two functions
check, both taking a string as argument. The
replacementparameter can either be a
Functionto pass to the
For instance, the following code defines a rule that replaces three periods by an ellipsis:
ignore(name, expression, invertRanges)– defines parts of a string where the rules don't apply. It returns an
Objectwith the given name and a
rangesmethod that returns an array of the text ranges to preserve. When the
truethe ranges returned by the
rangesmethod will span every part of the string that are not matched by the expression.
For instance, the following code defines an ignore to preserve inline code blocks in Markdown:
Both functions take an
expression parameter that can be either a
RegExp or a
String. It'll be used to create a regular expression (so take care of escaping backslashes when passing a string).
When passing a regular expression, the
global flag will be automatically defined for the regexes used to scan a string. The
ignoreCase can be freely defined, they will be preserved in the regexes created in the rule's methods.
Rules and ignores can be organized using the last exposed function:
group. This function takes a name and an array of rules and returns a new array. Every rules in the new array will have a name such as
Groups can be be nested, so the following is possible:
// The resulting array will contains one rule with the// name topGroup.nestedGroup.theRule'topGroup'
The result is always a flat array so that we can apply every array operations on a ruleset without having to care about nesting. For instance, excluding rules is as simple as running a filter on the array.