Highlight mistakes and their absence in a piece of text.
Table of Contents
npm install --save highlight-mistakes
I would recommend TypeScript or some other setup that provides auto-completion and type checking.
highlight-mistakes highlights mistakes and their absence in a piece of text based on a given specification.
It is intended as a complementary tool for authoring and proof-reading written text.
highlight-mistakes exports two functions:
proofreadWith, you have to define these things:
- A list of rules describing what should be marked as a mistake and what should be verified as correct.
- A function that highlights a single mistake and one for non-mistakes.
highlightWith can be used to further process the output of
proofreadWith, e.g. to make non-breaking spaces neither marked as correct nor incorrect visible to a human editor.
The canonical example is trying to enforce correct usage of non-breaking spaces and/or non-breaking hyphens, since those are difficult or impossible to tell from their breaking counterparts by visual inspection.
Here, we want non-breaking spaces between numbers and units (just a handful for illustration purposes) – e.g. "144 Hz" or "1 TB" – as well as in phrases like "Core i7".
We also want non-breaking hyphens in "G‑Sync" and phrases like "i7‑8700K".
(The text in question is assumed to be in HTML, hence
That's also why we use
<pre>; otherwise a highlighted space might not be visible at all.)
;;;;;;console.logproofread"G-Sync is from Nvidia, the up to 4.7 GHz Core i7-8700K is from Intel, and 240 Hz monitors are great for gaming.";
In this example, the hyphens in
i7-8700K and the spaces in
Core i7 and
240 Hz are highlighted as mistakes, whereas the non-breaking space in
4.7 GHz is highlighted as verified.
highlight-mistakes is no better than the patterns fed into it.
It's up to the user of this library to define what is correct and incorrect as precisely as possible.
The intention is to aid a human in detecting mistakes; neither soundness nor completeness is probably feasible in most cases.