forked from mjmlio/mjml2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
159 lines (124 loc) · 3.32 KB
/
parser.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import _ from 'lodash'
import htmlparser from 'htmlparser2'
import './libs/mjml'
import MJMLElements from 'mjml-core/lib/MJMLElementsCollection'
const CDATASections = []
const MJElements = []
_.forEach({
...MJMLElements,
}, (element, name) => {
const tagName = element.tagName || name
if (element.endingTag) {
CDATASections.push(tagName)
}
MJElements.push(tagName)
})
function cleanNode (node) {
delete node.parent
// delete children if needed
if (node.children.length) {
node.children.forEach(cleanNode)
} else {
delete node.children
}
// delete attributes if needed
if (Object.keys(node.attributes).length === 0) {
delete node.attributes
}
}
/**
* Avoid htmlparser to parse ending tags
*/
function addCDATASection (content) {
const regexTag = tag => new RegExp(`<${tag}([^>\/]*)>([^]*?)</${tag}>`, 'gmi')
const replaceTag = tag => `<${tag}$1><![CDATA[$2]]></${tag}>`
_.forEach(CDATASections, tag => {
content = content.replace(regexTag(tag), replaceTag(tag))
})
return content
}
function parseAttributes (content) {
const regexTag = tag => new RegExp(`<${tag}(\\s("[^"]*"|'[^']*'|[^'">])*)?>`, 'gmi')
const regexAttributes = /(\S+)\s*?=\s*([\'"])(.*?|)\2/gmi
_.forEach(MJElements, tag => {
content = content.replace(regexTag(tag), contentTag => (
contentTag.replace(regexAttributes, (match, attr, around, value) => `${attr}=${around}${encodeURIComponent(value)}${around}`)
))
})
return content
}
/**
* Convert "true" and "false" string attributes values
* to corresponding Booleans
*/
function convertBooleansOnAttrs (attrs) {
return _.mapValues(attrs, val => {
if (val === 'true') { return true }
if (val === 'false') { return false }
return val
})
}
function setEmptyAttributes (node) {
if (!node.attributes) {
node.attributes = {}
}
if (node.children) {
node.children.forEach(setEmptyAttributes)
}
}
export default function parseMjml (xml, {
addEmptyAttributes = true,
convertBooleans = true,
} = {}) {
if (!xml) { return null }
let safeXml = xml
safeXml = parseAttributes(safeXml)
safeXml = addCDATASection(safeXml)
let mjml = null
let cur = null
const parser = new htmlparser.Parser({
onopentag: (name, attrs) => {
attrs = _.mapValues(attrs, val => decodeURIComponent(val))
if (convertBooleans) {
// "true" and "false" will be converted to bools
attrs = convertBooleansOnAttrs(attrs)
}
const newNode = {
parent: cur,
tagName: name,
attributes: attrs,
children: [],
}
if (cur) {
cur.children.push(newNode)
} else {
mjml = newNode
}
cur = newNode
},
onclosetag: () => {
cur = (cur && cur.parent) || null
},
ontext: text => {
if (!text) { return }
const val = `${((cur && cur.content) || '')}${text}`.trim()
if (val) {
cur.content = val.replace('$', '$') // prevent issue with $ sign in MJML
}
},
}, {
xmlMode: true,
})
parser.write(safeXml)
parser.end()
if (!_.isObject(mjml)) {
throw new Error('Parsing failed. Check your mjml.')
}
cleanNode(mjml)
// assign "attributes" property if not set
if (addEmptyAttributes) {
setEmptyAttributes(mjml)
}
let mjmlstr = JSON.stringify(mjml)
return { "mjml": mjmlstr }
}