xml-to-html-converter 0.3.0 → 0.3.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.
- package/README.md +19 -3
- package/dist/index.cjs +7 -0
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +6 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,9 +10,9 @@ A zero-dependency Node.js package for converting XML to HTML. Currently in pre-1
|
|
|
10
10
|
|
|
11
11
|
---
|
|
12
12
|
|
|
13
|
-
##
|
|
13
|
+
## XML Node Extraction & Scaffolding
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
The `scaffold` function walks an XML string and produces an array of `XmlNode` objects, each carrying its role, its raw source text, and its position in the document, both globally across the full document and locally within its parent.
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
18
|
interface XmlAttribute {
|
|
@@ -50,6 +50,7 @@ This scaffold is the foundation everything else will be built on. No transformat
|
|
|
50
50
|
>
|
|
51
51
|
> `v0.x` is building the scaffold and the first render pass.
|
|
52
52
|
>
|
|
53
|
+
> - **`minify(xml)`** strips inter-tag whitespace from prettified XML before parsing — text content is left untouched
|
|
53
54
|
> - **`scaffold(xml)`** reads any XML string and returns a nested node tree
|
|
54
55
|
> - Every node knows its `role`, its `raw` source string, its `globalIndex` in the document, and its `localIndex` within its parent
|
|
55
56
|
> - Tag nodes (`openTag`, `selfTag`) also carry `xmlTag`, `xmlInner`, and `xmlAttributes` — the parsed tag name, raw attribute string, and structured attribute array
|
|
@@ -164,6 +165,20 @@ Processing instructions and doctypes are dropped. Comments are passed through un
|
|
|
164
165
|
|
|
165
166
|
---
|
|
166
167
|
|
|
168
|
+
### Minifying prettified XML
|
|
169
|
+
|
|
170
|
+
When your XML comes from a file or an API it is usually indented and line-broken. `minify` strips the whitespace between tags before parsing, leaving text content completely untouched.
|
|
171
|
+
|
|
172
|
+
```js
|
|
173
|
+
import { minify, scaffold, render } from "xml-to-html-converter";
|
|
174
|
+
|
|
175
|
+
const html = render(scaffold(minify(xml)));
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
`minify` is opt-in. Skip it if whitespace inside your content is meaningful.
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
167
182
|
## Node Shape
|
|
168
183
|
|
|
169
184
|
Every node in the tree has the following fields:
|
|
@@ -253,7 +268,7 @@ const tree = scaffold("<root><unclosed><valid>text</valid></root>");
|
|
|
253
268
|
## Exports
|
|
254
269
|
|
|
255
270
|
```ts
|
|
256
|
-
import { scaffold, render, isMalformed } from "xml-to-html-converter";
|
|
271
|
+
import { scaffold, render, minify, isMalformed } from "xml-to-html-converter";
|
|
257
272
|
import type {
|
|
258
273
|
XmlNode,
|
|
259
274
|
XmlNodeRole,
|
|
@@ -264,6 +279,7 @@ import type {
|
|
|
264
279
|
|
|
265
280
|
| Export | Kind | Description |
|
|
266
281
|
| ------------------ | -------- | --------------------------------------------------- |
|
|
282
|
+
| `minify` | function | Strips inter-tag whitespace from an XML string |
|
|
267
283
|
| `scaffold` | function | Parses an XML string and returns a node tree |
|
|
268
284
|
| `render` | function | Converts a node tree to an HTML string |
|
|
269
285
|
| `isMalformed` | function | Type guard, narrows `XmlNode` to `MalformedXmlNode` |
|
package/dist/index.cjs
CHANGED
|
@@ -21,11 +21,17 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
isMalformed: () => isMalformed,
|
|
24
|
+
minify: () => minify,
|
|
24
25
|
render: () => render,
|
|
25
26
|
scaffold: () => scaffold
|
|
26
27
|
});
|
|
27
28
|
module.exports = __toCommonJS(src_exports);
|
|
28
29
|
|
|
30
|
+
// src/modules/minify/minify.ts
|
|
31
|
+
function minify(xml) {
|
|
32
|
+
return xml.replace(/>(\s+)</g, (_, gap) => gap.trim() === "" ? "><" : `>${gap}<`).trim();
|
|
33
|
+
}
|
|
34
|
+
|
|
29
35
|
// src/modules/render/render.ts
|
|
30
36
|
function render(nodes) {
|
|
31
37
|
return nodes.map(renderNode).join("");
|
|
@@ -253,6 +259,7 @@ function isMalformed(node) {
|
|
|
253
259
|
// Annotate the CommonJS export names for ESM import in node:
|
|
254
260
|
0 && (module.exports = {
|
|
255
261
|
isMalformed,
|
|
262
|
+
minify,
|
|
256
263
|
render,
|
|
257
264
|
scaffold
|
|
258
265
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
declare function minify(xml: string): string;
|
|
2
|
+
|
|
1
3
|
interface XmlAttribute {
|
|
2
4
|
name: string;
|
|
3
5
|
value: string;
|
|
@@ -23,4 +25,4 @@ declare function render(nodes: XmlNode[]): string;
|
|
|
23
25
|
|
|
24
26
|
declare function scaffold(xml: string): XmlNode[];
|
|
25
27
|
|
|
26
|
-
export { type MalformedXmlNode, type XmlAttribute, type XmlNode, type XmlNodeRole, isMalformed, render, scaffold };
|
|
28
|
+
export { type MalformedXmlNode, type XmlAttribute, type XmlNode, type XmlNodeRole, isMalformed, minify, render, scaffold };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
declare function minify(xml: string): string;
|
|
2
|
+
|
|
1
3
|
interface XmlAttribute {
|
|
2
4
|
name: string;
|
|
3
5
|
value: string;
|
|
@@ -23,4 +25,4 @@ declare function render(nodes: XmlNode[]): string;
|
|
|
23
25
|
|
|
24
26
|
declare function scaffold(xml: string): XmlNode[];
|
|
25
27
|
|
|
26
|
-
export { type MalformedXmlNode, type XmlAttribute, type XmlNode, type XmlNodeRole, isMalformed, render, scaffold };
|
|
28
|
+
export { type MalformedXmlNode, type XmlAttribute, type XmlNode, type XmlNodeRole, isMalformed, minify, render, scaffold };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
// src/modules/minify/minify.ts
|
|
2
|
+
function minify(xml) {
|
|
3
|
+
return xml.replace(/>(\s+)</g, (_, gap) => gap.trim() === "" ? "><" : `>${gap}<`).trim();
|
|
4
|
+
}
|
|
5
|
+
|
|
1
6
|
// src/modules/render/render.ts
|
|
2
7
|
function render(nodes) {
|
|
3
8
|
return nodes.map(renderNode).join("");
|
|
@@ -224,6 +229,7 @@ function isMalformed(node) {
|
|
|
224
229
|
}
|
|
225
230
|
export {
|
|
226
231
|
isMalformed,
|
|
232
|
+
minify,
|
|
227
233
|
render,
|
|
228
234
|
scaffold
|
|
229
235
|
};
|