@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.
@@ -1,2 +1,3 @@
1
1
  export * from './content-elements.js';
2
2
  export * as HTML from './html/index.js';
3
+ export * as XML from './xml/index.js';
package/dist/index.cjs CHANGED
@@ -11,6 +11,7 @@ var FormData = require('form-data');
11
11
  var ws = require('ws');
12
12
  var nodeHtmlParser = require('node-html-parser');
13
13
  var htmlEntities = require('html-entities');
14
+ var xmldoc = require('xmldoc');
14
15
  var encode = require('base32-encode');
15
16
  var uuid = require('uuid');
16
17
  var assert = require('node:assert');
@@ -34,6 +35,7 @@ function _interopNamespaceDefault(e) {
34
35
 
35
36
  var rateLimit__namespace = /*#__PURE__*/_interopNamespaceDefault(rateLimit);
36
37
  var ws__namespace = /*#__PURE__*/_interopNamespaceDefault(ws);
38
+ var xmldoc__namespace = /*#__PURE__*/_interopNamespaceDefault(xmldoc);
37
39
 
38
40
  const safeJSONStringify = (data) => {
39
41
  try {
@@ -913,7 +915,7 @@ const ArcAPI = (options) => {
913
915
  return API;
914
916
  };
915
917
 
916
- const ContentElement = {
918
+ const ContentElement$1 = {
917
919
  divider: () => {
918
920
  return {
919
921
  type: 'divider',
@@ -1145,7 +1147,7 @@ const ContentElement = {
1145
1147
  },
1146
1148
  };
1147
1149
 
1148
- const BLOCK_ELEMENT_TAGS = [
1150
+ const BLOCK_ELEMENT_TAGS$1 = [
1149
1151
  'ADDRESS',
1150
1152
  'ARTICLE',
1151
1153
  'ASIDE',
@@ -1184,7 +1186,7 @@ const BLOCK_ELEMENT_TAGS = [
1184
1186
 
1185
1187
  var html_constants = /*#__PURE__*/Object.freeze({
1186
1188
  __proto__: null,
1187
- BLOCK_ELEMENT_TAGS: BLOCK_ELEMENT_TAGS
1189
+ BLOCK_ELEMENT_TAGS: BLOCK_ELEMENT_TAGS$1
1188
1190
  });
1189
1191
 
1190
1192
  const socialRegExps = {
@@ -1228,27 +1230,27 @@ function createSocial(url = '') {
1228
1230
  const embeds = [];
1229
1231
  const instagram = instagramURLParser(url);
1230
1232
  if (instagram) {
1231
- embeds.push(ContentElement.instagram(instagram));
1233
+ embeds.push(ContentElement$1.instagram(instagram));
1232
1234
  }
1233
1235
  const twitter = twitterURLParser(url);
1234
1236
  if (twitter) {
1235
- embeds.push(ContentElement.twitter(twitter));
1237
+ embeds.push(ContentElement$1.twitter(twitter));
1236
1238
  }
1237
1239
  const tiktok = tiktokURLParser(url);
1238
1240
  if (tiktok) {
1239
- embeds.push(ContentElement.tiktok(tiktok));
1241
+ embeds.push(ContentElement$1.tiktok(tiktok));
1240
1242
  }
1241
1243
  const youtube = youtubeURLParser(url);
1242
1244
  if (youtube) {
1243
- embeds.push(ContentElement.youtube(youtube));
1245
+ embeds.push(ContentElement$1.youtube(youtube));
1244
1246
  }
1245
1247
  const facebookPost = facebookPostURLParser(url);
1246
1248
  if (facebookPost) {
1247
- embeds.push(ContentElement.facebook_post(facebookPost));
1249
+ embeds.push(ContentElement$1.facebook_post(facebookPost));
1248
1250
  }
1249
1251
  const facebookVideo = facebookVideoURLParser(url);
1250
1252
  if (facebookVideo) {
1251
- embeds.push(ContentElement.facebook_video(facebookVideo));
1253
+ embeds.push(ContentElement$1.facebook_video(facebookVideo));
1252
1254
  }
1253
1255
  return embeds;
1254
1256
  }
@@ -1272,7 +1274,7 @@ var ContentElements = /*#__PURE__*/Object.freeze({
1272
1274
  youtubeURLParser: youtubeURLParser
1273
1275
  });
1274
1276
 
1275
- const isTextNode = (node) => {
1277
+ const isTextNode$1 = (node) => {
1276
1278
  return node instanceof nodeHtmlParser.TextNode;
1277
1279
  };
1278
1280
  const isHTMLElement = (node) => {
@@ -1306,7 +1308,7 @@ var html_utils = /*#__PURE__*/Object.freeze({
1306
1308
  htmlToText: htmlToText,
1307
1309
  isCommentNode: isCommentNode,
1308
1310
  isHTMLElement: isHTMLElement,
1309
- isTextNode: isTextNode,
1311
+ isTextNode: isTextNode$1,
1310
1312
  nodeTagIn: nodeTagIn,
1311
1313
  nodeTagIs: nodeTagIs
1312
1314
  });
@@ -1454,7 +1456,7 @@ class HTMLProcessor {
1454
1456
  }
1455
1457
  });
1456
1458
  this.handle('text', (node) => {
1457
- if (isTextNode(node)) {
1459
+ if (isTextNode$1(node)) {
1458
1460
  return this.createText(node);
1459
1461
  }
1460
1462
  });
@@ -1632,23 +1634,23 @@ class HTMLProcessor {
1632
1634
  return [];
1633
1635
  }
1634
1636
  async handleTable(node) {
1635
- return [ContentElement.raw_html(node.toString())];
1637
+ return [ContentElement$1.raw_html(node.toString())];
1636
1638
  }
1637
1639
  async handleIframe(node) {
1638
- return [ContentElement.raw_html(node.toString())];
1640
+ return [ContentElement$1.raw_html(node.toString())];
1639
1641
  }
1640
1642
  async handleImage(node) {
1641
- return [ContentElement.raw_html(node.toString())];
1643
+ return [ContentElement$1.raw_html(node.toString())];
1642
1644
  }
1643
1645
  async handleBreak(_) {
1644
- return [ContentElement.divider()];
1646
+ return [ContentElement$1.divider()];
1645
1647
  }
1646
1648
  async createQuote(node) {
1647
1649
  const items = await this.handleNested(node);
1648
- return [ContentElement.quote(items)];
1650
+ return [ContentElement$1.quote(items)];
1649
1651
  }
1650
1652
  async createText(node) {
1651
- const text = ContentElement.text(node.text);
1653
+ const text = ContentElement$1.text(node.text);
1652
1654
  return [text];
1653
1655
  }
1654
1656
  filterListItems(items) {
@@ -1656,16 +1658,16 @@ class HTMLProcessor {
1656
1658
  }
1657
1659
  async createList(node, type) {
1658
1660
  const items = await this.handleNested(node);
1659
- return [ContentElement.list(type, this.filterListItems(items))];
1661
+ return [ContentElement$1.list(type, this.filterListItems(items))];
1660
1662
  }
1661
1663
  async createHeader(node) {
1662
1664
  const level = +node.tagName.split('H')[1] || 3;
1663
- return [ContentElement.header(node.innerText, level)];
1665
+ return [ContentElement$1.header(node.innerText, level)];
1664
1666
  }
1665
1667
  isBlockElement(node) {
1666
1668
  if (!isHTMLElement(node))
1667
1669
  return false;
1668
- const defaultBlockElements = new Set(BLOCK_ELEMENT_TAGS);
1670
+ const defaultBlockElements = new Set(BLOCK_ELEMENT_TAGS$1);
1669
1671
  return defaultBlockElements.has(node.tagName);
1670
1672
  }
1671
1673
  warn(metadata, message) {
@@ -1673,17 +1675,240 @@ class HTMLProcessor {
1673
1675
  }
1674
1676
  }
1675
1677
 
1676
- var index$3 = /*#__PURE__*/Object.freeze({
1678
+ var index$4 = /*#__PURE__*/Object.freeze({
1677
1679
  __proto__: null,
1678
1680
  Constants: html_constants,
1679
1681
  HTMLProcessor: HTMLProcessor,
1680
1682
  Utils: html_utils
1681
1683
  });
1682
1684
 
1685
+ const BLOCK_ELEMENT_TAGS = ['paragraph', 'line', 'header', 'ul', 'ol', 'li', 'embed', 'iframe', 'table'];
1686
+
1687
+ var xml_constants = /*#__PURE__*/Object.freeze({
1688
+ __proto__: null,
1689
+ BLOCK_ELEMENT_TAGS: BLOCK_ELEMENT_TAGS
1690
+ });
1691
+
1692
+ const isXmlElement = (node) => {
1693
+ return node?.type === 'element';
1694
+ };
1695
+ const isTextNode = (node) => {
1696
+ return node?.type === 'text';
1697
+ };
1698
+ const nodeNameIs = (node, name) => {
1699
+ return isXmlElement(node) && node.name === name;
1700
+ };
1701
+ const nodeNameIn = (node, names) => {
1702
+ return isXmlElement(node) && names.includes(node.name);
1703
+ };
1704
+
1705
+ var xml_utils = /*#__PURE__*/Object.freeze({
1706
+ __proto__: null,
1707
+ isTextNode: isTextNode,
1708
+ isXmlElement: isXmlElement,
1709
+ nodeNameIn: nodeNameIn,
1710
+ nodeNameIs: nodeNameIs
1711
+ });
1712
+
1713
+ const ContentElement = ContentElement$1;
1714
+ class XMLProcessor {
1715
+ constructor() {
1716
+ this.handlers = {
1717
+ node: new Map(),
1718
+ wrap: new Map(),
1719
+ };
1720
+ }
1721
+ init() {
1722
+ // wrappers are used to wrap the content of nested text nodes
1723
+ // in a specific way
1724
+ this.wrap('link', (node, content) => {
1725
+ return `<a href="${node.attr.url || node.attr.href || '/'}">${content}</a>`;
1726
+ });
1727
+ this.wrap('header', (_node, content) => {
1728
+ return `<h3>${content}</h3>`;
1729
+ });
1730
+ this.wrap('emphasize', (_node, content) => {
1731
+ return `<i>${content}</i>`;
1732
+ });
1733
+ this.wrap('strong', (_node, content) => {
1734
+ return `<b>${content}</b>`;
1735
+ });
1736
+ // handlers are used to handle specific nodes
1737
+ // and return a list of content elements
1738
+ this.handle('default', (node) => {
1739
+ if (nodeNameIn(node, ['section', 'paragraph', 'line', 'header', 'emphasize', 'strong', 'link', 'li'])) {
1740
+ return this.handleNested(node);
1741
+ }
1742
+ });
1743
+ this.handle('text', (node) => {
1744
+ if (isTextNode(node)) {
1745
+ return [ContentElement.text(node.text)];
1746
+ }
1747
+ });
1748
+ this.handle('list', async (node) => {
1749
+ if (nodeNameIn(node, ['ul', 'ol'])) {
1750
+ const listType = node.name === 'ul' ? 'unordered' : 'ordered';
1751
+ return this.createList(node, listType);
1752
+ }
1753
+ });
1754
+ this.handle('table', (node) => {
1755
+ if (nodeNameIs(node, 'table')) {
1756
+ return this.handleTable(node);
1757
+ }
1758
+ });
1759
+ }
1760
+ async parse(xml) {
1761
+ const doc = new xmldoc__namespace.XmlDocument(xml);
1762
+ const elements = await this.process(doc);
1763
+ return elements || [];
1764
+ }
1765
+ handle(name, handler) {
1766
+ if (this.handlers.node.has(name)) {
1767
+ throw new Error(`${name} node handler already set`);
1768
+ }
1769
+ this.handlers.node.set(name, handler);
1770
+ }
1771
+ wrap(name, handler) {
1772
+ if (this.handlers.wrap.has(name)) {
1773
+ throw new Error(`${name} wrap handler already set`);
1774
+ }
1775
+ this.handlers.wrap.set(name, handler);
1776
+ }
1777
+ addTextAdditionalProperties(c, parent) {
1778
+ const additionalProperties = c.additional_properties || {};
1779
+ const parentNodeIsBlockElement = this.isBlockElement(parent);
1780
+ c.additional_properties = {
1781
+ ...c.additional_properties,
1782
+ isBlockElement: additionalProperties.isBlockElement || parentNodeIsBlockElement,
1783
+ };
1784
+ return c;
1785
+ }
1786
+ wrapChildrenTextNodes(node, elements) {
1787
+ const wrapped = [];
1788
+ for (const c of elements) {
1789
+ if (!isTextCE(c)) {
1790
+ wrapped.push(c);
1791
+ continue;
1792
+ }
1793
+ this.addTextAdditionalProperties(c, node);
1794
+ const handler = this.handlers.wrap.get(node.name);
1795
+ if (handler) {
1796
+ wrapped.push({
1797
+ ...c,
1798
+ content: handler(node, c.content),
1799
+ });
1800
+ }
1801
+ else {
1802
+ wrapped.push(c);
1803
+ }
1804
+ }
1805
+ return wrapped;
1806
+ }
1807
+ async handleNested(node) {
1808
+ const children = await Promise.all(node.children.map((child) => this.process(child)));
1809
+ const filtered = children.filter(Boolean).flat();
1810
+ const merged = this.mergeParagraphs(filtered);
1811
+ const wrapped = this.wrapChildrenTextNodes(node, merged);
1812
+ return wrapped;
1813
+ }
1814
+ async process(node) {
1815
+ let isKnownNode = false;
1816
+ const elements = [];
1817
+ for (const [name, handler] of this.handlers.node.entries()) {
1818
+ try {
1819
+ const result = await handler(node);
1820
+ if (Array.isArray(result)) {
1821
+ // if handler returns an array of elements, it means that the node was handled properly, even if there is no elements inside
1822
+ isKnownNode = true;
1823
+ elements.push(...result);
1824
+ break;
1825
+ }
1826
+ }
1827
+ catch (error) {
1828
+ this.warn({ node: node.toString(), error: error.toString(), name }, 'HandlerError');
1829
+ }
1830
+ }
1831
+ if (isKnownNode)
1832
+ return elements;
1833
+ this.warn({ node: node.toString(), type: node.type }, 'UnknownNodeError');
1834
+ }
1835
+ mergeParagraphs(items) {
1836
+ const merged = [];
1837
+ let toMerge = [];
1838
+ const merge = () => {
1839
+ if (!toMerge.length)
1840
+ return;
1841
+ const paragraph = toMerge.reduce((acc, p) => {
1842
+ return {
1843
+ ...p,
1844
+ content: acc.content + p.content,
1845
+ };
1846
+ }, { type: 'text', content: '' });
1847
+ merged.push(paragraph);
1848
+ toMerge = [];
1849
+ };
1850
+ for (let i = 0; i < items.length; i++) {
1851
+ const item = items[i];
1852
+ const isBlockElement = item.additional_properties?.isBlockElement;
1853
+ if (isTextCE(item) && !isBlockElement) {
1854
+ toMerge.push(item);
1855
+ }
1856
+ else {
1857
+ merge();
1858
+ merged.push(item);
1859
+ }
1860
+ }
1861
+ merge();
1862
+ return merged;
1863
+ }
1864
+ async handleTable(node) {
1865
+ const html = node.toString({ html: true });
1866
+ return [ContentElement.raw_html(html)];
1867
+ }
1868
+ async createQuote(node) {
1869
+ const items = await this.handleNested(node);
1870
+ return [ContentElement.quote(items)];
1871
+ }
1872
+ async createList(node, type) {
1873
+ const items = await this.handleNested(node);
1874
+ return [ContentElement.list(type, items)];
1875
+ }
1876
+ getNodeInnerText(node) {
1877
+ return node.children.map((n) => this.htmlFromNode(n).innerText.trim());
1878
+ }
1879
+ getNodeInnerHTML(node) {
1880
+ return node.children.map((n) => this.htmlFromNode(n).innerHTML.trim());
1881
+ }
1882
+ htmlFromNode(node) {
1883
+ return nodeHtmlParser.parse(node.toString({ html: true }));
1884
+ }
1885
+ getDecodedHTMLFromInnerNodes(node) {
1886
+ const encodedHtml = this.getNodeInnerText(node).join('');
1887
+ const decoded = decodeHTMLEntities(encodedHtml);
1888
+ return decoded;
1889
+ }
1890
+ isBlockElement(node) {
1891
+ const defaultBlockElements = new Set(BLOCK_ELEMENT_TAGS);
1892
+ if (defaultBlockElements.has(node.name))
1893
+ return true;
1894
+ }
1895
+ warn(metadata, message) {
1896
+ console.warn(metadata, message);
1897
+ }
1898
+ }
1899
+
1900
+ var index$3 = /*#__PURE__*/Object.freeze({
1901
+ __proto__: null,
1902
+ Constants: xml_constants,
1903
+ Utils: xml_utils,
1904
+ XMLProcessor: XMLProcessor
1905
+ });
1906
+
1683
1907
  var index$2 = /*#__PURE__*/Object.freeze({
1684
1908
  __proto__: null,
1685
- ContentElement: ContentElement,
1686
- HTML: index$3
1909
+ ContentElement: ContentElement$1,
1910
+ HTML: index$4,
1911
+ XML: index$3
1687
1912
  });
1688
1913
 
1689
1914
  /**