@haklex/rich-litexml-cli 0.69.2 → 0.70.0

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.
Files changed (2) hide show
  1. package/dist/cli.mjs +394 -76
  2. package/package.json +4 -4
package/dist/cli.mjs CHANGED
@@ -11880,10 +11880,43 @@ function fence(content) {
11880
11880
  const max = content.match(/`+/g)?.reduce((a, r) => Math.max(a, r.length), 0) ?? 0;
11881
11881
  return "`".repeat(Math.max(3, max + 1));
11882
11882
  }
11883
+ var INLINE_SERIALIZED_TYPES$1 = /* @__PURE__ */ new Set([
11884
+ "text",
11885
+ "linebreak",
11886
+ "tab",
11887
+ "link",
11888
+ "autolink",
11889
+ "spoiler",
11890
+ "ruby",
11891
+ "mention",
11892
+ "tag",
11893
+ "comment",
11894
+ "footnote",
11895
+ "katex-inline",
11896
+ "code-highlight"
11897
+ ]);
11898
+ function isInlineSerializedNode$1(node) {
11899
+ const type = node.type;
11900
+ return typeof type === "string" && INLINE_SERIALIZED_TYPES$1.has(type);
11901
+ }
11883
11902
  function walkText(node) {
11903
+ const type = node.type;
11904
+ if (type === "linebreak") return "\n";
11905
+ if (type === "tab") return " ";
11884
11906
  const children = node.children;
11885
- if (!children) return node.text ?? "";
11886
- return children.map(walkText).join("\n");
11907
+ if (!children) {
11908
+ if (typeof node.text === "string") return node.text;
11909
+ if ((type === "banner" || type === "alert-quote" || type === "nested-doc") && node.content) return stateText(node.content);
11910
+ if (type === "grid-container" && Array.isArray(node.cells)) return node.cells.map((cell) => stateText(cell)).join("\n");
11911
+ if (type === "steps" && Array.isArray(node.steps)) return node.steps.map((step) => [step.title, stateText(step.content)].filter(Boolean).join("\n")).join("\n");
11912
+ return "";
11913
+ }
11914
+ let out = "";
11915
+ for (let i = 0; i < children.length; i++) {
11916
+ if (i > 0 && (!isInlineSerializedNode$1(children[i - 1]) || !isInlineSerializedNode$1(children[i]))) out += "\n";
11917
+ out += walkText(children[i]);
11918
+ }
11919
+ return out;
11887
11920
  }
11888
11921
  function stateText(state) {
11889
11922
  if (!state || typeof state !== "object") return "";
@@ -12030,6 +12063,58 @@ var KATEX_BLOCK_TRANSFORMER = {
12030
12063
  trigger: "$",
12031
12064
  type: "text-match"
12032
12065
  };
12066
+ var KATEX_BLOCK_ELEMENT_TRANSFORMER = {
12067
+ dependencies: [],
12068
+ export: (node) => {
12069
+ if (node.getType() !== "katex-block") return null;
12070
+ return `$$\n${node.__equation ?? ""}\n$$`;
12071
+ },
12072
+ regExp: NEVER,
12073
+ replace: NOOP,
12074
+ type: "element"
12075
+ };
12076
+ var RICH_QUOTE_BLOCK_TRANSFORMER = {
12077
+ dependencies: [],
12078
+ export: (node, exportChildren) => {
12079
+ if (node.getType() !== "rich-quote") return null;
12080
+ const lines = exportChildren(node).split("\n").map((line) => `> ${line}`);
12081
+ const attribution = node.__attribution?.trim();
12082
+ if (attribution) {
12083
+ const footer = attribution.startsWith("—") ? attribution : `— ${attribution}`;
12084
+ lines.push(`> ${footer}`);
12085
+ }
12086
+ return lines.join("\n");
12087
+ },
12088
+ regExp: NEVER,
12089
+ replace: NOOP,
12090
+ type: "element"
12091
+ };
12092
+ var FILE_BLOCK_TRANSFORMER = {
12093
+ dependencies: [],
12094
+ export: (node) => {
12095
+ if (node.getType() !== "file") return null;
12096
+ const j = node.exportJSON();
12097
+ const label = j.name || j.src || "attachment";
12098
+ return j.src ? `[${label}](${j.src})` : label;
12099
+ },
12100
+ regExp: NEVER,
12101
+ replace: NOOP,
12102
+ type: "element"
12103
+ };
12104
+ var DYNAMIC_BLOCK_TRANSFORMER = {
12105
+ dependencies: [],
12106
+ export: (node) => {
12107
+ if (node.getType() !== "dynamic") return null;
12108
+ const j = node.exportJSON();
12109
+ const attrs = [`url="${qA(String(j.url ?? ""))}"`];
12110
+ if (j.initialHeight != null) attrs.push(`initial-height=${j.initialHeight}`);
12111
+ if (!(j.props && typeof j.props === "object" && Object.keys(j.props).length > 0)) return `<dynamic ${attrs.join(" ")} />`;
12112
+ return `<dynamic ${attrs.join(" ")}>\n${JSON.stringify(j.props)}\n</dynamic>`;
12113
+ },
12114
+ regExp: NEVER,
12115
+ replace: NOOP,
12116
+ type: "element"
12117
+ };
12033
12118
  var IMAGE_BLOCK_TRANSFORMER = {
12034
12119
  dependencies: [],
12035
12120
  export: (node) => {
@@ -12286,9 +12371,13 @@ var allHeadlessTransformers = [
12286
12371
  CONTAINER_TRANSFORMER,
12287
12372
  GIT_ALERT_TRANSFORMER,
12288
12373
  Wt,
12374
+ RICH_QUOTE_BLOCK_TRANSFORMER,
12375
+ KATEX_BLOCK_ELEMENT_TRANSFORMER,
12289
12376
  KATEX_BLOCK_TRANSFORMER,
12290
12377
  IMAGE_BLOCK_TRANSFORMER,
12291
12378
  VIDEO_BLOCK_TRANSFORMER,
12379
+ FILE_BLOCK_TRANSFORMER,
12380
+ DYNAMIC_BLOCK_TRANSFORMER,
12292
12381
  CODE_BLOCK_NODE_TRANSFORMER,
12293
12382
  LEGACY_CODE_NODE_TRANSFORMER,
12294
12383
  LINK_CARD_BLOCK_TRANSFORMER,
@@ -12341,6 +12430,25 @@ var builtinNodes = [
12341
12430
  function stubDOM() {
12342
12431
  return document.createElement("span");
12343
12432
  }
12433
+ var INLINE_SERIALIZED_TYPES = /* @__PURE__ */ new Set([
12434
+ "text",
12435
+ "linebreak",
12436
+ "tab",
12437
+ "link",
12438
+ "autolink",
12439
+ "spoiler",
12440
+ "ruby",
12441
+ "mention",
12442
+ "tag",
12443
+ "comment",
12444
+ "footnote",
12445
+ "katex-inline",
12446
+ "code-highlight"
12447
+ ]);
12448
+ function isInlineSerializedNode(node) {
12449
+ const type = node.type;
12450
+ return typeof type === "string" && INLINE_SERIALIZED_TYPES.has(type);
12451
+ }
12344
12452
  function extractText(state) {
12345
12453
  if (!state || typeof state !== "object") return "";
12346
12454
  const root = state.root;
@@ -12348,9 +12456,23 @@ function extractText(state) {
12348
12456
  return walkChildren(root);
12349
12457
  }
12350
12458
  function walkChildren(node) {
12459
+ const type = node.type;
12460
+ if (type === "linebreak") return "\n";
12461
+ if (type === "tab") return " ";
12351
12462
  const children = node.children;
12352
- if (!children) return node.text ?? "";
12353
- return children.map(walkChildren).join("\n");
12463
+ if (!children) {
12464
+ if (typeof node.text === "string") return node.text;
12465
+ if ((type === "banner" || type === "alert-quote" || type === "nested-doc") && node.content) return extractText(node.content);
12466
+ if (type === "grid-container" && Array.isArray(node.cells)) return node.cells.map((cell) => extractText(cell)).join("\n");
12467
+ if (type === "steps" && Array.isArray(node.steps)) return node.steps.map((step) => [step.title, extractText(step.content)].filter(Boolean).join("\n")).join("\n");
12468
+ return "";
12469
+ }
12470
+ let out = "";
12471
+ for (let i = 0; i < children.length; i++) {
12472
+ if (i > 0 && (!isInlineSerializedNode(children[i - 1]) || !isInlineSerializedNode(children[i]))) out += "\n";
12473
+ out += walkChildren(children[i]);
12474
+ }
12475
+ return out;
12354
12476
  }
12355
12477
  var SpoilerNode = class SpoilerNode extends ei {
12356
12478
  static getType() {
@@ -12506,7 +12628,10 @@ function headlessDecorator(type, propKeys, defaults, inline = false) {
12506
12628
  static importJSON(_json) {
12507
12629
  const json = _json;
12508
12630
  const node = new Node();
12509
- for (const k of propKeys) node[`__${k}`] = json[k] ?? defaults[k];
12631
+ for (const k of propKeys) {
12632
+ const def = defaults[k];
12633
+ node[`__${k}`] = def === void 0 || def === null ? json[k] === void 0 ? def : json[k] : json[k] ?? def;
12634
+ }
12510
12635
  return node.updateFromJSON(json);
12511
12636
  }
12512
12637
  exportJSON() {
@@ -12601,7 +12726,10 @@ var LinkCardNode = headlessDecorator("link-card", [
12601
12726
  favicon: void 0,
12602
12727
  image: void 0
12603
12728
  });
12604
- var KaTeXInlineNode = headlessDecorator("katex-inline", ["equation"], { equation: "" }, true);
12729
+ var KaTeXInlineNode = headlessDecorator("katex-inline", ["equation", "color"], {
12730
+ equation: "",
12731
+ color: void 0
12732
+ }, true);
12605
12733
  var KaTeXBlockNode = headlessDecorator("katex-block", ["equation"], { equation: "" });
12606
12734
  var MermaidNode = headlessDecorator("mermaid", ["diagram"], { diagram: "" });
12607
12735
  var MentionNode = headlessDecorator("mention", [
@@ -12648,8 +12776,75 @@ var DynamicNode = headlessDecorator("dynamic", [
12648
12776
  props: {},
12649
12777
  initialHeight: 320
12650
12778
  });
12651
- var TagNode = headlessDecorator("tag", ["text"], { text: "" }, true);
12652
- var CommentNode = headlessDecorator("comment", ["text"], { text: "" }, true);
12779
+ var TagNode = class TagNode extends Si {
12780
+ static getType() {
12781
+ return "tag";
12782
+ }
12783
+ static clone(node) {
12784
+ return new TagNode(node.__text, node.__key);
12785
+ }
12786
+ constructor(text = "", key) {
12787
+ super(text, key);
12788
+ }
12789
+ static importJSON(_json) {
12790
+ const json = _json;
12791
+ return new TagNode(json.text ?? "").updateFromJSON(json);
12792
+ }
12793
+ exportJSON() {
12794
+ return {
12795
+ ...super.exportJSON(),
12796
+ type: "tag",
12797
+ version: 1
12798
+ };
12799
+ }
12800
+ createDOM() {
12801
+ return stubDOM();
12802
+ }
12803
+ updateDOM() {
12804
+ return false;
12805
+ }
12806
+ };
12807
+ var CommentNode = class CommentNode extends Si {
12808
+ static getType() {
12809
+ return "comment";
12810
+ }
12811
+ static clone(node) {
12812
+ return new CommentNode(node.__text, node.__key);
12813
+ }
12814
+ constructor(text = "", key) {
12815
+ super(text, key);
12816
+ }
12817
+ static importJSON(_json) {
12818
+ const json = _json;
12819
+ return new CommentNode(json.text ?? "").updateFromJSON(json);
12820
+ }
12821
+ exportJSON() {
12822
+ return {
12823
+ ...super.exportJSON(),
12824
+ type: "comment",
12825
+ version: 1
12826
+ };
12827
+ }
12828
+ createDOM() {
12829
+ return stubDOM();
12830
+ }
12831
+ updateDOM() {
12832
+ return false;
12833
+ }
12834
+ };
12835
+ var AgentDiffNode = headlessDecorator("agent-diff", [
12836
+ "batchId",
12837
+ "diffEntryId",
12838
+ "opType",
12839
+ "originalNode",
12840
+ "proposedNode"
12841
+ ], {
12842
+ batchId: "",
12843
+ diffEntryId: "",
12844
+ opType: "insert",
12845
+ originalNode: null,
12846
+ proposedNode: null
12847
+ });
12653
12848
  var PollNode = headlessDecorator("poll", [
12654
12849
  "pollId",
12655
12850
  "question",
@@ -12935,7 +13130,8 @@ var customHeadlessNodes = [
12935
13130
  GridContainerNode,
12936
13131
  StepsNode,
12937
13132
  PollNode,
12938
- ChatNode
13133
+ ChatNode,
13134
+ AgentDiffNode
12939
13135
  ];
12940
13136
  var allHeadlessNodes = [...builtinNodes, ...customHeadlessNodes];
12941
13137
  //#endregion
@@ -28604,18 +28800,22 @@ function registerBuiltinReaders(registry) {
28604
28800
  version: 1
28605
28801
  };
28606
28802
  });
28607
- registry.registerReader("ol", (el, ctx) => ({
28608
- type: "list",
28609
- listType: "number",
28610
- tag: "ol",
28611
- start: Number(el.getAttribute("start") ?? 1),
28612
- ...extractBlockId$1(el),
28613
- children: ctx.parseChildren(el),
28614
- direction: "ltr",
28615
- format: "",
28616
- indent: 0,
28617
- version: 1
28618
- }));
28803
+ registry.registerReader("ol", (el, ctx) => {
28804
+ const startAttr = el.getAttribute("start");
28805
+ const parsedStart = startAttr !== null ? Number(startAttr) : 1;
28806
+ return {
28807
+ type: "list",
28808
+ listType: "number",
28809
+ tag: "ol",
28810
+ start: Number.isFinite(parsedStart) ? parsedStart : 1,
28811
+ ...extractBlockId$1(el),
28812
+ children: ctx.parseChildren(el),
28813
+ direction: "ltr",
28814
+ format: "",
28815
+ indent: 0,
28816
+ version: 1
28817
+ };
28818
+ });
28619
28819
  registry.registerReader("li", (el, ctx) => {
28620
28820
  const checked = el.getAttribute("checked");
28621
28821
  return {
@@ -28656,24 +28856,30 @@ function registerBuiltinReaders(registry) {
28656
28856
  indent: 0,
28657
28857
  version: 1
28658
28858
  }));
28659
- registry.registerReader("th", (el, ctx) => ({
28660
- type: "tablecell",
28661
- headerState: 1,
28662
- children: ctx.parseChildren(el),
28663
- direction: "ltr",
28664
- format: "",
28665
- indent: 0,
28666
- version: 1
28667
- }));
28668
- registry.registerReader("td", (el, ctx) => ({
28669
- type: "tablecell",
28670
- headerState: 0,
28671
- children: ctx.parseChildren(el),
28672
- direction: "ltr",
28673
- format: "",
28674
- indent: 0,
28675
- version: 1
28676
- }));
28859
+ const readTableCell = (defaultHeaderState) => (el, ctx) => {
28860
+ const headerStateAttr = el.getAttribute("header-state");
28861
+ const parsedHeaderState = headerStateAttr !== null ? Number(headerStateAttr) : defaultHeaderState;
28862
+ const headerState = Number.isFinite(parsedHeaderState) ? parsedHeaderState : defaultHeaderState;
28863
+ const colSpanAttr = el.getAttribute("colspan");
28864
+ const parsedColSpan = colSpanAttr !== null ? Number(colSpanAttr) : void 0;
28865
+ const colSpan = parsedColSpan !== void 0 && Number.isFinite(parsedColSpan) && parsedColSpan > 1 ? Math.floor(parsedColSpan) : void 0;
28866
+ const rowSpanAttr = el.getAttribute("rowspan");
28867
+ const parsedRowSpan = rowSpanAttr !== null ? Number(rowSpanAttr) : void 0;
28868
+ const rowSpan = parsedRowSpan !== void 0 && Number.isFinite(parsedRowSpan) && parsedRowSpan > 1 ? Math.floor(parsedRowSpan) : void 0;
28869
+ return {
28870
+ type: "tablecell",
28871
+ headerState,
28872
+ ...colSpan !== void 0 ? { colSpan } : {},
28873
+ ...rowSpan !== void 0 ? { rowSpan } : {},
28874
+ children: ctx.parseChildren(el),
28875
+ direction: "ltr",
28876
+ format: "",
28877
+ indent: 0,
28878
+ version: 1
28879
+ };
28880
+ };
28881
+ registry.registerReader("th", readTableCell(1));
28882
+ registry.registerReader("td", readTableCell(0));
28677
28883
  }
28678
28884
  //#endregion
28679
28885
  //#region ../../node_modules/.pnpm/nanoid@6.0.2/node_modules/nanoid/url-alphabet/index.js
@@ -28837,15 +29043,24 @@ function galleryMaxItemHeightAttr(el) {
28837
29043
  }
28838
29044
  /**
28839
29045
  * Extract CDATA text content from an element.
28840
- * linkedom (HTML parser) converts <![CDATA[...]]> to a comment node
28841
- * with value "[CDATA[...]]", so we detect that pattern.
29046
+ * linkedom / HTML parsers convert <![CDATA[...]]> to comment nodes with value
29047
+ * "[CDATA[...]]". Multiple consecutive CDATA sections (e.g. from standard XML
29048
+ * `]]]]><![CDATA[>` splitting) are concatenated into a single string.
28842
29049
  */
28843
29050
  function extractCdataText(el) {
29051
+ let found = false;
29052
+ const parts = [];
28844
29053
  for (const child of el.childNodes) if (child.nodeType === 8) {
28845
29054
  const val = child.nodeValue ?? "";
28846
- if (val.startsWith("[CDATA[") && val.endsWith("]]")) return val.slice(7, -2);
29055
+ if (val.startsWith("[CDATA[") && val.endsWith("]]")) {
29056
+ found = true;
29057
+ parts.push(val.slice(7, -2));
29058
+ }
29059
+ } else if (child.nodeType === 4) {
29060
+ found = true;
29061
+ parts.push(child.nodeValue ?? "");
28847
29062
  }
28848
- return null;
29063
+ return found ? parts.join("") : null;
28849
29064
  }
28850
29065
  function extractRawText(el) {
28851
29066
  return extractCdataText(el) ?? el.textContent ?? "";
@@ -29016,13 +29231,17 @@ function registerCustomReaders(registry) {
29016
29231
  textStyle: "",
29017
29232
  version: 1
29018
29233
  }));
29019
- registry.registerReader("mention", (el) => ({
29020
- type: "mention",
29021
- platform: el.getAttribute("platform") ?? "",
29022
- handle: el.getAttribute("handle") ?? "",
29023
- displayName: el.textContent || void 0,
29024
- version: 1
29025
- }));
29234
+ registry.registerReader("mention", (el) => {
29235
+ const handle = el.getAttribute("handle") ?? "";
29236
+ const body = el.textContent || "";
29237
+ return {
29238
+ type: "mention",
29239
+ platform: el.getAttribute("platform") ?? "",
29240
+ handle,
29241
+ displayName: body && body !== handle ? body : void 0,
29242
+ version: 1
29243
+ };
29244
+ });
29026
29245
  registry.registerReader("tag", (el) => ({
29027
29246
  type: "tag",
29028
29247
  text: el.textContent ?? "",
@@ -29305,6 +29524,7 @@ function registerBuiltinWriters(registry) {
29305
29524
  const tag = n.listType === "number" ? "ol" : "ul";
29306
29525
  const attrs = { ...blockId$1(n) };
29307
29526
  if (n.listType === "check") attrs.type = "check";
29527
+ if (tag === "ol" && typeof n.start === "number" && n.start !== 1) attrs.start = String(n.start);
29308
29528
  return {
29309
29529
  tag,
29310
29530
  attrs,
@@ -29358,11 +29578,35 @@ function registerBuiltinWriters(registry) {
29358
29578
  });
29359
29579
  registry.registerWriter("tablecell", (node, ctx) => {
29360
29580
  const n = node;
29581
+ const headerState = typeof n.headerState === "number" ? n.headerState : 0;
29582
+ const tag = headerState !== 0 ? "th" : "td";
29583
+ const attrs = {};
29584
+ if (headerState !== 0 && headerState !== 1) attrs["header-state"] = String(headerState);
29585
+ if (typeof n.colSpan === "number" && n.colSpan > 1) attrs.colspan = String(n.colSpan);
29586
+ if (typeof n.rowSpan === "number" && n.rowSpan > 1) attrs.rowspan = String(n.rowSpan);
29361
29587
  return {
29362
- tag: n.headerState === 1 ? "th" : "td",
29588
+ tag,
29589
+ ...Object.keys(attrs).length > 0 ? { attrs } : {},
29363
29590
  children: ctx.serializeChildren(n.children ?? [])
29364
29591
  };
29365
29592
  });
29593
+ registry.registerWriter("code", (node) => {
29594
+ const n = node;
29595
+ const extractCodeText = (children) => children.map((c) => {
29596
+ if (c.type === "linebreak") return "\n";
29597
+ if (c.type === "tab") return " ";
29598
+ if (typeof c.text === "string") return c.text;
29599
+ if (Array.isArray(c.children)) return extractCodeText(c.children);
29600
+ return "";
29601
+ }).join("");
29602
+ const attrs = { ...blockId$1(n) };
29603
+ if (typeof n.language === "string" && n.language !== "") attrs.lang = n.language;
29604
+ return {
29605
+ tag: "codeblock",
29606
+ attrs,
29607
+ children: [extractCodeText(n.children ?? [])]
29608
+ };
29609
+ });
29366
29610
  }
29367
29611
  //#endregion
29368
29612
  //#region ../rich-litexml/src/writers/custom.ts
@@ -29403,7 +29647,9 @@ function registerCustomWriters(registry) {
29403
29647
  attrs: optAttr({
29404
29648
  ...blockId(n),
29405
29649
  src: n.src,
29406
- poster: n.poster
29650
+ poster: n.poster,
29651
+ width: n.width != null ? String(n.width) : void 0,
29652
+ height: n.height != null ? String(n.height) : void 0
29407
29653
  }),
29408
29654
  selfClosing: true
29409
29655
  };
@@ -29891,12 +30137,10 @@ function expandSelfClosing(chunk) {
29891
30137
  return `<${tag}${attrs}></${tag}>`;
29892
30138
  });
29893
30139
  }
29894
- /**
29895
- * Expand `<custom-tag … />` to `<custom-tag …></custom-tag>` outside CDATA.
29896
- * Leaves HTML void elements and already-paired tags untouched. CDATA content
29897
- * is sliced out verbatim with `indexOf` to avoid regex pitfalls around `]]>`.
29898
- */
29899
- function normalizeSelfClosingTags(xml) {
30140
+ function prepareXmlForHtmlParse(xml) {
30141
+ let marker = "__LITEXML_CDATA_";
30142
+ while (xml.includes(marker)) marker += "_";
30143
+ const cdataPayloads = [];
29900
30144
  const out = [];
29901
30145
  let cursor = 0;
29902
30146
  while (cursor < xml.length) {
@@ -29911,13 +30155,32 @@ function normalizeSelfClosingTags(xml) {
29911
30155
  out.push(xml.slice(start));
29912
30156
  break;
29913
30157
  }
29914
- out.push(xml.slice(start, end + 3));
30158
+ const payload = xml.slice(start + 9, end);
30159
+ const idx = cdataPayloads.length;
30160
+ cdataPayloads.push(payload);
30161
+ out.push(`<!--${marker}${idx}-->`);
29915
30162
  cursor = end + 3;
29916
30163
  }
29917
- return out.join("");
30164
+ return {
30165
+ html: out.join(""),
30166
+ cdataPayloads,
30167
+ marker
30168
+ };
30169
+ }
30170
+ function restoreCdataPlaceholders(root, cdataPayloads, marker) {
30171
+ for (const child of root.childNodes) if (child.nodeType === 8) {
30172
+ const val = child.nodeValue ?? "";
30173
+ if (val.startsWith(marker)) {
30174
+ const idx = Number(val.slice(marker.length));
30175
+ if (Number.isInteger(idx) && idx >= 0 && idx < cdataPayloads.length) child.nodeValue = `[CDATA[${cdataPayloads[idx]}]]`;
30176
+ }
30177
+ } else if (child.nodeType === 1) restoreCdataPlaceholders(child, cdataPayloads, marker);
29918
30178
  }
29919
30179
  function parseXml(xml) {
29920
- return parseHTML(`<!DOCTYPE html><html><body>${normalizeSelfClosingTags(xml)}</body></html>`);
30180
+ const { html, cdataPayloads, marker } = prepareXmlForHtmlParse(xml);
30181
+ const doc = parseHTML(`<!DOCTYPE html><html><body>${html}</body></html>`);
30182
+ if (cdataPayloads.length > 0 && doc.body) restoreCdataPlaceholders(doc.body, cdataPayloads, marker);
30183
+ return doc;
29921
30184
  }
29922
30185
  function deserializeFromXml(xml, registry) {
29923
30186
  const doc = parseXml(xml);
@@ -29958,33 +30221,71 @@ var BLOCK_TAGS = /* @__PURE__ */ new Set([
29958
30221
  function isBlockContainer(element) {
29959
30222
  return BLOCK_TAGS.has(element.tagName.toLowerCase());
29960
30223
  }
30224
+ function detectChildIndent(element) {
30225
+ let prevText = "";
30226
+ for (const child of element.childNodes) if (child.nodeType === 3) prevText += child.textContent ?? "";
30227
+ else if (child.nodeType === 1) {
30228
+ if (!isInlineTag(child.tagName)) {
30229
+ const lastNl = prevText.lastIndexOf("\n");
30230
+ if (lastNl >= 0) {
30231
+ const afterNl = prevText.slice(lastNl + 1);
30232
+ if (/^[ \t]*$/.test(afterNl)) return afterNl;
30233
+ }
30234
+ }
30235
+ prevText = "";
30236
+ }
30237
+ return null;
30238
+ }
30239
+ function stripLeadingLayoutIndent(text, childIndent) {
30240
+ const match = /^[ \t]*\n/.exec(text);
30241
+ if (!match) return text;
30242
+ const afterNewline = text.slice(match[0].length);
30243
+ if (childIndent !== null) {
30244
+ if (afterNewline.startsWith(childIndent)) return afterNewline.slice(childIndent.length);
30245
+ let i = 0;
30246
+ while (i < afterNewline.length && i < childIndent.length && (afterNewline[i] === " " || afterNewline[i] === " ")) i++;
30247
+ return afterNewline.slice(i);
30248
+ }
30249
+ return afterNewline.replace(/^[ \t]*/, "");
30250
+ }
29961
30251
  function createReaderContext(registry) {
29962
30252
  const ctx = {
29963
- parseChildren(element) {
30253
+ parseChildren(element, inheritedFormat = 0) {
29964
30254
  const holdsBlockChild = [...element.children].some((child) => !isInlineTag(child.tagName));
29965
- const blockLevel = isBlockContainer(element) || holdsBlockChild;
30255
+ const isStructuralBlock = isBlockContainer(element);
30256
+ const blockLevel = isStructuralBlock || holdsBlockChild;
30257
+ const childIndent = holdsBlockChild ? detectChildIndent(element) : null;
29966
30258
  const nodes = [];
29967
30259
  let pendingText = "";
29968
- const flushText = () => {
30260
+ let prevWasBoundary = true;
30261
+ const flushText = (nextIsBoundary) => {
29969
30262
  if (pendingText === "") return;
29970
- if (blockLevel && pendingText.trim() === "") {
30263
+ if (blockLevel && pendingText.trim() === "" && (isStructuralBlock || prevWasBoundary || nextIsBoundary)) {
29971
30264
  pendingText = "";
29972
30265
  return;
29973
30266
  }
29974
- const text = blockLevel ? pendingText.replace(/^\s*\n[ \t]*/, "").replace(/\n\s*$/, "") : pendingText;
29975
- nodes.push(makeTextNode(text, 0));
30267
+ let text = pendingText;
30268
+ if (blockLevel) {
30269
+ const hadLeadingLayout = prevWasBoundary && /^[ \t]*\n/.test(text);
30270
+ if (hadLeadingLayout) text = stripLeadingLayoutIndent(text, childIndent);
30271
+ if (nextIsBoundary && (holdsBlockChild || hadLeadingLayout)) text = text.replace(/\n[ \t]*$/, "");
30272
+ }
30273
+ nodes.push(makeTextNode(text, inheritedFormat));
29976
30274
  pendingText = "";
29977
30275
  };
29978
30276
  for (const child of element.childNodes) if (child.nodeType === 3) pendingText += child.textContent ?? "";
29979
30277
  else if (child.nodeType === 1) {
29980
- flushText();
29981
- const parsed = parseElement(child, registry, ctx, 0);
30278
+ const el = child;
30279
+ const isBlock = !isInlineTag(el.tagName);
30280
+ flushText(isBlock);
30281
+ const parsed = parseElement(el, registry, ctx, inheritedFormat);
29982
30282
  if (parsed) {
29983
30283
  if (Array.isArray(parsed)) nodes.push(...parsed);
29984
30284
  else nodes.push(parsed);
29985
30285
  }
30286
+ prevWasBoundary = isBlock;
29986
30287
  }
29987
- flushText();
30288
+ flushText(true);
29988
30289
  return nodes;
29989
30290
  },
29990
30291
  parseNestedState(xml) {
@@ -30001,12 +30302,19 @@ function parseElement(element, registry, ctx, inheritedFormat) {
30001
30302
  version: 1
30002
30303
  };
30003
30304
  if (tag === "node") return parseFallbackNode(element);
30305
+ const effectiveFormat = isBlockContainer(element) ? 0 : inheritedFormat;
30306
+ const scopedCtx = effectiveFormat !== 0 ? {
30307
+ ...ctx,
30308
+ parseChildren(el, fmt) {
30309
+ return ctx.parseChildren(el, fmt ?? effectiveFormat);
30310
+ }
30311
+ } : ctx;
30004
30312
  const reader = registry.getReader(tag);
30005
30313
  if (reader) {
30006
- const result = reader(element, ctx);
30314
+ const result = reader(element, scopedCtx);
30007
30315
  if (result !== false) return result;
30008
30316
  }
30009
- return ctx.parseChildren(element);
30317
+ return scopedCtx.parseChildren(element);
30010
30318
  }
30011
30319
  function parseInlineChildren(element, registry, ctx, format) {
30012
30320
  const nodes = [];
@@ -30032,12 +30340,22 @@ function parseFallbackNode(element) {
30032
30340
  const type = element.getAttribute("type") ?? "unknown";
30033
30341
  const id = element.getAttribute("id");
30034
30342
  const dataStr = element.getAttribute("data");
30035
- const data = dataStr ? JSON.parse(dataStr) : {};
30343
+ let data = {};
30344
+ if (dataStr) try {
30345
+ const parsed = JSON.parse(dataStr);
30346
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) data = parsed;
30347
+ } catch {}
30348
+ const { $: dataMeta, version: dataVersion, ...rest } = data;
30349
+ const mergedMeta = {
30350
+ ...id ? { blockId: id } : {},
30351
+ ...dataMeta && typeof dataMeta === "object" && !Array.isArray(dataMeta) ? dataMeta : {}
30352
+ };
30353
+ const version = typeof dataVersion === "number" ? dataVersion : 1;
30036
30354
  return {
30037
30355
  type,
30038
- ...id ? { $: { blockId: id } } : {},
30039
- ...data,
30040
- version: 1
30356
+ ...Object.keys(mergedMeta).length > 0 ? { $: mergedMeta } : {},
30357
+ ...rest,
30358
+ version
30041
30359
  };
30042
30360
  }
30043
30361
  function makeTextNode(text, format) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haklex/rich-litexml-cli",
3
- "version": "0.69.2",
3
+ "version": "0.70.0",
4
4
  "description": "Unified CLI for haklex LiteXML: convert LiteXML or Lexical JSON to HTML, JSON, or Markdown.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,9 +16,9 @@
16
16
  "dist"
17
17
  ],
18
18
  "dependencies": {
19
- "@haklex/rich-compose": "0.69.2",
20
- "@haklex/rich-headless": "0.69.2",
21
- "@haklex/rich-litexml": "0.69.2",
19
+ "@haklex/rich-compose": "0.70.0",
20
+ "@haklex/rich-headless": "0.70.0",
21
+ "@haklex/rich-litexml": "0.70.0",
22
22
  "@lexical/headless": "^0.53.0",
23
23
  "lexical": "^0.53.0"
24
24
  },