@examind/block-sdk 0.1.10 → 0.1.12

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
@@ -767,12 +767,21 @@ function hasFormat(format, type) {
767
767
  return (format & formatFlag) !== 0;
768
768
  }
769
769
  function encodeHTMLEntities(text) {
770
- return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
770
+ return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/\u00A0/g, "&nbsp;");
771
+ }
772
+ function preserveSpaces(text) {
773
+ return text.replace(/ {2,}/g, (match) => {
774
+ let result = "";
775
+ for (let i = 0; i < match.length; i++) {
776
+ result += i % 2 === 0 ? " " : "&nbsp;";
777
+ }
778
+ return result;
779
+ });
771
780
  }
772
781
  var TextNodeHandler = class extends NodeHandler {
773
782
  processNode(node) {
774
783
  if (!isSerializedTextNode(node)) return null;
775
- let result = encodeHTMLEntities(node.text);
784
+ let result = preserveSpaces(encodeHTMLEntities(node.text));
776
785
  if (hasFormat(node.format, "subscript")) {
777
786
  result = `<sub>${result}</sub>`;
778
787
  } else if (hasFormat(node.format, "superscript")) {
@@ -1715,27 +1724,15 @@ var import_node_html_parser20 = require("node-html-parser");
1715
1724
  var TextNode = import_node_html_parser20.parse.TextNode;
1716
1725
  var SpanNodeHandler = class extends NodeHandler2 {
1717
1726
  processNode(node) {
1718
- if (!(node instanceof import_node_html_parser20.HTMLElement)) return null;
1719
- if (node.tagName !== "SPAN") return null;
1720
- if (!node.hasAttribute("style")) return null;
1721
- const styleAttr = node.getAttribute("style");
1722
- if (!styleAttr) return null;
1727
+ if (!(node instanceof import_node_html_parser20.HTMLElement) || node.tagName !== "SPAN")
1728
+ return null;
1729
+ const styleAttr = node.getAttribute("style") || "";
1723
1730
  if (node.childNodes.length === 1 && node.childNodes[0] instanceof TextNode) {
1724
- const textContent2 = node.text;
1725
- return {
1726
- detail: 0,
1727
- format: 0,
1728
- mode: "normal",
1729
- style: styleAttr,
1730
- text: textContent2,
1731
- type: "text",
1732
- version: 1
1733
- };
1731
+ return this.createTextNode(node.text, styleAttr);
1734
1732
  }
1735
1733
  let childResult = null;
1736
1734
  for (const childNode of node.childNodes) {
1737
- const processedChildren = traverse2(childNode);
1738
- for (const child of processedChildren) {
1735
+ for (const child of traverse2(childNode)) {
1739
1736
  if (child.type !== "text") continue;
1740
1737
  const textNode = child;
1741
1738
  if (!childResult) {
@@ -1746,39 +1743,32 @@ var SpanNodeHandler = class extends NodeHandler2 {
1746
1743
  } else {
1747
1744
  childResult.text += textNode.text;
1748
1745
  childResult.format |= textNode.format || 0;
1749
- if (textNode.style) {
1750
- childResult.style = this.mergeStyles(
1751
- childResult.style,
1752
- textNode.style
1753
- );
1754
- }
1746
+ childResult.style = this.mergeStyles(
1747
+ childResult.style,
1748
+ textNode.style
1749
+ );
1755
1750
  }
1756
1751
  }
1757
1752
  }
1758
- if (childResult) {
1759
- childResult.style = this.mergeStyles(
1760
- childResult.style,
1761
- styleAttr
1762
- );
1763
- return childResult;
1764
- }
1765
- const textContent = node.text;
1766
- if (textContent.trim() !== "") {
1767
- return {
1768
- detail: 0,
1769
- format: 0,
1770
- mode: "normal",
1771
- style: styleAttr,
1772
- text: textContent,
1773
- type: "text",
1774
- version: 1
1775
- };
1776
- }
1777
- return null;
1753
+ if (childResult) return childResult;
1754
+ const textContent = node.text.trim();
1755
+ return textContent ? this.createTextNode(node.text, styleAttr) : null;
1756
+ }
1757
+ createTextNode(text, style) {
1758
+ return {
1759
+ detail: 0,
1760
+ format: 0,
1761
+ mode: "normal",
1762
+ style,
1763
+ text,
1764
+ type: "text",
1765
+ version: 1
1766
+ };
1778
1767
  }
1779
1768
  mergeStyles(existingStyle, newStyle) {
1780
- if (!existingStyle) return newStyle;
1781
- if (!newStyle) return existingStyle;
1769
+ if (!existingStyle && !newStyle) return "";
1770
+ if (!existingStyle) return newStyle ?? "";
1771
+ if (!newStyle) return existingStyle ?? "";
1782
1772
  return `${existingStyle};${newStyle}`.replace(/;;/g, ";").replace(/^;/, "").replace(/;$/, "");
1783
1773
  }
1784
1774
  };
package/dist/index.mjs CHANGED
@@ -740,12 +740,21 @@ function hasFormat(format, type) {
740
740
  return (format & formatFlag) !== 0;
741
741
  }
742
742
  function encodeHTMLEntities(text) {
743
- return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
743
+ return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/\u00A0/g, "&nbsp;");
744
+ }
745
+ function preserveSpaces(text) {
746
+ return text.replace(/ {2,}/g, (match) => {
747
+ let result = "";
748
+ for (let i = 0; i < match.length; i++) {
749
+ result += i % 2 === 0 ? " " : "&nbsp;";
750
+ }
751
+ return result;
752
+ });
744
753
  }
745
754
  var TextNodeHandler = class extends NodeHandler {
746
755
  processNode(node) {
747
756
  if (!isSerializedTextNode(node)) return null;
748
- let result = encodeHTMLEntities(node.text);
757
+ let result = preserveSpaces(encodeHTMLEntities(node.text));
749
758
  if (hasFormat(node.format, "subscript")) {
750
759
  result = `<sub>${result}</sub>`;
751
760
  } else if (hasFormat(node.format, "superscript")) {
@@ -1688,27 +1697,15 @@ import { HTMLElement as HTMLElement20, parse } from "node-html-parser";
1688
1697
  var TextNode = parse.TextNode;
1689
1698
  var SpanNodeHandler = class extends NodeHandler2 {
1690
1699
  processNode(node) {
1691
- if (!(node instanceof HTMLElement20)) return null;
1692
- if (node.tagName !== "SPAN") return null;
1693
- if (!node.hasAttribute("style")) return null;
1694
- const styleAttr = node.getAttribute("style");
1695
- if (!styleAttr) return null;
1700
+ if (!(node instanceof HTMLElement20) || node.tagName !== "SPAN")
1701
+ return null;
1702
+ const styleAttr = node.getAttribute("style") || "";
1696
1703
  if (node.childNodes.length === 1 && node.childNodes[0] instanceof TextNode) {
1697
- const textContent2 = node.text;
1698
- return {
1699
- detail: 0,
1700
- format: 0,
1701
- mode: "normal",
1702
- style: styleAttr,
1703
- text: textContent2,
1704
- type: "text",
1705
- version: 1
1706
- };
1704
+ return this.createTextNode(node.text, styleAttr);
1707
1705
  }
1708
1706
  let childResult = null;
1709
1707
  for (const childNode of node.childNodes) {
1710
- const processedChildren = traverse2(childNode);
1711
- for (const child of processedChildren) {
1708
+ for (const child of traverse2(childNode)) {
1712
1709
  if (child.type !== "text") continue;
1713
1710
  const textNode = child;
1714
1711
  if (!childResult) {
@@ -1719,39 +1716,32 @@ var SpanNodeHandler = class extends NodeHandler2 {
1719
1716
  } else {
1720
1717
  childResult.text += textNode.text;
1721
1718
  childResult.format |= textNode.format || 0;
1722
- if (textNode.style) {
1723
- childResult.style = this.mergeStyles(
1724
- childResult.style,
1725
- textNode.style
1726
- );
1727
- }
1719
+ childResult.style = this.mergeStyles(
1720
+ childResult.style,
1721
+ textNode.style
1722
+ );
1728
1723
  }
1729
1724
  }
1730
1725
  }
1731
- if (childResult) {
1732
- childResult.style = this.mergeStyles(
1733
- childResult.style,
1734
- styleAttr
1735
- );
1736
- return childResult;
1737
- }
1738
- const textContent = node.text;
1739
- if (textContent.trim() !== "") {
1740
- return {
1741
- detail: 0,
1742
- format: 0,
1743
- mode: "normal",
1744
- style: styleAttr,
1745
- text: textContent,
1746
- type: "text",
1747
- version: 1
1748
- };
1749
- }
1750
- return null;
1726
+ if (childResult) return childResult;
1727
+ const textContent = node.text.trim();
1728
+ return textContent ? this.createTextNode(node.text, styleAttr) : null;
1729
+ }
1730
+ createTextNode(text, style) {
1731
+ return {
1732
+ detail: 0,
1733
+ format: 0,
1734
+ mode: "normal",
1735
+ style,
1736
+ text,
1737
+ type: "text",
1738
+ version: 1
1739
+ };
1751
1740
  }
1752
1741
  mergeStyles(existingStyle, newStyle) {
1753
- if (!existingStyle) return newStyle;
1754
- if (!newStyle) return existingStyle;
1742
+ if (!existingStyle && !newStyle) return "";
1743
+ if (!existingStyle) return newStyle ?? "";
1744
+ if (!newStyle) return existingStyle ?? "";
1755
1745
  return `${existingStyle};${newStyle}`.replace(/;;/g, ";").replace(/^;/, "").replace(/;$/, "");
1756
1746
  }
1757
1747
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@examind/block-sdk",
3
- "version": "0.1.10",
3
+ "version": "0.1.12",
4
4
  "@comment version": [
5
5
  "Don't specify package version here. It will be injected by publish workflow."
6
6
  ],