index.js
function reflowText (text, width, gfm) {
  // Hard break was inserted by Renderer.prototype.br or is
  // <br /> when gfm is true
  var splitRe = gfm ? HARD_RETURN_GFM_RE : HARD_RETURN_RE,
      sections = text.split(splitRe),
      reflowed = [];

  sections.forEach(function (section) {
    // Split the section by escape codes so that we can
    // deal with them separately.
    var fragments = section.split(/(\u001b\[(?:\d{1,3})(?:;\d{1,3})*m)/g);
    var column = 0;
    var currentLine = '';
    var lastWasEscapeChar = false;

    while (fragments.length) {
      var fragment = fragments[0];

      if (fragment === '') {
        fragments.splice(0, 1);
        lastWasEscapeChar = false;
        continue;
      }

      // This is an escape code - leave it whole and
      // move to the next fragment.
      if (!textLength(fragment)) {
        currentLine += fragment;
        fragments.splice(0, 1);
        lastWasEscapeChar = true;
        continue;
      }

      var words = fragment.split(/[ \t\n]+/);

      for (var i = 0; i < words.length; i++) {
        var word = words[i];
        var addSpace = column != 0;
        if (lastWasEscapeChar) addSpace = false;

        // If adding the new word overflows the required width
        if (column + word.length + addSpace > width) {

          if (word.length <= width) {
            // If the new word is smaller than the required width
            // just add it at the beginning of a new line
            reflowed.push(currentLine);
            currentLine = word;
            column = word.length;
          } else {
            // If the new word is longer than the required width
            // split this word into smaller parts.
            var w = word.substr(0, width - column - addSpace);
            if (addSpace) currentLine += ' ';
            currentLine += w;
            reflowed.push(currentLine);
            currentLine = '';
            column = 0;

            word = word.substr(w.length);
            while (word.length) {
              var w = word.substr(0, width);

              if (!w.length) break;

              if (w.length < width) {
                currentLine = w;
                column = w.length;
                break;
              } else {
                reflowed.push(w);
                word = word.substr(width);
              }
            }
          }
        } else {
          if (addSpace) {
            currentLine += ' ';
            column++;
          }

          currentLine += word;
          column += word.length;
        }

        lastWasEscapeChar = false;
      }

      fragments.splice(0, 1);
    }

    if (textLength(currentLine)) reflowed.push(currentLine);
  });

  return reflowed.join('\n');
}
function reflowText (text, width, gfm) {
  // Hard break was inserted by Renderer.prototype.br or is
  // <br /> when gfm is true
  var splitRe = gfm ? HARD_RETURN_GFM_RE : HARD_RETURN_RE,
      sections = text.split(splitRe),
      reflowed = [];

  sections.forEach(function (section) {
    // Split the section by escape codes so that we can
    // deal with them separately.
    var fragments = section.split(/(\u001b\[(?:\d{1,3})(?:;\d{1,3})*m)/g);
    var column = 0;
    var currentLine = '';
    var lastWasEscapeChar = false;

    while (fragments.length) {
      var fragment = fragments[0];

      if (fragment === '') {
        fragments.splice(0, 1);
        lastWasEscapeChar = false;
        continue;
      }

      // This is an escape code - leave it whole and
      // move to the next fragment.
      if (!textLength(fragment)) {
        currentLine += fragment;
        fragments.splice(0, 1);
        lastWasEscapeChar = true;
        continue;
      }

      var words = fragment.split(/[ \t\n]+/);

      for (var i = 0; i < words.length; i++) {
        var word = words[i];
        var addSpace = column != 0;
        if (lastWasEscapeChar) addSpace = false;

        // If adding the new word overflows the required width
        if (column + word.length + addSpace > width) {

          if (word.length <= width) {
            // If the new word is smaller than the required width
            // just add it at the beginning of a new line
            reflowed.push(currentLine);
            currentLine = word;
            column = word.length;
          } else {
            // If the new word is longer than the required width
            // split this word into smaller parts.
            var w = word.substr(0, width - column - addSpace);
            if (addSpace) currentLine += ' ';
            currentLine += w;
            reflowed.push(currentLine);
            currentLine = '';
            column = 0;

            word = word.substr(w.length);
            while (word.length) {
              var w = word.substr(0, width);

              if (!w.length) break;

              if (w.length < width) {
                currentLine = w;
                column = w.length;
                break;
              } else {
                reflowed.push(w);
                word = word.substr(width);
              }
            }
          }
        } else {
          if (addSpace) {
            currentLine += ' ';
            column++;
          }

          currentLine += word;
          column += word.length;
        }

        lastWasEscapeChar = false;
      }

      fragments.splice(0, 1);
    }

    if (textLength(currentLine)) reflowed.push(currentLine);
  });

  return reflowed.join('\n');
}
function reflowText ( text , width , gfm ) { // Hard break was inserted by Renderer.prototype.br or is // <br /> when gfm is true var splitRe = gfm ? HARD_RETURN_GFM_RE : HARD_RETURN_RE , sections = text . split ( splitRe ) , reflowed = [ ] ; sections . forEach ( function ( section ) { // Split the section by escape codes so that we can // deal with them separately. var fragments = section . split ( / (\u001b\[(?:\d{1,3})(?:;\d{1,3})*m) / g ) ; var column = 0 ; var currentLine = '' ; var lastWasEscapeChar = false ; while ( fragments . length ) { var fragment = fragments [ 0 ] ; if ( fragment === '' ) { fragments . splice ( 0 , 1 ) ; lastWasEscapeChar = false ; continue ; } // This is an escape code - leave it whole and // move to the next fragment. if ( ! textLength ( fragment ) ) { currentLine += fragment ; fragments . splice ( 0 , 1 ) ; lastWasEscapeChar = true ; continue ; } var words = fragment . split ( / [ \t\n]+ / ) ; for ( var i = 0 ; i < words . length ; i ++ ) { var word = words [ i ] ; var addSpace = column != 0 ; if ( lastWasEscapeChar ) addSpace = false ; // If adding the new word overflows the required width if ( column + word . length + addSpace > width ) { if ( word . length <= width ) { // If the new word is smaller than the required width // just add it at the beginning of a new line reflowed . push ( currentLine ) ; currentLine = word ; column = word . length ; } else { // If the new word is longer than the required width // split this word into smaller parts. var w = word . substr ( 0 , width - column - addSpace ) ; if ( addSpace ) currentLine += ' ' ; currentLine += w ; reflowed . push ( currentLine ) ; currentLine = '' ; column = 0 ; word = word . substr ( w . length ) ; while ( word . length ) { var w = word . substr ( 0 , width ) ; if ( ! w . length ) break ; if ( w . length < width ) { currentLine = w ; column = w . length ; break ; } else { reflowed . push ( w ) ; word = word . substr ( width ) ; } } } } else { if ( addSpace ) { currentLine += ' ' ; column ++ ; } currentLine += word ; column += word . length ; } lastWasEscapeChar = false ; } fragments . splice ( 0 , 1 ) ; } if ( textLength ( currentLine ) ) reflowed . push ( currentLine ) ; } ) ; return reflowed . join ( '\n' ) ; }
Munge \n's and spaces in "text" so that the number of characters between \n's is less than or equal to "width".
Munge \ n s and spaces in text so that the number of characters between \ n s is less than or equal to width .