1.0.0-alpha.7 • Published 2 years ago

@liquid-labs/wrap-text v1.0.0-alpha.7

Weekly downloads
-
License
UNLICENSED
Repository
github
Last release
2 years ago

liquid-labs/wrap-text

Wraps text for any width while respecting potentially invisible tags and smartly indenting lists.

Usage

Install:

npm i @liquid-labs/wrap-text

Code:

import { wrap } from '@liquid-labs/wrap-text'
//                      0        1         2         3         4         5         6
//                      12345678901234567890123456789012345678901234567890123456789012
const someTaggedText = "Hey! Here's some <i>text</i> with <em>tags</em> embedded in it."
console.log('Default wrapping:\n')
console.log(wrap(someTaggedText, { width: 40 }))

console.log('Tag-ignoring wrapping:\n')
console.log(wrap(someTaggedText, { ignoreTags: true, width: 40 }))

See Examples section for output.

Examples

The numbers are given as a visual aid, only the text is actually printed.

Given text:

Hey! Here's some <i>text</i> with <em>tags</em> embedded in it.

Basic wrapping: wrap(text, { width: 40 }) yields:

0        1         2         3         4
1234567890123456789012345678901234567890
Hey! Here's some <i>text</i> with
<em>tags</em> embedded in it.
  • A width of 0 means to set the wrapping to process.stdout.columns if defined, and the default (80) otherwise.
  • A width of -1 means no wrapping at all.

Tag ignoring wrapping: wrap(text, { ignoreTags: true, width: 40 }) yields:

0        1            2                 3              4
12345678901234567   8901     234567    8901     234567890
Hey! Here's some <i>text<rst> with <em>tags<rst>
embedded in it.
  • Tags are simple and cannot contain any spaces. I.e., these are not full HTML/XML tags.
  • The tags wrapping is meant to be compitible with @liquid-labs/terminal-text.

Given text:

- We'll indent the entire item to match the list.
  - And same goes for this sub item!
Now back to normal.

Smart list indenting: wrap(text, { smartIndent: true, width: 30 }) yields:

0        1         2         3
123456789012345678901234567890
- We'll indent the entire item
  to match the list.
  - And same goes for this sub 
    item!
Now back to normal.

Of course you can combine smartIndent with ignoreTags.