@easy-cms/richtext 1.13.0 → 1.14.0

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.
Files changed (2) hide show
  1. package/dist/index.js +55 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -92,17 +92,51 @@ function renderNode(node, options) {
92
92
  const title = typeof node.attrs?.title === "string" ? node.attrs.title : void 0;
93
93
  return `<img${attributes({ src, alt, title })}>`;
94
94
  }
95
+ case "table":
96
+ return renderTable(node, options);
97
+ case "tableRow":
98
+ return `<tr>${children}</tr>`;
99
+ case "tableHeader":
100
+ case "tableCell": {
101
+ const tag = node.type === "tableHeader" ? "th" : "td";
102
+ const span = (name) => {
103
+ const n = Number(node.attrs?.[name]);
104
+ return Number.isInteger(n) && n > 1 ? String(n) : void 0;
105
+ };
106
+ return `<${tag}${attributes({ colspan: span("colspan"), rowspan: span("rowspan") })}>${children}</${tag}>`;
107
+ }
95
108
  default:
96
109
  return children;
97
110
  }
98
111
  }
112
+ function headerRows(rows) {
113
+ const header = (row) => (row.content ?? []).length > 0 && (row.content ?? []).every((c) => c.type === "tableHeader");
114
+ const first = rows.findIndex((row) => !header(row));
115
+ return first === -1 ? rows.length : first;
116
+ }
117
+ function renderTable(node, options) {
118
+ const rows = (node.content ?? []).filter((r) => r?.type === "tableRow");
119
+ const head = headerRows(rows);
120
+ const html = (list) => list.map((r) => renderNode(r, options)).join("");
121
+ const thead = head > 0 ? `<thead>${html(rows.slice(0, head))}</thead>` : "";
122
+ const tbody = head < rows.length ? `<tbody>${html(rows.slice(head))}</tbody>` : "";
123
+ return `<table>${thead}${tbody}</table>`;
124
+ }
99
125
  function renderRichText(doc, options = {}) {
100
126
  if (!doc) return "";
101
127
  return renderNode(doc, options);
102
128
  }
103
129
  function richTextToPlainText(doc) {
104
130
  if (!doc) return "";
105
- const blocks = /* @__PURE__ */ new Set(["paragraph", "heading", "listItem", "blockquote", "codeBlock"]);
131
+ const blocks = /* @__PURE__ */ new Set([
132
+ "paragraph",
133
+ "heading",
134
+ "listItem",
135
+ "blockquote",
136
+ "codeBlock",
137
+ "tableHeader",
138
+ "tableCell"
139
+ ]);
106
140
  const walk = (node) => {
107
141
  if (node.type === "text") return String(node.text ?? "");
108
142
  if (node.type === "hardBreak") return "\n";
@@ -199,10 +233,30 @@ ${fence}`;
199
233
  const alt = typeof node.attrs?.alt === "string" ? escapeMarkdown(node.attrs.alt) : "";
200
234
  return `![${alt}](${src.replace(/[()\s]/g, urlChar)})`;
201
235
  }
236
+ case "table":
237
+ return markdownTable(node, options);
202
238
  default:
203
239
  return content.some((c) => c.type === "text") ? inline() : blocks();
204
240
  }
205
241
  }
242
+ function markdownTable(node, options) {
243
+ const rows = (node.content ?? []).filter((r) => r?.type === "tableRow");
244
+ const cells = rows.map(
245
+ (row) => (row.content ?? []).map(
246
+ (cell) => (cell.content ?? []).map(
247
+ (c) => (
248
+ // A cell holds one line: its lines are joined with spaces (no regex over user text).
249
+ markdownNode(c, options).split("\n").map((line2) => line2.trim()).filter(Boolean).join(" ")
250
+ )
251
+ ).filter((c) => c.trim() !== "").join("<br>")
252
+ )
253
+ );
254
+ const width = Math.max(0, ...cells.map((r) => r.length));
255
+ if (width === 0) return "";
256
+ const line = (r) => `| ${Array.from({ length: width }, (_, i) => r[i] ?? "").join(" | ")} |`;
257
+ const [first, ...rest] = headerRows(rows) > 0 ? cells : [[], ...cells];
258
+ return [line(first ?? []), `|${" --- |".repeat(width)}`, ...rest.map(line)].join("\n");
259
+ }
206
260
  function renderMarkdown(doc, options = {}) {
207
261
  if (!doc) return "";
208
262
  return markdownNode(doc, options).trim();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easy-cms/richtext",
3
- "version": "1.13.0",
3
+ "version": "1.14.0",
4
4
  "description": "Render Easy CMS rich text (Tiptap JSON) to safe HTML",
5
5
  "license": "MIT",
6
6
  "type": "module",