condense.js 598 B

12345678910111213141516171819202122
  1. var mout = require('mout');
  2. var leadLinesRegExp = /^\r?\n/;
  3. var multipleLinesRegExp = /\r?\n(\r?\n)+/mg;
  4. function condense(Handlebars) {
  5. Handlebars.registerHelper('condense', function (context) {
  6. var str = context.fn(this);
  7. // Remove multiple lines
  8. str = str.replace(multipleLinesRegExp, '$1');
  9. // Remove leading new lines (while keeping indentation)
  10. str = str.replace(leadLinesRegExp, '');
  11. // Remove trailing whitespaces (including new lines);
  12. str = mout.string.rtrim(str);
  13. return str;
  14. });
  15. }
  16. module.exports = condense;