-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
47 lines (38 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*!
* pretty <https://github.com/jonschlinkert/pretty>
*
* Copyright (c) 2013-2015, 2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
const beautify = require('js-beautify');
const condense = require('condense-newlines');
const defaults = {
unformatted: ['code', 'pre', 'em', 'strong', 'span'],
indent_inner_html: true,
indent_char: ' ',
indent_size: 2,
sep: '\n'
};
const 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, '> ');
};
const pretty = (str, options = {}) => {
var opts = { ...defaults, ...options };
str = beautify.html(str, opts);
if (opts.ocd) {
if (opts.newlines) opts.sep = opts.newlines;
return ocd(str, opts);
}
return str;
};
module.exports = pretty