toon-formatter 2.0.0 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,116 @@
1
+ /**
2
+ * XML <-> YAML Converter (for YamlConverter)
3
+ */
4
+
5
+ import yaml from 'js-yaml';
6
+ import { encodeXmlReservedChars, extractXmlFromString, extractJsonFromString } from '../utils.js';
7
+ import { xmlToJsonSync, jsonToXmlSync } from '../json_formatter/xml.js';
8
+ import { jsonToYamlSync, yamlToJsonSync } from '../json_formatter/yaml.js';
9
+
10
+ // We can reuse the JSON-based intermediate conversion functions
11
+ // XML -> JSON -> YAML
12
+ // YAML -> JSON -> XML
13
+
14
+ /**
15
+ * Convert XML string to YAML string (Sync)
16
+ * Supports mixed text XML.
17
+ * @param {string} xmlString
18
+ * @returns {string} YAML string
19
+ */
20
+ export function xmlToYamlSync(xmlString) {
21
+ if (!xmlString || typeof xmlString !== 'string') {
22
+ throw new Error('Input must be a non-empty string');
23
+ }
24
+
25
+ let convertedText = xmlString;
26
+ let iterationCount = 0;
27
+ const maxIterations = 100;
28
+ let wasModified = false;
29
+
30
+ // Check if pure XML first
31
+ const firstExtract = extractXmlFromString(xmlString);
32
+ if (firstExtract === xmlString) {
33
+ // Pure XML -> JSON -> YAML
34
+ try {
35
+ const json = xmlToJsonSync(xmlString);
36
+ return yaml.dump(json);
37
+ } catch (e) {
38
+ return xmlString; // Fallback?
39
+ }
40
+ }
41
+
42
+ // Mixed Text Loop
43
+ while (iterationCount < maxIterations) {
44
+ const xmlBlock = extractXmlFromString(convertedText);
45
+ if (!xmlBlock) break;
46
+
47
+ try {
48
+ const jsonObject = xmlToJsonSync(xmlBlock); // handles parsing
49
+ const yamlOutput = yaml.dump(jsonObject).trim();
50
+ convertedText = convertedText.replace(xmlBlock, yamlOutput);
51
+ wasModified = true;
52
+ iterationCount++;
53
+ } catch (e) {
54
+ break;
55
+ }
56
+ }
57
+
58
+ if (wasModified) return convertedText;
59
+
60
+ // Fallback try strict
61
+ try {
62
+ const json = xmlToJsonSync(xmlString);
63
+ return yaml.dump(json);
64
+ } catch (e) {
65
+ return xmlString;
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Convert XML string to YAML string (Async)
71
+ * @param {string} xmlString
72
+ * @returns {Promise<string>} YAML string
73
+ */
74
+ export async function xmlToYaml(xmlString) {
75
+ // Ensure DOMParser availability via xmlToJson wrapper if needed,
76
+ // but xmlToJsonSync in json_formatter/xml.js checks for it.
77
+ // However, Node.js polyfill might be needed if not globally set?
78
+ // xmlToJson (Async) in json_formatter/xml.js handles polyfill import.
79
+ // We should probably rely on consistent async patterns.
80
+ // But here we are wrapping Sync logic for mixed text.
81
+ // Mixed text loop is synchronous.
82
+ // For async mixed text, we'd need async loop?
83
+ // Usually mixed text string manipulation is fast enough to be sync.
84
+ return xmlToYamlSync(xmlString);
85
+ }
86
+
87
+
88
+ /**
89
+ * Convert YAML string to XML string (Sync)
90
+ * Note: Does not support mixed text YAML extraction (no extractYaml).
91
+ * @param {string} yamlString
92
+ * @returns {string} XML string
93
+ */
94
+ export function yamlToXmlSync(yamlString) {
95
+ if (!yamlString || typeof yamlString !== 'string') {
96
+ throw new Error('Input must be a non-empty string');
97
+ }
98
+
99
+ try {
100
+ // YAML -> JSON
101
+ const json = yamlToJsonSync(yamlString); // from json_formatter/yaml.js
102
+ // JSON -> XML
103
+ return jsonToXmlSync(json); // from json_formatter/xml.js
104
+ } catch (e) {
105
+ throw new Error(`YAML to XML conversion failed: ${e.message}`);
106
+ }
107
+ }
108
+
109
+ /**
110
+ * Convert YAML string to XML string (Async)
111
+ * @param {string} yamlString
112
+ * @returns {Promise<string>} XML string
113
+ */
114
+ export async function yamlToXml(yamlString) {
115
+ return yamlToXmlSync(yamlString);
116
+ }