@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.cjs CHANGED
@@ -67,6 +67,7 @@ function openPrintWindow(htmlDocument, filename = "print", windowFeatures = "wid
67
67
 
68
68
  // src/markdown.ts
69
69
  var import_html_escape2 = require("@ai-matrx/kit/html-escape");
70
+ var import_source = require("@ai-matrx/content-ir/source");
70
71
 
71
72
  // src/safe-html.ts
72
73
  var import_hast_util_from_html = require("hast-util-from-html");
@@ -1489,6 +1490,12 @@ function protectRegions(markdown, g, stash, ctx) {
1489
1490
  i = j;
1490
1491
  continue;
1491
1492
  }
1493
+ if ((0, import_source.tableStartsAt)(lines, i)) {
1494
+ const end = (0, import_source.findTableEnd)(lines, i);
1495
+ out.push(stash.block(tableHtml(lines.slice(i, end), g, stash)));
1496
+ i = end - 1;
1497
+ continue;
1498
+ }
1492
1499
  out.push(line);
1493
1500
  }
1494
1501
  let text = out.join("\n");
@@ -1496,6 +1503,7 @@ function protectRegions(markdown, g, stash, ctx) {
1496
1503
  /`([^`\n]+)`/g,
1497
1504
  (_m, code) => stash.inline(`<code${cls(g.inlineCode)}>${(0, import_html_escape2.escapeHtml)(code)}</code>`)
1498
1505
  );
1506
+ text = resolveEscapes(text, stash);
1499
1507
  text = text.replace(/<!--[\s\S]*?-->/g, "");
1500
1508
  const before = text;
1501
1509
  for (let pass = 0; pass < 8; pass++) {
@@ -1576,12 +1584,6 @@ var LINK_RE = new RegExp(String.raw`\[([^\]]+)\]\(` + LINK_URL + String.raw`\)`,
1576
1584
  function convertRules(html, g) {
1577
1585
  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)}>`);
1578
1586
  }
1579
- function splitTableRow(row) {
1580
- let r = row.trim();
1581
- if (r.startsWith("|")) r = r.slice(1);
1582
- if (r.endsWith("|") && !r.endsWith("\\|")) r = r.slice(0, -1);
1583
- return r.split(/(?<!\\)\|/).map((c) => c.trim().replace(/\\\|/g, "|"));
1584
- }
1585
1587
  function cellAlign2(separator) {
1586
1588
  const s = separator.trim();
1587
1589
  const left = s.startsWith(":");
@@ -1590,41 +1592,58 @@ function cellAlign2(separator) {
1590
1592
  if (right) return ' style="text-align:right"';
1591
1593
  return "";
1592
1594
  }
1593
- function convertTables(html, g) {
1594
- const tableRegex = /^(\|.*\|)\s*\n(\|[-\s:|]*\|)\s*\n((?:\|.*\|\s*\n?)*)/gm;
1595
- return html.replace(
1596
- tableRegex,
1597
- (_m, headerRow, separatorRow, bodyRows) => {
1598
- const headers = splitTableRow(headerRow);
1599
- const aligns = splitTableRow(separatorRow).map(cellAlign2);
1600
- const rows = bodyRows.trim().split("\n").filter((r) => r.trim()).map(splitTableRow);
1601
- let t = `<table${cls(g.table)}>
1595
+ var ESCAPE_RE = /\\([!-\/:-@[-`{-~])/g;
1596
+ function resolveEscapes(text, stash) {
1597
+ return text.replace(ESCAPE_RE, (_m, ch) => stash.inline((0, import_html_escape2.escapeHtml)(ch)));
1598
+ }
1599
+ function cellInlineHtml(cell, g, stash) {
1600
+ let t = (0, import_source.mapCodeRanges)(
1601
+ (0, import_source.unescapeCellPipes)(cell),
1602
+ (range, raw) => range.kind === "span" ? stash.inline(`<code${cls(g.inlineCode)}>${(0, import_html_escape2.escapeHtml)((0, import_source.codeSpanText)(raw))}</code>`) : raw
1603
+ );
1604
+ t = resolveEscapes(t, stash);
1605
+ t = t.replace(
1606
+ IMAGE_RE,
1607
+ (_m, alt, url) => stash.inline(`<img class="${g.skin === "document" ? "matrx-image" : "print-image"}" src="${safeUrl(url)}" alt="${(0, import_html_escape2.escapeHtml)(alt)}" />`)
1608
+ );
1609
+ t = t.replace(
1610
+ LINK_RE,
1611
+ (_m, text, url) => stash.inline(`<a${g.skin === "document" ? ' class="matrx-link"' : ""} href="${safeUrl(url)}">${convertEmphasis(text, g)}</a>`)
1612
+ );
1613
+ return convertEmphasis(t, g);
1614
+ }
1615
+ function tableHtml(lines, g, stash) {
1616
+ const cellsOf = (line) => (0, import_source.rowCells)(line).map((cell) => cellInlineHtml(cell, g, stash));
1617
+ const headers = cellsOf(lines[0] ?? "");
1618
+ const aligns = (0, import_source.rowCells)(lines[1] ?? "").map(cellAlign2);
1619
+ const rows = lines.slice(2).map((line) => {
1620
+ const cells = cellsOf(line);
1621
+ return headers.map((_h, k) => cells[k] ?? "");
1622
+ });
1623
+ let t = `<table${cls(g.table)}>
1602
1624
  <thead${cls(g.thead)}>
1603
1625
  <tr${cls(g.tr)}>
1604
1626
  `;
1605
- headers.forEach((h, i) => {
1606
- t += `<th${cls(g.th)}${aligns[i] ?? ""}>${h}</th>
1627
+ headers.forEach((h, k) => {
1628
+ t += `<th${cls(g.th)}${aligns[k] ?? ""}>${h}</th>
1607
1629
  `;
1608
- });
1609
- t += "</tr>\n</thead>\n";
1610
- if (rows.length > 0) {
1611
- t += `<tbody${cls(g.tbody)}>
1630
+ });
1631
+ t += "</tr>\n</thead>\n";
1632
+ if (rows.length > 0) {
1633
+ t += `<tbody${cls(g.tbody)}>
1612
1634
  `;
1613
- for (const row of rows) {
1614
- t += `<tr${cls(g.tr)}>
1635
+ for (const row of rows) {
1636
+ t += `<tr${cls(g.tr)}>
1615
1637
  `;
1616
- row.forEach((c, i) => {
1617
- t += `<td${cls(g.td)}${aligns[i] ?? ""}>${c}</td>
1618
- `;
1619
- });
1620
- t += "</tr>\n";
1621
- }
1622
- t += "</tbody>\n";
1623
- }
1624
- return `${t}</table>
1638
+ row.forEach((c, k) => {
1639
+ t += `<td${cls(g.td)}${aligns[k] ?? ""}>${c}</td>
1625
1640
  `;
1641
+ });
1642
+ t += "</tr>\n";
1626
1643
  }
1627
- );
1644
+ t += "</tbody>\n";
1645
+ }
1646
+ return `${t}</table>`;
1628
1647
  }
1629
1648
  function convertLists(html, g) {
1630
1649
  const lines = html.split("\n");
@@ -1731,7 +1750,6 @@ function documentBody(markdown, ctx) {
1731
1750
  html = convertLists(html, g);
1732
1751
  html = convertEmphasis(html, g);
1733
1752
  html = html.replace(/^> ?(.+)$/gm, '<blockquote class="matrx-blockquote">$1</blockquote>');
1734
- html = convertTables(html, g);
1735
1753
  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">');
1736
1754
  html = html.replace(/<img(?![^>]*class=)([^>]*)>/g, '<img class="matrx-image"$1>');
1737
1755
  html = html.replace(/<div\b([^>]*)>/g, (m, attrs) => {
@@ -1788,7 +1806,6 @@ function articleBody(markdown, ctx) {
1788
1806
  const g = ARTICLE_GRAMMAR;
1789
1807
  const stash = new Stash();
1790
1808
  let html = protectRegions(markdown, g, stash, ctx);
1791
- html = convertTables(html, g);
1792
1809
  html = convertRules(html, g);
1793
1810
  html = html.replace(
1794
1811
  IMAGE_RE,