@code.store/arcxp-sdk-ts 5.3.0 → 5.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/dist/index.js CHANGED
@@ -7,6 +7,7 @@ import FormData from 'form-data';
7
7
  import * as ws from 'ws';
8
8
  import { TextNode, HTMLElement, CommentNode, parse } from 'node-html-parser';
9
9
  import { decode } from 'html-entities';
10
+ import * as xmldoc from 'xmldoc';
10
11
  import encode from 'base32-encode';
11
12
  import { v5 } from 'uuid';
12
13
  import assert from 'node:assert';
@@ -889,7 +890,7 @@ const ArcAPI = (options) => {
889
890
  return API;
890
891
  };
891
892
 
892
- const ContentElement = {
893
+ const ContentElement$1 = {
893
894
  divider: () => {
894
895
  return {
895
896
  type: 'divider',
@@ -1121,7 +1122,7 @@ const ContentElement = {
1121
1122
  },
1122
1123
  };
1123
1124
 
1124
- const BLOCK_ELEMENT_TAGS = [
1125
+ const BLOCK_ELEMENT_TAGS$1 = [
1125
1126
  'ADDRESS',
1126
1127
  'ARTICLE',
1127
1128
  'ASIDE',
@@ -1160,7 +1161,7 @@ const BLOCK_ELEMENT_TAGS = [
1160
1161
 
1161
1162
  var html_constants = /*#__PURE__*/Object.freeze({
1162
1163
  __proto__: null,
1163
- BLOCK_ELEMENT_TAGS: BLOCK_ELEMENT_TAGS
1164
+ BLOCK_ELEMENT_TAGS: BLOCK_ELEMENT_TAGS$1
1164
1165
  });
1165
1166
 
1166
1167
  const socialRegExps = {
@@ -1204,27 +1205,27 @@ function createSocial(url = '') {
1204
1205
  const embeds = [];
1205
1206
  const instagram = instagramURLParser(url);
1206
1207
  if (instagram) {
1207
- embeds.push(ContentElement.instagram(instagram));
1208
+ embeds.push(ContentElement$1.instagram(instagram));
1208
1209
  }
1209
1210
  const twitter = twitterURLParser(url);
1210
1211
  if (twitter) {
1211
- embeds.push(ContentElement.twitter(twitter));
1212
+ embeds.push(ContentElement$1.twitter(twitter));
1212
1213
  }
1213
1214
  const tiktok = tiktokURLParser(url);
1214
1215
  if (tiktok) {
1215
- embeds.push(ContentElement.tiktok(tiktok));
1216
+ embeds.push(ContentElement$1.tiktok(tiktok));
1216
1217
  }
1217
1218
  const youtube = youtubeURLParser(url);
1218
1219
  if (youtube) {
1219
- embeds.push(ContentElement.youtube(youtube));
1220
+ embeds.push(ContentElement$1.youtube(youtube));
1220
1221
  }
1221
1222
  const facebookPost = facebookPostURLParser(url);
1222
1223
  if (facebookPost) {
1223
- embeds.push(ContentElement.facebook_post(facebookPost));
1224
+ embeds.push(ContentElement$1.facebook_post(facebookPost));
1224
1225
  }
1225
1226
  const facebookVideo = facebookVideoURLParser(url);
1226
1227
  if (facebookVideo) {
1227
- embeds.push(ContentElement.facebook_video(facebookVideo));
1228
+ embeds.push(ContentElement$1.facebook_video(facebookVideo));
1228
1229
  }
1229
1230
  return embeds;
1230
1231
  }
@@ -1248,7 +1249,7 @@ var ContentElements = /*#__PURE__*/Object.freeze({
1248
1249
  youtubeURLParser: youtubeURLParser
1249
1250
  });
1250
1251
 
1251
- const isTextNode = (node) => {
1252
+ const isTextNode$1 = (node) => {
1252
1253
  return node instanceof TextNode;
1253
1254
  };
1254
1255
  const isHTMLElement = (node) => {
@@ -1282,7 +1283,7 @@ var html_utils = /*#__PURE__*/Object.freeze({
1282
1283
  htmlToText: htmlToText,
1283
1284
  isCommentNode: isCommentNode,
1284
1285
  isHTMLElement: isHTMLElement,
1285
- isTextNode: isTextNode,
1286
+ isTextNode: isTextNode$1,
1286
1287
  nodeTagIn: nodeTagIn,
1287
1288
  nodeTagIs: nodeTagIs
1288
1289
  });
@@ -1430,7 +1431,7 @@ class HTMLProcessor {
1430
1431
  }
1431
1432
  });
1432
1433
  this.handle('text', (node) => {
1433
- if (isTextNode(node)) {
1434
+ if (isTextNode$1(node)) {
1434
1435
  return this.createText(node);
1435
1436
  }
1436
1437
  });
@@ -1608,23 +1609,23 @@ class HTMLProcessor {
1608
1609
  return [];
1609
1610
  }
1610
1611
  async handleTable(node) {
1611
- return [ContentElement.raw_html(node.toString())];
1612
+ return [ContentElement$1.raw_html(node.toString())];
1612
1613
  }
1613
1614
  async handleIframe(node) {
1614
- return [ContentElement.raw_html(node.toString())];
1615
+ return [ContentElement$1.raw_html(node.toString())];
1615
1616
  }
1616
1617
  async handleImage(node) {
1617
- return [ContentElement.raw_html(node.toString())];
1618
+ return [ContentElement$1.raw_html(node.toString())];
1618
1619
  }
1619
1620
  async handleBreak(_) {
1620
- return [ContentElement.divider()];
1621
+ return [ContentElement$1.divider()];
1621
1622
  }
1622
1623
  async createQuote(node) {
1623
1624
  const items = await this.handleNested(node);
1624
- return [ContentElement.quote(items)];
1625
+ return [ContentElement$1.quote(items)];
1625
1626
  }
1626
1627
  async createText(node) {
1627
- const text = ContentElement.text(node.text);
1628
+ const text = ContentElement$1.text(node.text);
1628
1629
  return [text];
1629
1630
  }
1630
1631
  filterListItems(items) {
@@ -1632,16 +1633,16 @@ class HTMLProcessor {
1632
1633
  }
1633
1634
  async createList(node, type) {
1634
1635
  const items = await this.handleNested(node);
1635
- return [ContentElement.list(type, this.filterListItems(items))];
1636
+ return [ContentElement$1.list(type, this.filterListItems(items))];
1636
1637
  }
1637
1638
  async createHeader(node) {
1638
1639
  const level = +node.tagName.split('H')[1] || 3;
1639
- return [ContentElement.header(node.innerText, level)];
1640
+ return [ContentElement$1.header(node.innerText, level)];
1640
1641
  }
1641
1642
  isBlockElement(node) {
1642
1643
  if (!isHTMLElement(node))
1643
1644
  return false;
1644
- const defaultBlockElements = new Set(BLOCK_ELEMENT_TAGS);
1645
+ const defaultBlockElements = new Set(BLOCK_ELEMENT_TAGS$1);
1645
1646
  return defaultBlockElements.has(node.tagName);
1646
1647
  }
1647
1648
  warn(metadata, message) {
@@ -1649,17 +1650,240 @@ class HTMLProcessor {
1649
1650
  }
1650
1651
  }
1651
1652
 
1652
- var index$3 = /*#__PURE__*/Object.freeze({
1653
+ var index$4 = /*#__PURE__*/Object.freeze({
1653
1654
  __proto__: null,
1654
1655
  Constants: html_constants,
1655
1656
  HTMLProcessor: HTMLProcessor,
1656
1657
  Utils: html_utils
1657
1658
  });
1658
1659
 
1660
+ const BLOCK_ELEMENT_TAGS = ['paragraph', 'line', 'header', 'ul', 'ol', 'li', 'embed', 'iframe', 'table'];
1661
+
1662
+ var xml_constants = /*#__PURE__*/Object.freeze({
1663
+ __proto__: null,
1664
+ BLOCK_ELEMENT_TAGS: BLOCK_ELEMENT_TAGS
1665
+ });
1666
+
1667
+ const isXmlElement = (node) => {
1668
+ return node?.type === 'element';
1669
+ };
1670
+ const isTextNode = (node) => {
1671
+ return node?.type === 'text';
1672
+ };
1673
+ const nodeNameIs = (node, name) => {
1674
+ return isXmlElement(node) && node.name === name;
1675
+ };
1676
+ const nodeNameIn = (node, names) => {
1677
+ return isXmlElement(node) && names.includes(node.name);
1678
+ };
1679
+
1680
+ var xml_utils = /*#__PURE__*/Object.freeze({
1681
+ __proto__: null,
1682
+ isTextNode: isTextNode,
1683
+ isXmlElement: isXmlElement,
1684
+ nodeNameIn: nodeNameIn,
1685
+ nodeNameIs: nodeNameIs
1686
+ });
1687
+
1688
+ const ContentElement = ContentElement$1;
1689
+ class XMLProcessor {
1690
+ constructor() {
1691
+ this.handlers = {
1692
+ node: new Map(),
1693
+ wrap: new Map(),
1694
+ };
1695
+ }
1696
+ init() {
1697
+ // wrappers are used to wrap the content of nested text nodes
1698
+ // in a specific way
1699
+ this.wrap('link', (node, content) => {
1700
+ return `<a href="${node.attr.url || node.attr.href || '/'}">${content}</a>`;
1701
+ });
1702
+ this.wrap('header', (_node, content) => {
1703
+ return `<h3>${content}</h3>`;
1704
+ });
1705
+ this.wrap('emphasize', (_node, content) => {
1706
+ return `<i>${content}</i>`;
1707
+ });
1708
+ this.wrap('strong', (_node, content) => {
1709
+ return `<b>${content}</b>`;
1710
+ });
1711
+ // handlers are used to handle specific nodes
1712
+ // and return a list of content elements
1713
+ this.handle('default', (node) => {
1714
+ if (nodeNameIn(node, ['section', 'paragraph', 'line', 'header', 'emphasize', 'strong', 'link', 'li'])) {
1715
+ return this.handleNested(node);
1716
+ }
1717
+ });
1718
+ this.handle('text', (node) => {
1719
+ if (isTextNode(node)) {
1720
+ return [ContentElement.text(node.text)];
1721
+ }
1722
+ });
1723
+ this.handle('list', async (node) => {
1724
+ if (nodeNameIn(node, ['ul', 'ol'])) {
1725
+ const listType = node.name === 'ul' ? 'unordered' : 'ordered';
1726
+ return this.createList(node, listType);
1727
+ }
1728
+ });
1729
+ this.handle('table', (node) => {
1730
+ if (nodeNameIs(node, 'table')) {
1731
+ return this.handleTable(node);
1732
+ }
1733
+ });
1734
+ }
1735
+ async parse(xml) {
1736
+ const doc = new xmldoc.XmlDocument(xml);
1737
+ const elements = await this.process(doc);
1738
+ return elements || [];
1739
+ }
1740
+ handle(name, handler) {
1741
+ if (this.handlers.node.has(name)) {
1742
+ throw new Error(`${name} node handler already set`);
1743
+ }
1744
+ this.handlers.node.set(name, handler);
1745
+ }
1746
+ wrap(name, handler) {
1747
+ if (this.handlers.wrap.has(name)) {
1748
+ throw new Error(`${name} wrap handler already set`);
1749
+ }
1750
+ this.handlers.wrap.set(name, handler);
1751
+ }
1752
+ addTextAdditionalProperties(c, parent) {
1753
+ const additionalProperties = c.additional_properties || {};
1754
+ const parentNodeIsBlockElement = this.isBlockElement(parent);
1755
+ c.additional_properties = {
1756
+ ...c.additional_properties,
1757
+ isBlockElement: additionalProperties.isBlockElement || parentNodeIsBlockElement,
1758
+ };
1759
+ return c;
1760
+ }
1761
+ wrapChildrenTextNodes(node, elements) {
1762
+ const wrapped = [];
1763
+ for (const c of elements) {
1764
+ if (!isTextCE(c)) {
1765
+ wrapped.push(c);
1766
+ continue;
1767
+ }
1768
+ this.addTextAdditionalProperties(c, node);
1769
+ const handler = this.handlers.wrap.get(node.name);
1770
+ if (handler) {
1771
+ wrapped.push({
1772
+ ...c,
1773
+ content: handler(node, c.content),
1774
+ });
1775
+ }
1776
+ else {
1777
+ wrapped.push(c);
1778
+ }
1779
+ }
1780
+ return wrapped;
1781
+ }
1782
+ async handleNested(node) {
1783
+ const children = await Promise.all(node.children.map((child) => this.process(child)));
1784
+ const filtered = children.filter(Boolean).flat();
1785
+ const merged = this.mergeParagraphs(filtered);
1786
+ const wrapped = this.wrapChildrenTextNodes(node, merged);
1787
+ return wrapped;
1788
+ }
1789
+ async process(node) {
1790
+ let isKnownNode = false;
1791
+ const elements = [];
1792
+ for (const [name, handler] of this.handlers.node.entries()) {
1793
+ try {
1794
+ const result = await handler(node);
1795
+ if (Array.isArray(result)) {
1796
+ // if handler returns an array of elements, it means that the node was handled properly, even if there is no elements inside
1797
+ isKnownNode = true;
1798
+ elements.push(...result);
1799
+ break;
1800
+ }
1801
+ }
1802
+ catch (error) {
1803
+ this.warn({ node: node.toString(), error: error.toString(), name }, 'HandlerError');
1804
+ }
1805
+ }
1806
+ if (isKnownNode)
1807
+ return elements;
1808
+ this.warn({ node: node.toString(), type: node.type }, 'UnknownNodeError');
1809
+ }
1810
+ mergeParagraphs(items) {
1811
+ const merged = [];
1812
+ let toMerge = [];
1813
+ const merge = () => {
1814
+ if (!toMerge.length)
1815
+ return;
1816
+ const paragraph = toMerge.reduce((acc, p) => {
1817
+ return {
1818
+ ...p,
1819
+ content: acc.content + p.content,
1820
+ };
1821
+ }, { type: 'text', content: '' });
1822
+ merged.push(paragraph);
1823
+ toMerge = [];
1824
+ };
1825
+ for (let i = 0; i < items.length; i++) {
1826
+ const item = items[i];
1827
+ const isBlockElement = item.additional_properties?.isBlockElement;
1828
+ if (isTextCE(item) && !isBlockElement) {
1829
+ toMerge.push(item);
1830
+ }
1831
+ else {
1832
+ merge();
1833
+ merged.push(item);
1834
+ }
1835
+ }
1836
+ merge();
1837
+ return merged;
1838
+ }
1839
+ async handleTable(node) {
1840
+ const html = node.toString({ html: true });
1841
+ return [ContentElement.raw_html(html)];
1842
+ }
1843
+ async createQuote(node) {
1844
+ const items = await this.handleNested(node);
1845
+ return [ContentElement.quote(items)];
1846
+ }
1847
+ async createList(node, type) {
1848
+ const items = await this.handleNested(node);
1849
+ return [ContentElement.list(type, items)];
1850
+ }
1851
+ getNodeInnerText(node) {
1852
+ return node.children.map((n) => this.htmlFromNode(n).innerText.trim());
1853
+ }
1854
+ getNodeInnerHTML(node) {
1855
+ return node.children.map((n) => this.htmlFromNode(n).innerHTML.trim());
1856
+ }
1857
+ htmlFromNode(node) {
1858
+ return parse(node.toString({ html: true }));
1859
+ }
1860
+ getDecodedHTMLFromInnerNodes(node) {
1861
+ const encodedHtml = this.getNodeInnerText(node).join('');
1862
+ const decoded = decodeHTMLEntities(encodedHtml);
1863
+ return decoded;
1864
+ }
1865
+ isBlockElement(node) {
1866
+ const defaultBlockElements = new Set(BLOCK_ELEMENT_TAGS);
1867
+ if (defaultBlockElements.has(node.name))
1868
+ return true;
1869
+ }
1870
+ warn(metadata, message) {
1871
+ console.warn(metadata, message);
1872
+ }
1873
+ }
1874
+
1875
+ var index$3 = /*#__PURE__*/Object.freeze({
1876
+ __proto__: null,
1877
+ Constants: xml_constants,
1878
+ Utils: xml_utils,
1879
+ XMLProcessor: XMLProcessor
1880
+ });
1881
+
1659
1882
  var index$2 = /*#__PURE__*/Object.freeze({
1660
1883
  __proto__: null,
1661
- ContentElement: ContentElement,
1662
- HTML: index$3
1884
+ ContentElement: ContentElement$1,
1885
+ HTML: index$4,
1886
+ XML: index$3
1663
1887
  });
1664
1888
 
1665
1889
  /**