hensei-web/node_modules/htmlnano/lib/modules/minifyJson.js
2020-09-11 06:44:42 -07:00

33 lines
No EOL
614 B
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = minifyJson;
/* Minify JSON inside <script> tags */
function minifyJson(tree) {
// Match all <script> tags which have JSON mime type
tree.match({
tag: 'script',
attrs: {
type: /(\/|\+)json/
}
}, function (node) {
var content = (node.content || []).join('');
if (!content) {
return node;
}
try {
content = JSON.stringify(JSON.parse(content));
} catch (error) {
return node;
}
node.content = [content];
return node;
});
return tree;
}