Skip to content

Commit 8e94745

Browse files
committed
Working 👌
1 parent 2f3f22e commit 8e94745

9 files changed

+3787
-968
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ typings/
5656

5757
# dotenv environment variables file
5858
.env
59+
60+
# mjml files
61+
*.mjml

README.MD

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# MJML2JSON
1+
# JSON2MJML
22

33
## Purpose
44

5-
Converts a MJML template from an `XML` syntax to a valid `JSON` syntax.
5+
Converts a MJML template from a `JSON` syntax to a valid `XML` syntax.
66

77
## Installation
88

@@ -12,6 +12,12 @@ Optional: if you get an error like `command not found: babel-node` when running
1212

1313
## Usage
1414

15-
Input and output filenames must be set, both with their extensions. Use the `-s` optional argument to stringify the output (and make it compatible with the [MJML API](https://mjml.io/api)).
15+
Input and output filenames must be set, both with their extensions.
1616

1717
`babel-node mjml2json.js input output [-s]`
18+
19+
You can run a test on the provided styleguide with `npm run test`.
20+
21+
## MJML2JSON
22+
23+
If you need to convert a MJML template from a `XML` syntax to a valid `JSON` syntax, check [MJML2JSON](https://github.com/ngarnier/mjml2json).

json2mjml.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import json2xml from './parser'
2+
import fs from 'fs'
3+
4+
const input = process.argv[2]
5+
const output = process.argv[3]
6+
let i = 0
7+
8+
// Read MJML from input
9+
async function readMjml(mjmlJSON) {
10+
return new Promise(resolve => {
11+
fs.readFile(mjmlJSON, 'utf8', (err, mjmlContent) => {
12+
resolve(mjmlContent)
13+
})
14+
})
15+
}
16+
17+
// Write the output in a (specified) file
18+
const outputXML = (mjml) => {
19+
writeMjml(json2xml(mjml), output)
20+
}
21+
22+
const writeMjml = (outputMjml, outputLocation) => {
23+
fs.writeFile(outputLocation, outputMjml, err => {
24+
if (err) { console.log(err) }
25+
})
26+
}
27+
28+
async function outputAll(input, output) {
29+
if (input && output) {
30+
const inputMjml = await readMjml(input)
31+
outputXML(JSON.parse(inputMjml))
32+
33+
console.log(`${input} was converted to XML format in ${output}`)
34+
}
35+
else {
36+
console.log('usage: babel-node json2mjml.js input output')
37+
return false
38+
}
39+
}
40+
41+
outputAll(input, output)

mjml2json.js

-49
This file was deleted.

0 commit comments

Comments
 (0)