@ai-matrx/print 0.8.8 → 0.8.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/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 { codeSpanText, findTableEnd, mapCodeRanges, 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";
@@ -1444,6 +1445,12 @@ function protectRegions(markdown, g, stash, ctx) {
1444
1445
  i = j;
1445
1446
  continue;
1446
1447
  }
1448
+ if (tableStartsAt(lines, i)) {
1449
+ const end = findTableEnd(lines, i);
1450
+ out.push(stash.block(tableHtml(lines.slice(i, end), g, stash)));
1451
+ i = end - 1;
1452
+ continue;
1453
+ }
1447
1454
  out.push(line);
1448
1455
  }
1449
1456
  let text = out.join("\n");
@@ -1451,6 +1458,7 @@ function protectRegions(markdown, g, stash, ctx) {
1451
1458
  /`([^`\n]+)`/g,
1452
1459
  (_m, code) => stash.inline(`<code${cls(g.inlineCode)}>${escapeHtml2(code)}</code>`)
1453
1460
  );
1461
+ text = resolveEscapes(text, stash);
1454
1462
  text = text.replace(/<!--[\s\S]*?-->/g, "");
1455
1463
  const before = text;
1456
1464
  for (let pass = 0; pass < 8; pass++) {
@@ -1531,12 +1539,6 @@ var LINK_RE = new RegExp(String.raw`\[([^\]]+)\]\(` + LINK_URL + String.raw`\)`,
1531
1539
  function convertRules(html, g) {
1532
1540
  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
1541
  }
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
1542
  function cellAlign2(separator) {
1541
1543
  const s = separator.trim();
1542
1544
  const left = s.startsWith(":");
@@ -1545,41 +1547,58 @@ function cellAlign2(separator) {
1545
1547
  if (right) return ' style="text-align:right"';
1546
1548
  return "";
1547
1549
  }
1548
- 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)}>
1550
+ var ESCAPE_RE = /\\([!-\/:-@[-`{-~])/g;
1551
+ function resolveEscapes(text, stash) {
1552
+ return text.replace(ESCAPE_RE, (_m, ch) => stash.inline(escapeHtml2(ch)));
1553
+ }
1554
+ function cellInlineHtml(cell, g, stash) {
1555
+ let t = mapCodeRanges(
1556
+ unescapeCellPipes(cell),
1557
+ (range, raw) => range.kind === "span" ? stash.inline(`<code${cls(g.inlineCode)}>${escapeHtml2(codeSpanText(raw))}</code>`) : raw
1558
+ );
1559
+ t = resolveEscapes(t, stash);
1560
+ t = t.replace(
1561
+ IMAGE_RE,
1562
+ (_m, alt, url) => stash.inline(`<img class="${g.skin === "document" ? "matrx-image" : "print-image"}" src="${safeUrl(url)}" alt="${escapeHtml2(alt)}" />`)
1563
+ );
1564
+ t = t.replace(
1565
+ LINK_RE,
1566
+ (_m, text, url) => stash.inline(`<a${g.skin === "document" ? ' class="matrx-link"' : ""} href="${safeUrl(url)}">${convertEmphasis(text, g)}</a>`)
1567
+ );
1568
+ return convertEmphasis(t, g);
1569
+ }
1570
+ function tableHtml(lines, g, stash) {
1571
+ const cellsOf = (line) => rowCells(line).map((cell) => cellInlineHtml(cell, g, stash));
1572
+ const headers = cellsOf(lines[0] ?? "");
1573
+ const aligns = rowCells(lines[1] ?? "").map(cellAlign2);
1574
+ const rows = lines.slice(2).map((line) => {
1575
+ const cells = cellsOf(line);
1576
+ return headers.map((_h, k) => cells[k] ?? "");
1577
+ });
1578
+ let t = `<table${cls(g.table)}>
1557
1579
  <thead${cls(g.thead)}>
1558
1580
  <tr${cls(g.tr)}>
1559
1581
  `;
1560
- headers.forEach((h, i) => {
1561
- t += `<th${cls(g.th)}${aligns[i] ?? ""}>${h}</th>
1582
+ headers.forEach((h, k) => {
1583
+ t += `<th${cls(g.th)}${aligns[k] ?? ""}>${h}</th>
1562
1584
  `;
1563
- });
1564
- t += "</tr>\n</thead>\n";
1565
- if (rows.length > 0) {
1566
- t += `<tbody${cls(g.tbody)}>
1585
+ });
1586
+ t += "</tr>\n</thead>\n";
1587
+ if (rows.length > 0) {
1588
+ t += `<tbody${cls(g.tbody)}>
1567
1589
  `;
1568
- for (const row of rows) {
1569
- t += `<tr${cls(g.tr)}>
1590
+ for (const row of rows) {
1591
+ t += `<tr${cls(g.tr)}>
1570
1592
  `;
1571
- row.forEach((c, i) => {
1572
- t += `<td${cls(g.td)}${aligns[i] ?? ""}>${c}</td>
1573
- `;
1574
- });
1575
- t += "</tr>\n";
1576
- }
1577
- t += "</tbody>\n";
1578
- }
1579
- return `${t}</table>
1593
+ row.forEach((c, k) => {
1594
+ t += `<td${cls(g.td)}${aligns[k] ?? ""}>${c}</td>
1580
1595
  `;
1596
+ });
1597
+ t += "</tr>\n";
1581
1598
  }
1582
- );
1599
+ t += "</tbody>\n";
1600
+ }
1601
+ return `${t}</table>`;
1583
1602
  }
1584
1603
  function convertLists(html, g) {
1585
1604
  const lines = html.split("\n");
@@ -1686,7 +1705,6 @@ function documentBody(markdown, ctx) {
1686
1705
  html = convertLists(html, g);
1687
1706
  html = convertEmphasis(html, g);
1688
1707
  html = html.replace(/^> ?(.+)$/gm, '<blockquote class="matrx-blockquote">$1</blockquote>');
1689
- html = convertTables(html, g);
1690
1708
  html = html.replace(/<table>/g, '<table class="matrx-table">').replace(/<thead>/g, '<thead class="matrx-table-head">').replace(/<tbody>/g, '<tbody class="matrx-table-body">').replace(/<tr>/g, '<tr class="matrx-table-row">').replace(/<th>/g, '<th class="matrx-table-header">').replace(/<td>/g, '<td class="matrx-table-cell">');
1691
1709
  html = html.replace(/<img(?![^>]*class=)([^>]*)>/g, '<img class="matrx-image"$1>');
1692
1710
  html = html.replace(/<div\b([^>]*)>/g, (m, attrs) => {
@@ -1743,7 +1761,6 @@ function articleBody(markdown, ctx) {
1743
1761
  const g = ARTICLE_GRAMMAR;
1744
1762
  const stash = new Stash();
1745
1763
  let html = protectRegions(markdown, g, stash, ctx);
1746
- html = convertTables(html, g);
1747
1764
  html = convertRules(html, g);
1748
1765
  html = html.replace(
1749
1766
  IMAGE_RE,