rte-builder 2.0.8 → 2.0.9

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
@@ -1596,6 +1596,149 @@ var init_Indent = __esm({
1596
1596
  }
1597
1597
  });
1598
1598
 
1599
+ // src/utils/formatHtml.ts
1600
+ function tokenize(html) {
1601
+ const tokens = [];
1602
+ const re = /<!--[\s\S]*?-->|<!DOCTYPE[^>]*>|<\/([a-zA-Z][a-zA-Z0-9]*)\s*>|<([a-zA-Z][a-zA-Z0-9]*)(\s[^>]*)?\s*\/?>|[^<]+/gi;
1603
+ let match;
1604
+ while ((match = re.exec(html)) !== null) {
1605
+ const raw = match[0];
1606
+ if (raw.startsWith("<!--")) {
1607
+ tokens.push({ type: "comment", raw });
1608
+ } else if (raw.startsWith("<!")) {
1609
+ tokens.push({ type: "doctype", raw });
1610
+ } else if (match[1]) {
1611
+ tokens.push({ type: "close", tag: match[1].toLowerCase(), raw });
1612
+ } else if (match[2]) {
1613
+ const tag = match[2].toLowerCase();
1614
+ const isVoid = VOID_ELEMENTS.has(tag) || raw.endsWith("/>");
1615
+ tokens.push({
1616
+ type: isVoid ? "self-close" : "open",
1617
+ tag,
1618
+ raw
1619
+ });
1620
+ } else {
1621
+ const text = raw;
1622
+ if (text.trim()) {
1623
+ tokens.push({ type: "text", raw: text });
1624
+ }
1625
+ }
1626
+ }
1627
+ return tokens;
1628
+ }
1629
+ function formatHtml(html, indentStr = " ") {
1630
+ if (!html || !html.trim()) return html;
1631
+ const tokens = tokenize(html.trim());
1632
+ const lines = [];
1633
+ let depth = 0;
1634
+ for (const token of tokens) {
1635
+ const indent = indentStr.repeat(depth);
1636
+ switch (token.type) {
1637
+ case "open": {
1638
+ const isBlock = BLOCK_ELEMENTS.has(token.tag);
1639
+ if (isBlock) {
1640
+ lines.push(`${indent}${token.raw}`);
1641
+ depth++;
1642
+ } else {
1643
+ lines.push(`${indent}${token.raw}`);
1644
+ depth++;
1645
+ }
1646
+ break;
1647
+ }
1648
+ case "close": {
1649
+ depth = Math.max(0, depth - 1);
1650
+ const closeIndent = indentStr.repeat(depth);
1651
+ lines.push(`${closeIndent}${token.raw}`);
1652
+ break;
1653
+ }
1654
+ case "self-close":
1655
+ case "comment":
1656
+ case "doctype":
1657
+ lines.push(`${indent}${token.raw}`);
1658
+ break;
1659
+ case "text":
1660
+ lines.push(`${indent}${token.raw.trim()}`);
1661
+ break;
1662
+ }
1663
+ }
1664
+ return collapseInlineTags(lines.join("\n"));
1665
+ }
1666
+ function collapseInlineTags(formatted) {
1667
+ return formatted.replace(
1668
+ /^(\s*)<((?!\/)[a-zA-Z][a-zA-Z0-9]*)([^>]*)>\n\s+([^<\n]+)\n\s*<\/\2>/gm,
1669
+ (match, indent, tag, attrs, text) => {
1670
+ if (BLOCK_ELEMENTS.has(tag.toLowerCase())) return match;
1671
+ return `${indent}<${tag}${attrs}>${text.trim()}</${tag}>`;
1672
+ }
1673
+ );
1674
+ }
1675
+ var VOID_ELEMENTS, BLOCK_ELEMENTS;
1676
+ var init_formatHtml = __esm({
1677
+ "src/utils/formatHtml.ts"() {
1678
+ "use strict";
1679
+ VOID_ELEMENTS = /* @__PURE__ */ new Set([
1680
+ "area",
1681
+ "base",
1682
+ "br",
1683
+ "col",
1684
+ "embed",
1685
+ "hr",
1686
+ "img",
1687
+ "input",
1688
+ "link",
1689
+ "meta",
1690
+ "param",
1691
+ "source",
1692
+ "track",
1693
+ "wbr"
1694
+ ]);
1695
+ BLOCK_ELEMENTS = /* @__PURE__ */ new Set([
1696
+ "address",
1697
+ "article",
1698
+ "aside",
1699
+ "blockquote",
1700
+ "body",
1701
+ "details",
1702
+ "dialog",
1703
+ "dd",
1704
+ "div",
1705
+ "dl",
1706
+ "dt",
1707
+ "fieldset",
1708
+ "figcaption",
1709
+ "figure",
1710
+ "footer",
1711
+ "form",
1712
+ "h1",
1713
+ "h2",
1714
+ "h3",
1715
+ "h4",
1716
+ "h5",
1717
+ "h6",
1718
+ "head",
1719
+ "header",
1720
+ "hgroup",
1721
+ "hr",
1722
+ "html",
1723
+ "li",
1724
+ "main",
1725
+ "nav",
1726
+ "ol",
1727
+ "p",
1728
+ "pre",
1729
+ "section",
1730
+ "table",
1731
+ "tbody",
1732
+ "td",
1733
+ "tfoot",
1734
+ "th",
1735
+ "thead",
1736
+ "tr",
1737
+ "ul"
1738
+ ]);
1739
+ }
1740
+ });
1741
+
1599
1742
  // src/adapters/tiptap/TipTapToolbar.tsx
1600
1743
  import { useState, useRef, useEffect } from "react";
1601
1744
  import { CodeXml } from "lucide-react";
@@ -1605,6 +1748,7 @@ var init_TipTapToolbar = __esm({
1605
1748
  "src/adapters/tiptap/TipTapToolbar.tsx"() {
1606
1749
  "use strict";
1607
1750
  init_Emoji();
1751
+ init_formatHtml();
1608
1752
  FONT_FAMILIES = [
1609
1753
  { value: "Arial", label: "Arial" },
1610
1754
  { value: "Georgia", label: "Georgia" },
@@ -1809,12 +1953,13 @@ var init_TipTapToolbar = __esm({
1809
1953
  } else {
1810
1954
  const currentHTML = editor.getHTML();
1811
1955
  const isEmptyContent = !currentHTML || currentHTML === "<p></p>" || currentHTML.trim() === "";
1956
+ const formattedHTML = isEmptyContent ? "" : formatHtml(currentHTML);
1812
1957
  const codeBlockContent = isEmptyContent ? { type: "doc", content: [{ type: "codeBlock" }] } : {
1813
1958
  type: "doc",
1814
1959
  content: [
1815
1960
  {
1816
1961
  type: "codeBlock",
1817
- content: [{ type: "text", text: currentHTML }]
1962
+ content: [{ type: "text", text: formattedHTML }]
1818
1963
  }
1819
1964
  ]
1820
1965
  };
@@ -6026,6 +6171,7 @@ import { common as common2, createLowlight as createLowlight2 } from "lowlight";
6026
6171
 
6027
6172
  // src/components/Toolbar.tsx
6028
6173
  init_Emoji();
6174
+ init_formatHtml();
6029
6175
  import { useState as useState7, useRef as useRef7, useEffect as useEffect7 } from "react";
6030
6176
  import {
6031
6177
  Bold as Bold2,
@@ -6251,12 +6397,15 @@ var Toolbar = ({
6251
6397
  } else {
6252
6398
  const currentHTML = editor.getHTML();
6253
6399
  const isEmptyContent = !currentHTML || currentHTML === "<p></p>" || currentHTML.trim() === "";
6400
+ const formattedHTML = isEmptyContent ? "" : formatHtml(currentHTML);
6254
6401
  const codeBlockContent = isEmptyContent ? { type: "doc", content: [{ type: "codeBlock" }] } : {
6255
6402
  type: "doc",
6256
6403
  content: [
6257
6404
  {
6258
6405
  type: "codeBlock",
6259
- content: [{ type: "text", text: currentHTML }]
6406
+ content: [
6407
+ { type: "text", text: formattedHTML }
6408
+ ]
6260
6409
  }
6261
6410
  ]
6262
6411
  };