nbsp

Replaces normal spaces with non-breaking ones


License
MIT
Install
npm install nbsp@1.0.2

Documentation

nbsp

Nbsp is a JavaScript module that replaces normal spaces with non-breaking ones. The configuration allows to declare a list of words/characters which trailing space will be replaced with non-breaking one. It's also possible to replace spaces after strings that are within a chars length range.

Installation

Install with npm:

npm install nbsp

Install with yarn:

yarn add nbsp

Usage

Basic usage

import nbsp from 'nbsp'

document.addEventListener('DOMContentLoaded', () => {
  const textElement = document.querySelector('p')
  nbsp(textElement)
})

Parameters

Name Description Type Default
text a source element or a plain text HTMLelement, String -
opts - Object -
opts.words replace a space after any of this words/characters Array []
opts.min replace a space after any string within the min-max range Number 0
opts.max replace a space after any string within the min-max range Number 0
opts.removeWidows replace a space before last word Boolean false
import nbsp from 'nbsp'

document.addEventListener('DOMContentLoaded', () => {
  const textElement = document.querySelector('p')
  
  nbsp(textElement, {
    min: 1,
    max: 1,
    removeWidows: true
  })
})

If you don't want to modify element's node values you can pass element's content as the text argument.

import nbsp from 'nbsp'

document.addEventListener('DOMContentLoaded', () => {
  const textContent = document.querySelector('p').innerHTML
  
  const result = nbsp(textContent, {
    min: 1,
    max: 1,
    removeWidows: true
  })
})

License

nbsp.js is licensed under MIT license.