-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandFunctions.js
94 lines (84 loc) · 2.98 KB
/
CommandFunctions.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
'use strict';
/** Core Packages **/
import fs from 'node:fs'
/** Additional Packages **/
import * as json2toml from 'json2toml';
import * as json2yaml from 'json-to-pretty-yaml';
import * as json_format from 'json-format';
import showdown from 'showdown';
import xml2js from 'xml2js';
import { JSDOM } from 'jsdom';
const tomlParser = json2toml.default;
/**
* Takes a path to an XML file which will be transformed into JSON
* @param { Object } options - Program options
**/
const jsonify_xml = (options) => {
const xml_string = fs.readFileSync(options.path, { "encoding": "utf8" });
const parser = new xml2js.Parser();
parser.parseString(xml_string, function (err, result) {
const xmlJson = json_format.default(result, {
type : 'space',
space : '4'
});
console.log(xmlJson);
});
}
/**
* Takes a path to a JSON file which will be transformed into XML
* @param { Object } options - Program options
**/
const xmlify_json = (options) => {
const xml_json = JSON.parse(fs.readFileSync(options.path, { "encoding": "utf8" }));
const builder = new xml2js.Builder();
const xml_string = builder.buildObject(xml_json);
console.log(xml_string);
}
/**
* Takes a path to a JSON file which will be transformed into YAML
* @param { Object } options - Program options
**/
const yamlify_json = (options) => {
const json_string = fs.readFileSync(options.path, { "encoding": "utf8" });
const json = JSON.parse(json_string)
const json_yaml = json2yaml.default.stringify(json);
console.log(json_yaml);
}
/**
* Takes a path to a JSON file which will be transformed into TOML
* @param { Object } options - Program options
**/
const tomlify_json = (options) => {
const json_string = fs.readFileSync(options.path, { "encoding": "utf8" });
const json = JSON.parse(json_string)
const json_toml = tomlParser(json, {
indent: 4,
newlineAfterSection: true
});
console.log(json_toml);
}
/**
* Takes a path to a markdown file which will be transformed into HTML
* @param { Object } options - Program options
**/
const htmlify_markdown = (options) => {
const markdown_string = fs.readFileSync(options.path, { "encoding": "utf8" });
const converter = new showdown.Converter();
converter.setOption(`smartIndentationFix`, true)
console.log(converter.makeHtml(markdown_string));
}
/**
* Takes a path to an HTML file which will be transformed into Markdown
* @param { Object } options - Program options
**/
const markdownify_html = (options) => {
/**
* Solves window not defined error with showdown library
* which allows compatability with nodeJS.
**/
globalThis.window = new JSDOM('', {}).window
const html_string = fs.readFileSync(options.path, { "encoding": "utf8" });
const converter = new showdown.Converter();
console.log(converter.makeMarkdown(html_string));
}
export { xmlify_json, markdownify_html, htmlify_markdown, jsonify_xml, yamlify_json, tomlify_json };