rte-builder 2.0.8 → 2.0.10

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
@@ -1601,6 +1601,149 @@ var init_Indent = __esm({
1601
1601
  }
1602
1602
  });
1603
1603
 
1604
+ // src/utils/formatHtml.ts
1605
+ function tokenize(html) {
1606
+ const tokens = [];
1607
+ const re = /<!--[\s\S]*?-->|<!DOCTYPE[^>]*>|<\/([a-zA-Z][a-zA-Z0-9]*)\s*>|<([a-zA-Z][a-zA-Z0-9]*)(\s[^>]*)?\s*\/?>|[^<]+/gi;
1608
+ let match;
1609
+ while ((match = re.exec(html)) !== null) {
1610
+ const raw = match[0];
1611
+ if (raw.startsWith("<!--")) {
1612
+ tokens.push({ type: "comment", raw });
1613
+ } else if (raw.startsWith("<!")) {
1614
+ tokens.push({ type: "doctype", raw });
1615
+ } else if (match[1]) {
1616
+ tokens.push({ type: "close", tag: match[1].toLowerCase(), raw });
1617
+ } else if (match[2]) {
1618
+ const tag = match[2].toLowerCase();
1619
+ const isVoid = VOID_ELEMENTS.has(tag) || raw.endsWith("/>");
1620
+ tokens.push({
1621
+ type: isVoid ? "self-close" : "open",
1622
+ tag,
1623
+ raw
1624
+ });
1625
+ } else {
1626
+ const text = raw;
1627
+ if (text.trim()) {
1628
+ tokens.push({ type: "text", raw: text });
1629
+ }
1630
+ }
1631
+ }
1632
+ return tokens;
1633
+ }
1634
+ function formatHtml(html, indentStr = " ") {
1635
+ if (!html || !html.trim()) return html;
1636
+ const tokens = tokenize(html.trim());
1637
+ const lines = [];
1638
+ let depth = 0;
1639
+ for (const token of tokens) {
1640
+ const indent = indentStr.repeat(depth);
1641
+ switch (token.type) {
1642
+ case "open": {
1643
+ const isBlock = BLOCK_ELEMENTS.has(token.tag);
1644
+ if (isBlock) {
1645
+ lines.push(`${indent}${token.raw}`);
1646
+ depth++;
1647
+ } else {
1648
+ lines.push(`${indent}${token.raw}`);
1649
+ depth++;
1650
+ }
1651
+ break;
1652
+ }
1653
+ case "close": {
1654
+ depth = Math.max(0, depth - 1);
1655
+ const closeIndent = indentStr.repeat(depth);
1656
+ lines.push(`${closeIndent}${token.raw}`);
1657
+ break;
1658
+ }
1659
+ case "self-close":
1660
+ case "comment":
1661
+ case "doctype":
1662
+ lines.push(`${indent}${token.raw}`);
1663
+ break;
1664
+ case "text":
1665
+ lines.push(`${indent}${token.raw.trim()}`);
1666
+ break;
1667
+ }
1668
+ }
1669
+ return collapseInlineTags(lines.join("\n"));
1670
+ }
1671
+ function collapseInlineTags(formatted) {
1672
+ return formatted.replace(
1673
+ /^(\s*)<((?!\/)[a-zA-Z][a-zA-Z0-9]*)([^>]*)>\n\s+([^<\n]+)\n\s*<\/\2>/gm,
1674
+ (match, indent, tag, attrs, text) => {
1675
+ if (BLOCK_ELEMENTS.has(tag.toLowerCase())) return match;
1676
+ return `${indent}<${tag}${attrs}>${text.trim()}</${tag}>`;
1677
+ }
1678
+ );
1679
+ }
1680
+ var VOID_ELEMENTS, BLOCK_ELEMENTS;
1681
+ var init_formatHtml = __esm({
1682
+ "src/utils/formatHtml.ts"() {
1683
+ "use strict";
1684
+ VOID_ELEMENTS = /* @__PURE__ */ new Set([
1685
+ "area",
1686
+ "base",
1687
+ "br",
1688
+ "col",
1689
+ "embed",
1690
+ "hr",
1691
+ "img",
1692
+ "input",
1693
+ "link",
1694
+ "meta",
1695
+ "param",
1696
+ "source",
1697
+ "track",
1698
+ "wbr"
1699
+ ]);
1700
+ BLOCK_ELEMENTS = /* @__PURE__ */ new Set([
1701
+ "address",
1702
+ "article",
1703
+ "aside",
1704
+ "blockquote",
1705
+ "body",
1706
+ "details",
1707
+ "dialog",
1708
+ "dd",
1709
+ "div",
1710
+ "dl",
1711
+ "dt",
1712
+ "fieldset",
1713
+ "figcaption",
1714
+ "figure",
1715
+ "footer",
1716
+ "form",
1717
+ "h1",
1718
+ "h2",
1719
+ "h3",
1720
+ "h4",
1721
+ "h5",
1722
+ "h6",
1723
+ "head",
1724
+ "header",
1725
+ "hgroup",
1726
+ "hr",
1727
+ "html",
1728
+ "li",
1729
+ "main",
1730
+ "nav",
1731
+ "ol",
1732
+ "p",
1733
+ "pre",
1734
+ "section",
1735
+ "table",
1736
+ "tbody",
1737
+ "td",
1738
+ "tfoot",
1739
+ "th",
1740
+ "thead",
1741
+ "tr",
1742
+ "ul"
1743
+ ]);
1744
+ }
1745
+ });
1746
+
1604
1747
  // src/adapters/tiptap/TipTapToolbar.tsx
1605
1748
  var import_react, import_lucide_react, import_jsx_runtime, FONT_FAMILIES, FONT_SIZES, LINE_HEIGHTS, EmojiPicker, TipTapToolbar;
1606
1749
  var init_TipTapToolbar = __esm({
@@ -1608,6 +1751,7 @@ var init_TipTapToolbar = __esm({
1608
1751
  "use strict";
1609
1752
  import_react = require("react");
1610
1753
  init_Emoji();
1754
+ init_formatHtml();
1611
1755
  import_lucide_react = require("lucide-react");
1612
1756
  import_jsx_runtime = require("react/jsx-runtime");
1613
1757
  FONT_FAMILIES = [
@@ -1814,12 +1958,13 @@ var init_TipTapToolbar = __esm({
1814
1958
  } else {
1815
1959
  const currentHTML = editor.getHTML();
1816
1960
  const isEmptyContent = !currentHTML || currentHTML === "<p></p>" || currentHTML.trim() === "";
1961
+ const formattedHTML = isEmptyContent ? "" : formatHtml(currentHTML);
1817
1962
  const codeBlockContent = isEmptyContent ? { type: "doc", content: [{ type: "codeBlock" }] } : {
1818
1963
  type: "doc",
1819
1964
  content: [
1820
1965
  {
1821
1966
  type: "codeBlock",
1822
- content: [{ type: "text", text: currentHTML }]
1967
+ content: [{ type: "text", text: formattedHTML }]
1823
1968
  }
1824
1969
  ]
1825
1970
  };
@@ -6051,6 +6196,7 @@ init_Indent();
6051
6196
  // src/components/Toolbar.tsx
6052
6197
  var import_react9 = require("react");
6053
6198
  init_Emoji();
6199
+ init_formatHtml();
6054
6200
  var import_lucide_react2 = require("lucide-react");
6055
6201
  var import_jsx_runtime8 = require("react/jsx-runtime");
6056
6202
  var FONT_FAMILIES2 = [
@@ -6241,12 +6387,15 @@ var Toolbar = ({
6241
6387
  } else {
6242
6388
  const currentHTML = editor.getHTML();
6243
6389
  const isEmptyContent = !currentHTML || currentHTML === "<p></p>" || currentHTML.trim() === "";
6390
+ const formattedHTML = isEmptyContent ? "" : formatHtml(currentHTML);
6244
6391
  const codeBlockContent = isEmptyContent ? { type: "doc", content: [{ type: "codeBlock" }] } : {
6245
6392
  type: "doc",
6246
6393
  content: [
6247
6394
  {
6248
6395
  type: "codeBlock",
6249
- content: [{ type: "text", text: currentHTML }]
6396
+ content: [
6397
+ { type: "text", text: formattedHTML }
6398
+ ]
6250
6399
  }
6251
6400
  ]
6252
6401
  };