mjml-migrate
Advanced tools
Comparing version 4.0.5 to 4.1.0-beta.1
@@ -40,2 +40,3 @@ #!/bin/env node | ||
console.log(inputFilename + ' was converted to the MJML 4 syntax in ' + outputFilename); // eslint-disable-line no-console | ||
// eslint-disable-next-line no-console | ||
console.log(inputFilename + ' was converted to the MJML 4 syntax in ' + outputFilename); |
@@ -15,9 +15,11 @@ 'use strict'; | ||
var _extends2 = require('babel-runtime/helpers/extends'); | ||
var _extends3 = _interopRequireDefault(_extends2); | ||
exports.default = migrate; | ||
exports.handleMjml3 = handleMjml3; | ||
exports.default = migrate; | ||
var _fs = require('fs'); | ||
var _lodash = require('lodash'); | ||
var _fs2 = _interopRequireDefault(_fs); | ||
var _mjmlParserXml = require('mjml-parser-xml'); | ||
@@ -33,2 +35,4 @@ | ||
/* eslint-disable no-console */ | ||
function removeContainerTag(bodyTag) { | ||
@@ -45,5 +49,12 @@ bodyTag.attributes = bodyTag.children[0].attributes; | ||
function addPx(value) { | ||
if (!isNaN(value)) { | ||
return value + 'px'; | ||
} | ||
return value; | ||
} | ||
function fixUnits(attribute, value) { | ||
var length = _config.attributesWithUnit.length; | ||
for (var i = 0; i < length; i++) { | ||
for (var i = 0; i < length; i += 1) { | ||
if (_config.attributesWithUnit[i] === attribute) { | ||
@@ -56,14 +67,6 @@ return addPx(value); | ||
function addPx(value) { | ||
if (!isNaN(value)) { | ||
return value + 'px'; | ||
} else { | ||
return value; | ||
} | ||
} | ||
function cleanAttributes(attributes) { | ||
for (var key in attributes) { | ||
(0, _lodash.keys)(attributes).forEach(function (key) { | ||
attributes[key] = fixUnits(key, attributes[key]); | ||
} | ||
}); | ||
return attributes; | ||
@@ -76,4 +79,4 @@ } | ||
var listAllNetworks = function listAllNetworks(tag) { | ||
var attributes = (tag.attributes['display'] || DEFAULT_SOCIAL_DISPLAY).split(' '); | ||
delete tag.attributes['display']; | ||
var attributes = (tag.attributes.display || DEFAULT_SOCIAL_DISPLAY).split(' '); | ||
delete tag.attributes.display; | ||
return attributes; | ||
@@ -83,15 +86,15 @@ }; | ||
var attributes = listAttributes(socialTag); | ||
var networks = listAllNetworks(socialTag); | ||
var networks = listAllNetworks(socialTag); | ||
socialTag.children = []; | ||
// migrate all attributes to their child attributes | ||
for (var network in networks) { | ||
(0, _lodash.keys)(networks).forEach(function (network) { | ||
socialTag.children.push({ | ||
tagName: 'mj-social-element', | ||
attributes: { name: networks[network] }, | ||
content: attributes[networks[network] + '-content'] ? attributes[networks[network] + '-content'] : '' | ||
content: attributes[networks[network] + '-content'] || '' | ||
}); | ||
for (var attribute in attributes) { | ||
(0, _lodash.keys)(attributes).forEach(function (attribute) { | ||
if (attribute.match(networks[network]) && !attribute.match('content')) { | ||
@@ -101,11 +104,11 @@ socialTag.children[network].attributes[attribute.replace(networks[network] + '-', '')] = socialTag.attributes[attribute]; | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
// delete all content attributes from the root tag after they've been migrated | ||
for (var _attribute in attributes) { | ||
if (_attribute.match('content')) { | ||
delete attributes[_attribute]; | ||
(0, _lodash.keys)(attributes).forEach(function (attribute) { | ||
if (attribute.match('content')) { | ||
delete attributes[attribute]; | ||
} | ||
} | ||
}); | ||
@@ -122,26 +125,18 @@ return socialTag; | ||
function migrateHeroSyntax(heroTag) { | ||
var contentAttributes = listAttributes(heroTag.children[0]); | ||
var child = (0, _lodash.find)(heroTag.children, { tagName: 'mj-hero-content' }); | ||
for (var attribute in contentAttributes) { | ||
heroTag.attributes[attribute] = heroTag.children[0].attributes[attribute]; | ||
} | ||
heroTag.children = heroTag.children[0].children; | ||
return heroTag; | ||
return (0, _extends3.default)({}, heroTag, { | ||
children: child.children, | ||
attributes: (0, _extends3.default)({}, heroTag.attributes, child.attributes) | ||
}); | ||
} | ||
function isSupportedTag(tag) { | ||
var length = _config.unavailableTags.length; | ||
for (var i = 0; i < length; i++) { | ||
if (tag === _config.unavailableTags[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
return _config.unavailableTags.indexOf(tag) === -1; | ||
} | ||
function loopThrough(tree) { | ||
for (var key in tree) { | ||
(0, _lodash.keys)(tree).forEach(function (key) { | ||
if (key === 'children') { | ||
for (var i = 0; i < tree.children.length; i++) { | ||
for (var i = 0; i < tree.children.length; i += 1) { | ||
if (isSupportedTag(tree.children[i].tagName)) { | ||
@@ -167,2 +162,3 @@ switch (tree.children[i].tagName) { | ||
break; | ||
// no default | ||
} | ||
@@ -178,4 +174,3 @@ | ||
} | ||
} | ||
return tree; | ||
}); | ||
} | ||
@@ -190,10 +185,2 @@ | ||
function handleMjml3(mjml) { | ||
var isV3Synthax = checkV3Through(mjml); | ||
if (!isV3Synthax) return mjml; | ||
console.log('MJML v3 syntax detected, migrating to MJML v4 syntax. Use mjml -m to get the migrated MJML.'); | ||
return migrate(mjml); | ||
} | ||
var jsonToXML = function jsonToXML(_ref) { | ||
@@ -205,3 +192,3 @@ var tagName = _ref.tagName, | ||
var subNode = children && children.length > 0 ? children.map(jsonToXML).join('\n') : content ? content : ''; | ||
var subNode = children && children.length > 0 ? children.map(jsonToXML).join('\n') : content || ''; | ||
@@ -218,6 +205,16 @@ var stringAttrs = (0, _keys2.default)(attributes).map(function (attr) { | ||
var mjmlJson = (0, _mjmlParserXml2.default)(input, { components: _mjmlCore.components }); | ||
var mjmlJson = (0, _mjmlParserXml2.default)(input, { components: _mjmlCore.components, ignoreIncludes: true }); | ||
loopThrough(mjmlJson); | ||
return jsonToXML(mjmlJson); | ||
} | ||
} | ||
function handleMjml3(mjml) { | ||
var isV3Synthax = checkV3Through(mjml); | ||
if (!isV3Synthax) return mjml; | ||
console.log('MJML v3 syntax detected, migrating to MJML v4 syntax. Use mjml -m to get the migrated MJML.'); | ||
return migrate(mjml); | ||
} | ||
/* eslint-enable no-console */ |
{ | ||
"name": "mjml-migrate", | ||
"version": "4.0.5", | ||
"version": "4.1.0-beta.1", | ||
"description": "A tool to migrate a template from MJML 3 to MJML 4", | ||
@@ -27,5 +27,6 @@ "main": "lib/migrate.js", | ||
"cross-env": "^5.1.4", | ||
"mjml-core": "^4.0.5", | ||
"mjml-parser-xml": "^4.0.5" | ||
"lodash": "^4.17.2", | ||
"mjml-core": "^4.1.0-beta.1", | ||
"mjml-parser-xml": "^4.1.0-beta.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71529
1
5
190
2
+ Addedlodash@^4.17.2
Updatedmjml-core@^4.1.0-beta.1