Automatically replace whitespace in string with non-breaking spaces (\u00A0 or ) to prevent hanging articles, prepositions and digits.
Important: this package does not parse or traverse AST. Using it with raw HTML or Markdown strings will likely break your content or markup.
For rich Markdown content use remark-plugin-autonbsp.
Install with npm or any other package manager:
npm install @dinvader/autonbspyarn add @dinvader/autonbspBasic example:
import { autoNBSP } from '@dinvader/autonbsp';
const inputText = 'An example string with 6 tokens.';
const result = autoNBSP(inputText, {
mode: 'html',
afterDigits: true,
prepositions: ['an', 'with'],
});
console.log(result);
// -> "An example string with 6 tokens.";There are two config presets available for Russian and English languages. You can extend them for your needs.
import { autoNBSP } from '@dinvader/autonbsp';
import nbspEn from '@dinvader/autonbsp/presets/en';
import nbspRu from '@dinvader/autonbsp/presets/ru';
console.log(autoNBSP('5 items in stock', nbspEn));
// -> "5\u00A0items in\u00A0stock"
console.log(autoNBSP('5 items in stock', { ...nbspEn, prepositions: '', mode: 'html' }));
// -> "5 items in stock"
console.log(autoNBSP('5 шт. на складе', nbspEn));
// -> "5\u00A0шт. на\u00A0складе"Defines the replacement used for matched whitespace.
'utf'replaces whitespace with the Unicode non-breaking space character'\u00A0'or.'html'replaces whitespace with the HTML entity string' '.
Default value is 'utf'.
Replace runs of whitespace that occur between two adjacent digits.
Examples:
"123 456"→"123\u00A0456""1\t 2"→"1\u00A02"
false by default.
Replace runs of whitespace that occur immediately after a digit.
truereplaces whitespace when followed by any non-digit character.'before-letter'replaces whitespace only when followed by a Unicode letter.
Examples:
"2 pieces"→"2\u00A0pieces""5 %"→"5\u00A0%""5 %"→"5 %"with'before-letter'option
false by default.
Replace runs of whitespace that occur after specified whole-word prepositions.
Prepositions may be provided as:
- an array of strings (e.g.
['a', 'in', 'for']) - a pipe-separated regular expression pattern (e.g.
'a|in|for')
Matching is case-insensitive and respects word boundaries.