46 lines
1.1 KiB
JavaScript
46 lines
1.1 KiB
JavaScript
/*!
|
|
* pretty <https://github.com/jonschlinkert/pretty>
|
|
*
|
|
* Copyright (c) 2013-2015, 2017, Jon Schlinkert.
|
|
* Released under the MIT License.
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var beautify = require('js-beautify');
|
|
var condense = require('condense-newlines');
|
|
var extend = require('extend-shallow');
|
|
var defaults = {
|
|
unformatted: ['code', 'pre', 'em', 'strong', 'span'],
|
|
indent_inner_html: true,
|
|
indent_char: ' ',
|
|
indent_size: 2,
|
|
sep: '\n'
|
|
};
|
|
|
|
module.exports = function pretty(str, options) {
|
|
var opts = extend({}, defaults, options);
|
|
str = beautify.html(str, opts);
|
|
|
|
if (opts.ocd === true) {
|
|
if (opts.newlines) opts.sep = opts.newlines;
|
|
return ocd(str, opts);
|
|
}
|
|
|
|
return str;
|
|
};
|
|
|
|
function ocd(str, options) {
|
|
// Normalize and condense all newlines
|
|
return condense(str, options)
|
|
// Remove empty whitespace the top of a file.
|
|
.replace(/^\s+/g, '')
|
|
// Remove extra whitespace from eof
|
|
.replace(/\s+$/g, '\n')
|
|
|
|
// Add a space above each comment
|
|
.replace(/(\s*<!--)/g, '\n$1')
|
|
// Bring closing comments up to the same line as closing tag.
|
|
.replace(/>(\s*)(?=<!--\s*\/)/g, '> ');
|
|
}
|