Word wrapping with HTML, ANSI color code, indentation and paragraphing support.
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.
var linewrap = require'linewrap';// Wrap the string at 20 columns, using Windows-style line breaks.var wrap = linewrap20 lineBreak: '\r\n' /*, other options */;console.logwrap'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 = linewrap10 30 skipScheme: 'html' /*, other options */;console.logwrap'You and your <b>whole family</b> are made out of <i>meat</i>.';
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
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:
"bbcode". They are pre-configured regular expressions for common tasks.
When both options are specified,
skip takes precedence.
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 (
and a string that is used as line breaks in the output (
Supported values of
string. It is used as
P2, and a
RegExpobject is created from the string to be used as
[RegExp, string]. The
RegExpobject is used as
P1, and the string is used as
[string, string]. A
RegExpobject is created from the first string and used as
P1, the second string is used as
RegExp. It is used as
P1. We will match the regular expression against the input and use the first match as
P2. If no match is found, an exception is thrown. Not Recommended
You can, for example, use
P2 to convert the string
from one format to another.
lineBreakScheme can take one of the following values:
"xhtml". Each scheme specifies both
P2 for the specific
When both options are specified,
lineBreak takes precedence.
This option controls how to treat existing line breaks in the input. It's important for supporting various paragraphing styles.
"all"Default. All existing line breaks are preserved.
"none". All existing line breaks are discarded.
"multi". Only 2 or more consecutive line breaks (there can be whitespaces 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.
"m<num>". A number is specified to indicate how many consecutive line breaks are preserved. For example,
"multi"is equivalent to
"s<num>". 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.
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.
"default"Default. Both preceding and trailing whitespaces are stripped. This is the most similar to wordwrap's behavior.
"collapse". In addition to
"default", also collapse consecutive whitespaces within each line.
"line". Similar to
"default", but doesn't strip preceding whitespaces of lines preserved from the input (not wrapped by us). This option can be used with the
respectLineBreaksto support the indenting paragraphing style, so that the indentations to mark new paragraphs are preserved.
"all". 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.
"soft"Default. Split chunks by
/(\S+\s+/and don't break up chunks which are longer than the wrap length. So if a single word is longer than the wrap length it will overflow.
"hard". Split chunks with
/\b/and break up chunks longer than the wrap length.
\t characters are replaced with a certain number of spaces before doing
the wrap. This option controls how many spaces to replace a
\t. Default is 4.
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.
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.
Thanks to James Halliday for wordwrap.