Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·93 lines (79 loc) · 2.85 KB
/
Copy pathapp.js
File metadata and controls
executable file
·93 lines (79 loc) · 2.85 KB
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
var DOMParser = require('xmldom').DOMParser;
var XMLSerializer = require('xmldom').XMLSerializer;
var fs = require('fs');
var path = require('path');
function createMindMap(sourceFilePath, targetFilePath) {
if (!fs.existsSync(sourceFilePath)) throw ('Error: json file does not exist');
const jsonInput = fs.readFileSync(sourceFilePath, 'utf8');
const jsonFileName = path.basename(sourceFilePath);
const mindMapData = convert(jsonInput, jsonFileName);
fs.writeFileSync(targetFilePath, mindMapData, { encoding: 'utf8' });
}
function convert(json, jsonFileName) {
let parsedJson;
try { parsedJson = JSON.parse(json); }
catch (err) { throw ('Error: invalid json'); }
const jsonKeys = Object.keys(parsedJson);
const jsonValues = Object.values(parsedJson);
const parser = new DOMParser();
const serializer = new XMLSerializer();
const xmlWrapper = `<map version="1.0.1">
<!-- This file was generated from a JSON structure, using json-to-freemind. -->
<!-- To view this file, download free mind mapping software FreeMind from http://freemind.sourceforge.net -->
<node TEXT="${jsonFileName}"></node>
</map>`;
const xmlDoc = parser.parseFromString(xmlWrapper, 'text/xml');
recur = function recur(keys, values, runNumber, lastParentEl) {
let parentEl;
let topSiblings = [];
for (let p = 0; p < keys.length; p++) {
const key = keys[p];
let value = values[p];
const isLastChild = typeof value != 'object';
const isParent = !isLastChild;
parentEl = xmlDoc.createElement('node');
parentEl.setAttribute('TEXT', key);
if (runNumber == 0) {
lastParentEl = parentEl;
topSiblings.push(parentEl);
}
if (Array.isArray(value)) {
value.forEach((v) => {
if (typeof v != 'object') {
const childEl = xmlDoc.createElement('node');
childEl.setAttribute('TEXT', v);
parentEl.appendChild(childEl);
lastParentEl.appendChild(parentEl);
} else {
lastParentEl.appendChild(parentEl);
recur(Object.keys(v), Object.values(v), runNumber + 1, parentEl)
}
})
continue;
}
if (isLastChild) {
const childEl = xmlDoc.createElement('node');
childEl.setAttribute('TEXT', value);
parentEl.appendChild(childEl);
lastParentEl.appendChild(parentEl);
}
if (runNumber > 0 && isParent) {
lastParentEl.appendChild(parentEl);
}
if (isParent) {
recur(Object.keys(value), Object.values(value), runNumber + 1, parentEl)
}
}
if (runNumber == 0) {
topSiblings.forEach((topSibling) => {
xmlDoc.getElementsByTagName('node')[0].appendChild(topSibling);
})
}
}(jsonKeys, jsonValues, 0);
const output = serializer.serializeToString(xmlDoc);
return output;
}
module.exports = {
convert,
createMindMap
}