@maizzle/framework 4.7.2 → 4.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/transformers/prettify.js +38 -4
package/package.json
CHANGED
|
@@ -7,9 +7,25 @@ module.exports = async (html, config = {}, direct = false) => {
|
|
|
7
7
|
const defaultConfig = {
|
|
8
8
|
parser: 'html',
|
|
9
9
|
printWidth: 500,
|
|
10
|
-
embeddedLanguageFormatting: 'off',
|
|
11
10
|
htmlWhitespaceSensitivity: 'ignore',
|
|
12
|
-
xmlMode: get(config, 'posthtml.options.xmlMode', false)
|
|
11
|
+
xmlMode: get(config, 'posthtml.options.xmlMode', false),
|
|
12
|
+
rewriteSelfClosing: true,
|
|
13
|
+
selfClosingTags: [
|
|
14
|
+
'area',
|
|
15
|
+
'base',
|
|
16
|
+
'br',
|
|
17
|
+
'col',
|
|
18
|
+
'embed',
|
|
19
|
+
'hr',
|
|
20
|
+
'img',
|
|
21
|
+
'input',
|
|
22
|
+
'link',
|
|
23
|
+
'meta',
|
|
24
|
+
'param',
|
|
25
|
+
'source',
|
|
26
|
+
'track',
|
|
27
|
+
'wbr'
|
|
28
|
+
]
|
|
13
29
|
}
|
|
14
30
|
|
|
15
31
|
// Don't prettify if not explicitly enabled in config
|
|
@@ -27,9 +43,27 @@ module.exports = async (html, config = {}, direct = false) => {
|
|
|
27
43
|
}
|
|
28
44
|
|
|
29
45
|
const reFormat = (html, config) => {
|
|
30
|
-
if (/<!doctype html>/i.test(html) && !config.xmlMode) {
|
|
31
|
-
html = html.replace(
|
|
46
|
+
if (/<!doctype html>/i.test(html) && !config.xmlMode && config.rewriteSelfClosing) {
|
|
47
|
+
html = html.replace(new RegExp(`<(${config.selfClosingTags.join('|')})\s?([^>]*?)\s?\/>`, 'g'), (match, p1, p2) => {
|
|
48
|
+
return `<${p1}${p2.trimEnd()}>`
|
|
49
|
+
})
|
|
32
50
|
}
|
|
33
51
|
|
|
34
52
|
return html
|
|
53
|
+
// Fix style="" attributes broken down on multiple lines
|
|
54
|
+
.replace(/(\s+style="\s+)([\s\S]*?)(\s+")/g, (match, p1, p2, p3) => {
|
|
55
|
+
return p1.replace(/\n\s+?(style)/g, ' $1').trimEnd()
|
|
56
|
+
+ p2.replace(/\s+/g, ' ').trim()
|
|
57
|
+
+ p3.trim()
|
|
58
|
+
})
|
|
59
|
+
// Fix closing </pre> tags broken down on multiple lines (</pre>\n\s+>)
|
|
60
|
+
.replace(/(<\/pre)\s+>/g, '$1>')
|
|
61
|
+
// Undo escaping of quotes in attribute values
|
|
62
|
+
.replace(/="(.*?)"/g, (match, p1) => {
|
|
63
|
+
return `="${p1.replace(/"/g, '\'')}"`
|
|
64
|
+
})
|
|
65
|
+
// Fix <tag \n\s+{attrs}\n\s+> => <tag {attrs}>
|
|
66
|
+
.replace(/<([^>]+)\n\s*([^>]+)\n\s*>/g, '<$1 $2>')
|
|
67
|
+
// Fix <tag {attrs}\n[\s\t]*> => <tag {attrs}>
|
|
68
|
+
.replace(/<([^>]+)\n\s*>/g, '<$1>')
|
|
35
69
|
}
|