Word wrapping with HTML, ANSI color code, indentation and paragraphing support.
npm install linewraplinewrap
========
A fork of wordwrap that's faster and more powerful,
supporting HTML, ANSI Color Codes, multiple paragraphing styles, and more.
On a 3.4GHz Sandy Bridge core, Linewrap achieves roughly 20MB/s when wrapping
at 80 columns, or 15MB/s if wrapping at 20 columns.
Linewrap is almost backwards compatible with wordwrap. The behavior only differs
in some edge cases where I believe wordwrap didn't make the best choice. You probably
won't notice any difference in normal usage.
Usage
=====
``js
var linewrap = require('linewrap');
// Wrap the string at 20 columns, using Windows-style line breaks.
var wrap = linewrap(20, {lineBreak: '\r\n' /, other options /});
console.log(wrap('You and your whole family are made out of meat.'));
// Wrap the string at 20 columns, prepend 10 spaces to each line, and
// skip HTML tags when counting columns for wrapping.
var wrap = linewrap(10, 30, {skipScheme: 'html' /, other options /});
console.log(wrap('You and your whole family are made out of meat.'));
`
Options
=======
Skip strings
------------
Relevant options: skip, skipScheme.
Sometimes certain characters in the text are used to control styling, annotate
additional information, etc, and are not intended to be displayed. Examples
include HTML tags and ANSI color codes. These characters shouldn't be counted
when doing a wrap.
Supported values of skip:
1. RegExp.string
2. .
The specified regular expression or string is matched against the input, and all
matching sequences in the input are simply copied to the output and are ignored
by the wrapping algorithm.
skipScheme can take one of the following values: "ansi-color", "html", and"bbcode". They are pre-configured regular expressions for common tasks.
When both options are specified, skip takes precedence.
Line break strings
------------------
Relevant options: lineBreak, lineBreakScheme.
To support custom line breaks, there are actually two parameters that need to be
specified: a regular expression that is used to match line breaks in the input (P1),P2
and a string that is used as line breaks in the output ().
Supported values of lineBreak:
1. string. It is used as P2, and a RegExp object is created from the stringP1
to be used as .[RegExp, string]
2. . The RegExp object is used as P1, and the string is usedP2
as .[string, string]
3. . A RegExp object is created from the first string and usedP1
as , the second string is used as P2.RegExp
4. . It is used as P1. We will match the regular expression against theP2
input and use the first match as . If no match is found, an exception is
thrown. Not Recommended
You can, for example, use /\n/ as P1 and "
" as P2 to convert the string
from one format to another.
lineBreakScheme can take one of the following values: "unix", "dos", "mac","html", and "xhtml". Each scheme specifies both P1 and P2 for the specific
scenario.
When both options are specified, lineBreak takes precedence.
Existing line breaks
--------------------
Relevant option: respectLineBreaks.
This option controls how to treat existing line breaks in the input. It's important
for supporting various paragraphing styles.
Supported values:
1. "all" Default. All existing line breaks are preserved."none"
2. . All existing line breaks are discarded."multi"
3. . Only 2 or more consecutive line breaks (there can be whitespaces"m
between them) are preserved, single line breaks are discarded. This can be
used to support the paragraphing style that inserts a blank line between
paragraphs, so that each paragraph is re-formatted, but the paragraph structure
is preserved.
4. . A number is specified to indicate how many consecutive line breaks"multi"
are preserved. For example, is equivalent to "m2"."s
5. . A number is specified to indicate line breaks that are immediately
followed by at least how many whitespaces are preserved. This can be used to
support the paragraphing style that indents the first line of each paragraph.
Whitespaces
-----------
Relevant option: whitespace.
This option controls whether preceding and trailing whitespaces are stripped from
the output. The original wordwrap isn't consistent in this area: it strips preceding
whitespaces of all lines except the first one, and it strips trailing whitespaces of
some lines but not others.
Supported values:
1. "default" Default. Both preceding and trailing whitespaces are stripped."collapse"
This is the most similar to wordwrap's behavior.
2. . In addition to "default", also collapse consecutive whitespaces"line"
within each line.
3. . Similar to "default", but doesn't strip preceding whitespaces of"s
lines preserved from the input (not wrapped by us). This option can be used
with the options of respectLineBreaks to support the indenting"all"
paragraphing style, so that the indentations to mark new paragraphs are preserved.
4. . All whitespaces are preserved. In this mode, whitespaces are treated
like other non-alphabetical characters that are displayed but can be wrapped
at any position.
Hard wrapping
-------------
Relevant option: mode.
Supported values:
1. "soft" Default. Split chunks by /(\S+\s+/ and don't break up chunks which are"hard"
longer than the wrap length. So if a single word is longer than the wrap
length it will overflow.
2. . Split chunks with /\b/ and break up chunks longer than the wrap
length.
Tab width
---------
Relevant option: tabWidth.
All \t characters are replaced with a certain number of spaces before doing\t
the wrap. This option controls how many spaces to replace a . Default is 4.
Presets
-------
Relevant option: preset.
Are you overwhelmed by the sheer amount of options? Worry not, presets are to
the rescue!
Each preset contains values for one or more options. You can specify either a
single preset or an array of presets. If multiple presets in the array set the
same option, the last one wins.
Supported values:
1. "html". Sets skipScheme and lineBreakScheme to "html", and whitespace"collapse"`.
to
Options that are set explicitly take predence to those set by a preset.
You are welcome to suggest new schemes and presets by
creating an issue.
Acknowledgements
================
Thanks to James Halliday for wordwrap.
License
=======
MIT License