index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*!
  2. * pretty <https://github.com/jonschlinkert/pretty>
  3. *
  4. * Copyright (c) 2013-2015, 2017, Jon Schlinkert.
  5. * Released under the MIT License.
  6. */
  7. 'use strict';
  8. var beautify = require('js-beautify');
  9. var condense = require('condense-newlines');
  10. var extend = require('extend-shallow');
  11. var defaults = {
  12. unformatted: ['code', 'pre', 'em', 'strong', 'span'],
  13. indent_inner_html: true,
  14. indent_char: ' ',
  15. indent_size: 2,
  16. sep: '\n'
  17. };
  18. module.exports = function pretty(str, options) {
  19. var opts = extend({}, defaults, options);
  20. str = beautify.html(str, opts);
  21. if (opts.ocd === true) {
  22. if (opts.newlines) opts.sep = opts.newlines;
  23. return ocd(str, opts);
  24. }
  25. return str;
  26. };
  27. function ocd(str, options) {
  28. // Normalize and condense all newlines
  29. return condense(str, options)
  30. // Remove empty whitespace the top of a file.
  31. .replace(/^\s+/g, '')
  32. // Remove extra whitespace from eof
  33. .replace(/\s+$/g, '\n')
  34. // Add a space above each comment
  35. .replace(/(\s*<!--)/g, '\n$1')
  36. // Bring closing comments up to the same line as closing tag.
  37. .replace(/>(\s*)(?=<!--\s*\/)/g, '> ');
  38. }