-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
49 lines (48 loc) · 1.07 KB
/
index.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
var utils = require('./lib/utils');
var formatters = require('./lib/formatters');
module.exports = function formatMobiledoc(doc) {
if (doc == null || typeof doc === 'string') {
return JSON.stringify(doc);
}
if (doc.version !== '0.3.0') {
return JSON.stringify(doc, null, 2);
}
var lists = [
{
key: 'markups',
formatter: formatters.markup
},
{
key: 'atoms',
formatter: formatters.atom
},
{
key: 'cards',
formatter: formatters.card
},
{
key: 'sections',
formatter: formatters.section
}
];
var pairs = [];
lists.forEach(function(list) {
var key = list.key;
if (doc[key]) {
pairs.push({
key: key,
value: utils.multiLineArray(doc[key].map(list.formatter))
});
}
});
if (doc.version) {
pairs.unshift({
key: 'version',
value: formatters.version(doc.version)
});
}
var block = utils.indent(pairs.map(function(pair) {
return JSON.stringify(pair.key) + ': ' + pair.value;
}).join(',\n'), 2);
return '{\n' + block + '\n}';
}