@haklex/rich-litexml 0.2.0 → 0.3.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.
package/dist/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { customAlphabet } from "nanoid";
1
2
  import { parseHTML } from "linkedom";
2
3
  //#region src/readers/builtin.ts
3
4
  function extractBlockId$1(el) {
@@ -124,6 +125,9 @@ function registerBuiltinReaders(registry) {
124
125
  }
125
126
  //#endregion
126
127
  //#region src/readers/custom.ts
128
+ var idAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
129
+ var makePollIdSuffix = customAlphabet(idAlphabet, 10);
130
+ var makeOptionIdSuffix = customAlphabet(idAlphabet, 6);
127
131
  function extractBlockId(el) {
128
132
  const id = el.getAttribute("id");
129
133
  return id ? { $: { blockId: id } } : {};
@@ -403,6 +407,36 @@ function registerCustomReaders(registry) {
403
407
  version: 1
404
408
  };
405
409
  });
410
+ registry.registerReader("poll", (el) => {
411
+ let question = "";
412
+ const options = [];
413
+ for (const child of el.children) {
414
+ const tag = child.tagName.toLowerCase();
415
+ if (tag === "question") question = child.textContent ?? "";
416
+ else if (tag === "option") {
417
+ const id = child.getAttribute("id") || `o_${makeOptionIdSuffix()}`;
418
+ options.push({
419
+ id,
420
+ label: child.textContent ?? ""
421
+ });
422
+ }
423
+ }
424
+ const mode = el.getAttribute("mode") === "multiple" ? "multiple" : "single";
425
+ const closeAt = el.getAttribute("close-at");
426
+ const showResultsAttr = el.getAttribute("show-results");
427
+ const showResults = showResultsAttr === "always" || showResultsAttr === "after-vote" || showResultsAttr === "after-close" ? showResultsAttr : void 0;
428
+ return {
429
+ type: "poll",
430
+ ...extractBlockId(el),
431
+ pollId: el.getAttribute("poll-id") || `p_${makePollIdSuffix()}`,
432
+ question,
433
+ options,
434
+ mode,
435
+ ...closeAt ? { closeAt } : {},
436
+ ...showResults ? { showResults } : {},
437
+ version: 1
438
+ };
439
+ });
406
440
  }
407
441
  //#endregion
408
442
  //#region src/registry.ts
@@ -809,6 +843,28 @@ function registerCustomWriters(registry) {
809
843
  children: files
810
844
  };
811
845
  });
846
+ registry.registerWriter("poll", (node) => {
847
+ const n = node;
848
+ const optionElements = (n.options ?? []).map((option) => ({
849
+ tag: "option",
850
+ attrs: optAttr({ id: option.id }),
851
+ children: [option.label ?? ""]
852
+ }));
853
+ return {
854
+ tag: "poll",
855
+ attrs: optAttr({
856
+ ...blockId(n),
857
+ "poll-id": n.pollId,
858
+ "mode": n.mode,
859
+ "close-at": n.closeAt,
860
+ "show-results": n.showResults
861
+ }),
862
+ children: [{
863
+ tag: "question",
864
+ children: [n.question ?? ""]
865
+ }, ...optionElements]
866
+ };
867
+ });
812
868
  }
813
869
  //#endregion
814
870
  //#region src/default-registry.ts
@@ -1 +1 @@
1
- {"version":3,"file":"custom.d.ts","sourceRoot":"","sources":["../../src/readers/custom.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AA6BnD,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI,CA6XrE"}
1
+ {"version":3,"file":"custom.d.ts","sourceRoot":"","sources":["../../src/readers/custom.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAiCnD,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI,CAgarE"}
@@ -1 +1 @@
1
- {"version":3,"file":"custom.d.ts","sourceRoot":"","sources":["../../src/writers/custom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAenD,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI,CAyQrE"}
1
+ {"version":3,"file":"custom.d.ts","sourceRoot":"","sources":["../../src/writers/custom.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAenD,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI,CA+RrE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@haklex/rich-litexml",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Bidirectional Lexical SerializedNode <-> XML conversion with plugin registry",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,7 +21,8 @@
21
21
  ],
22
22
  "dependencies": {
23
23
  "lexical": "^0.44.0",
24
- "linkedom": "^0.18.12"
24
+ "linkedom": "^0.18.12",
25
+ "nanoid": "^5.1.9"
25
26
  },
26
27
  "devDependencies": {
27
28
  "typescript": "^5.9.3",