@alexnodeland/claude-telegram 0.3.1 → 0.3.2

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/package.json +1 -1
  2. package/src/html.ts +49 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexnodeland/claude-telegram",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Claude Code Channel plugin + standalone orchestrator bridging Telegram to Claude Code sessions",
5
5
  "module": "src/index.ts",
6
6
  "main": "src/index.ts",
package/src/html.ts CHANGED
@@ -35,9 +35,9 @@ export function markdownToTelegramHtml(md: string): string {
35
35
  const PLACEHOLDER_PREFIX = "\u2060CBLK";
36
36
  const PLACEHOLDER_SUFFIX = "CBLK\u2060";
37
37
 
38
- // 1. Extract fenced code blocks into placeholders
38
+ // 1. Extract fenced code blocks and tables into placeholders
39
39
  const codeBlocks: string[] = [];
40
- const withPlaceholders = md.replace(/```(\w*)\n([\s\S]*?)```/g, (_match, lang: string, code: string) => {
40
+ let withPlaceholders = md.replace(/```(\w*)\n([\s\S]*?)```/g, (_match, lang: string, code: string) => {
41
41
  const escaped = escapeHtml(code.replace(/\n$/, ""));
42
42
  const html = lang
43
43
  ? `<pre><code class="language-${escapeHtml(lang)}">${escaped}</code></pre>`
@@ -46,6 +46,13 @@ export function markdownToTelegramHtml(md: string): string {
46
46
  return `${PLACEHOLDER_PREFIX}${codeBlocks.length - 1}${PLACEHOLDER_SUFFIX}`;
47
47
  });
48
48
 
49
+ // 1b. Extract Markdown tables into <pre> placeholders
50
+ withPlaceholders = withPlaceholders.replace(/(?:^|\n)(\|.+\|(?:\r?\n\|.+\|)*)/g, (_match, tableBlock: string) => {
51
+ const html = convertMarkdownTable(tableBlock);
52
+ codeBlocks.push(html);
53
+ return `\n${PLACEHOLDER_PREFIX}${codeBlocks.length - 1}${PLACEHOLDER_SUFFIX}`;
54
+ });
55
+
49
56
  // 2. Process non-code-block text
50
57
  const placeholderRe = new RegExp(`(${PLACEHOLDER_PREFIX}\\d+${PLACEHOLDER_SUFFIX})`, "g");
51
58
  const matchRe = new RegExp(`^${PLACEHOLDER_PREFIX}(\\d+)${PLACEHOLDER_SUFFIX}$`);
@@ -98,3 +105,43 @@ function convertInlineFormatting(text: string): string {
98
105
  })
99
106
  .join("");
100
107
  }
108
+
109
+ /** Convert a Markdown table to an aligned monospace <pre> block. */
110
+ function convertMarkdownTable(tableText: string): string {
111
+ const rows = tableText.split(/\r?\n/).filter((r) => r.includes("|"));
112
+
113
+ // Parse cells from each row
114
+ const parsed = rows.map((row) =>
115
+ row
116
+ .replace(/^\|/, "")
117
+ .replace(/\|$/, "")
118
+ .split("|")
119
+ .map((c) => c.trim()),
120
+ );
121
+
122
+ // Filter out separator rows (--- or :---: etc.)
123
+ const dataRows = parsed.filter((cells) => !cells.every((c) => /^[:\-\s]+$/.test(c)));
124
+ if (dataRows.length === 0) return `<pre>${escapeHtml(tableText)}</pre>`;
125
+
126
+ // Calculate max width per column
127
+ const colCount = Math.max(...dataRows.map((r) => r.length));
128
+ const widths: number[] = Array.from({ length: colCount }, () => 0);
129
+ for (const row of dataRows) {
130
+ for (let i = 0; i < colCount; i++) {
131
+ widths[i] = Math.max(widths[i] ?? 0, (row[i] ?? "").length);
132
+ }
133
+ }
134
+
135
+ // Build aligned rows
136
+ const lines = dataRows.map((row, rowIdx) => {
137
+ const padded = widths.map((w, i) => (row[i] ?? "").padEnd(w)).join(" ");
138
+ // Add a separator line after the header
139
+ if (rowIdx === 0 && dataRows.length > 1) {
140
+ const sep = widths.map((w) => "─".repeat(w)).join("──");
141
+ return `${padded}\n${sep}`;
142
+ }
143
+ return padded;
144
+ });
145
+
146
+ return `<pre>${escapeHtml(lines.join("\n"))}</pre>`;
147
+ }