@ai-matrx/print 0.8.8 → 0.8.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/markdown.js CHANGED
@@ -22,6 +22,7 @@ function openPrintWindow(htmlDocument, filename = "print", windowFeatures = "wid
22
22
 
23
23
  // src/markdown.ts
24
24
  import { escapeHtml as escapeHtml2 } from "@ai-matrx/kit/html-escape";
25
+ import { findTableEnd, rowCells, tableStartsAt, unescapeCellPipes } from "@ai-matrx/content-ir/source";
25
26
 
26
27
  // src/safe-html.ts
27
28
  import { fromHtml } from "hast-util-from-html";
@@ -1531,12 +1532,6 @@ var LINK_RE = new RegExp(String.raw`\[([^\]]+)\]\(` + LINK_URL + String.raw`\)`,
1531
1532
  function convertRules(html, g) {
1532
1533
  return html.replace(/^\s{0,3}#\s*={3,}\s*$/gm, `<hr${cls(g.hr, "matrx-hr-heavy")}>`).replace(/^\s{0,3}\*(?:\s*\*){2,}\s*$/gm, `<hr${cls(g.hr, "matrx-hr-accent")}>`).replace(/^\s{0,3}(?:-\s*){3,}$/gm, `<hr${cls(g.hr)}>`).replace(/^\s{0,3}(?:_\s*){3,}$/gm, `<hr${cls(g.hr)}>`);
1533
1534
  }
1534
- function splitTableRow(row) {
1535
- let r = row.trim();
1536
- if (r.startsWith("|")) r = r.slice(1);
1537
- if (r.endsWith("|") && !r.endsWith("\\|")) r = r.slice(0, -1);
1538
- return r.split(/(?<!\\)\|/).map((c) => c.trim().replace(/\\\|/g, "|"));
1539
- }
1540
1535
  function cellAlign2(separator) {
1541
1536
  const s = separator.trim();
1542
1537
  const left = s.startsWith(":");
@@ -1546,40 +1541,48 @@ function cellAlign2(separator) {
1546
1541
  return "";
1547
1542
  }
1548
1543
  function convertTables(html, g) {
1549
- const tableRegex = /^(\|.*\|)\s*\n(\|[-\s:|]*\|)\s*\n((?:\|.*\|\s*\n?)*)/gm;
1550
- return html.replace(
1551
- tableRegex,
1552
- (_m, headerRow, separatorRow, bodyRows) => {
1553
- const headers = splitTableRow(headerRow);
1554
- const aligns = splitTableRow(separatorRow).map(cellAlign2);
1555
- const rows = bodyRows.trim().split("\n").filter((r) => r.trim()).map(splitTableRow);
1556
- let t = `<table${cls(g.table)}>
1544
+ const lines = html.split("\n");
1545
+ const out = [];
1546
+ for (let i = 0; i < lines.length; i += 1) {
1547
+ if (!tableStartsAt(lines, i)) {
1548
+ out.push(lines[i] ?? "");
1549
+ continue;
1550
+ }
1551
+ const end = findTableEnd(lines, i);
1552
+ const cellsOf = (line) => rowCells(line).map(unescapeCellPipes);
1553
+ const headers = cellsOf(lines[i] ?? "");
1554
+ const aligns = rowCells(lines[i + 1] ?? "").map(cellAlign2);
1555
+ const rows = lines.slice(i + 2, end).map((line) => {
1556
+ const cells = cellsOf(line);
1557
+ return headers.map((_h, k) => cells[k] ?? "");
1558
+ });
1559
+ let t = `<table${cls(g.table)}>
1557
1560
  <thead${cls(g.thead)}>
1558
1561
  <tr${cls(g.tr)}>
1559
1562
  `;
1560
- headers.forEach((h, i) => {
1561
- t += `<th${cls(g.th)}${aligns[i] ?? ""}>${h}</th>
1563
+ headers.forEach((h, k) => {
1564
+ t += `<th${cls(g.th)}${aligns[k] ?? ""}>${h}</th>
1562
1565
  `;
1563
- });
1564
- t += "</tr>\n</thead>\n";
1565
- if (rows.length > 0) {
1566
- t += `<tbody${cls(g.tbody)}>
1566
+ });
1567
+ t += "</tr>\n</thead>\n";
1568
+ if (rows.length > 0) {
1569
+ t += `<tbody${cls(g.tbody)}>
1567
1570
  `;
1568
- for (const row of rows) {
1569
- t += `<tr${cls(g.tr)}>
1571
+ for (const row of rows) {
1572
+ t += `<tr${cls(g.tr)}>
1570
1573
  `;
1571
- row.forEach((c, i) => {
1572
- t += `<td${cls(g.td)}${aligns[i] ?? ""}>${c}</td>
1574
+ row.forEach((c, k) => {
1575
+ t += `<td${cls(g.td)}${aligns[k] ?? ""}>${c}</td>
1573
1576
  `;
1574
- });
1575
- t += "</tr>\n";
1576
- }
1577
- t += "</tbody>\n";
1577
+ });
1578
+ t += "</tr>\n";
1578
1579
  }
1579
- return `${t}</table>
1580
- `;
1580
+ t += "</tbody>\n";
1581
1581
  }
1582
- );
1582
+ out.push(`${t}</table>`);
1583
+ i = end - 1;
1584
+ }
1585
+ return out.join("\n");
1583
1586
  }
1584
1587
  function convertLists(html, g) {
1585
1588
  const lines = html.split("\n");