You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This package converts between Wiki JSON (`item/info` API response) and a custom XML format (`<sklandDocument>`). All paths go through a shared **DocumentModel** intermediate representation.
18
+
19
+
**Three data formats:**
20
+
21
+
| Format | Source | Root shape |
22
+
|--------|--------|------------|
23
+
| Wiki JSON |`item/info` response |`InfoRoot { data: { item: InfoItem } }` or bare `InfoItem`|
24
+
| XML |`<sklandDocument>` with `<itemId>`, `<metainfo>`, `<description>`, `<chapters>`| Custom schema |
25
+
| DocumentModel | Internal only |`src/model.ts` — typed block/inline tree |
26
+
27
+
**Core pipeline:**
28
+
29
+
```
30
+
Wiki JSON ──[jsonFormat.ts:documentFromJsonText]──> DocumentModel ──[xmlFormat.ts:documentToXmlText]──> XML
31
+
XML ────────[xmlFormat.ts:documentFromXmlText]───> DocumentModel ──[jsonFormat.ts:documentToJsonText]──> Wiki JSON
32
+
```
33
+
34
+
**Key modules:**
35
+
36
+
-`src/model.ts` — DocumentModel types (Block, Inline, Chapter, etc.) plus guards (`isParagraph`, `isTextRun`), constructors (`textRun`, `paragraph`), and `normalizeBlocks` which trims empty paragraphs and merges adjacent text runs.
37
+
-`src/jsonFormat.ts` — Parses nested JSON payload (blockMap/childIds/itemMap structure) into DocumentModel; renders DocumentModel back to the same JSON structure with fresh IDs via IdFactory.
38
+
-`src/xmlFormat.ts` — Parses XML elements (h1-h3, ul/ol, table, img, inline tags) into DocumentModel; renders DocumentModel to XML string via manual string building (no DOM construction).
39
+
-`src/convert.ts` — Public conversion API (`wikiJsonToXml`, `xmlToWikiJson`, batch variants, `convert`). Batch functions are fail-fast: any entry failure throws immediately.
40
+
-`src/cli.ts` — CLI entry (`xml convert --from json|xml --to json|xml`). Uses a `CONVERSION_MAP` lookup table keyed by `"from->to"`.
41
+
-`src/ids.ts` — `IdFactory` generates random alphanumeric IDs (widget, block, item, tab) using `crypto.getRandomValues`.
42
+
-`src/xmlDom.ts` — XML parsing wrapper. Prefers native `globalThis.DOMParser` when available, falls back to `@xmldom/xmldom`.
43
+
-`src/colors.ts` / `src/constants.ts` — Color name mappings (`JSON_TO_XML_COLOR`, `XML_TO_JSON_COLOR`), inline/block tag sets, entry type mappings, table width defaults.
44
+
45
+
**JSON input shape detection** (`extractInfoItem` in `jsonFormat.ts`):
46
+
- Has `data.item` → InfoRoot (strips envelope, saves `infoRootMeta`)
47
+
- Has `brief` and `document` → InfoItem directly
48
+
49
+
**XML parsing** uses two-tier detection: native `DOMParser.parseFromString` first, `@xmldom/xmldom` fallback. Both paths check for `<parsererror>` element after parsing.
50
+
51
+
**Round-trip semantics:** Conversion targets DocumentModel equivalence, not byte-level identity. Rendered JSON regenerates all internal IDs (`widgetCommonMap`, `documentMap`, block ids) while preserving referential integrity.
52
+
53
+
**The `warnings` field** in `ConversionResult` exists in the type signature but is never populated in current code paths — all converters return `[]`.
0 commit comments