levix-bot 4.6.0 → 4.8.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.
@@ -1,161 +1,233 @@
1
+ // Markdown from any provider -> WhatsApp text. This is a display boundary:
2
+ // stored AI history, code contents, identifiers and URLs remain untouched.
1
3
  const { tr } = require("./i18n.cjs");
2
4
 
3
- // file: src/utils/markdownParser.cjs
4
- //
5
- // Converts standard Markdown output (especially from LLMs) into human-readable
6
- // WhatsApp formatting:
7
- // - **bold** -> *bold*
8
- // - ~~strike~~ -> ~strike~
9
- // - __italic__ -> _italic_
10
- // - # Headings -> *Headings*
11
- // - Markdown tables (| col1 | col2 |) -> clean, itemized WhatsApp cards
12
-
13
- /**
14
- * Parses markdown table text into structured data:
15
- * { headers: string[], rows: string[][] }
16
- */
17
- function parseMarkdownTable(tableLines) {
18
- const cleanCells = (line) => {
19
- return line
20
- .trim()
21
- .replace(/^\|/, "")
22
- .replace(/\|$/, "")
23
- .split("|")
24
- .map((c) => c.trim());
25
- };
26
-
27
- if (tableLines.length < 2) return null;
28
-
29
- const headers = cleanCells(tableLines[0]);
30
- // Line 1 is usually the separator: |---|---|
31
- const separatorLine = tableLines[1];
32
- if (!/^[\s|:-]+$/.test(separatorLine)) return null;
33
-
34
- const rows = [];
35
- for (let i = 2; i < tableLines.length; i++) {
36
- const line = tableLines[i].trim();
37
- if (!line) continue;
38
- rows.push(cleanCells(line));
5
+ /** Split table cells without splitting escaped pipes or pipes inside code. */
6
+ function tableCells(line) {
7
+ const cells = [];
8
+ let cell = "";
9
+ let ticks = 0;
10
+ const text = line.trim();
11
+ for (let i = 0; i < text.length; i++) {
12
+ if (text[i] === "\\" && text[i + 1] === "|") {
13
+ cell += "|";
14
+ i++;
15
+ } else if (text[i] === "`") {
16
+ let end = i + 1;
17
+ while (text[end] === "`") end++;
18
+ const run = end - i;
19
+ if (!ticks) ticks = run;
20
+ else if (ticks === run) ticks = 0;
21
+ cell += text.slice(i, end);
22
+ i = end - 1;
23
+ } else if (text[i] === "|" && !ticks) {
24
+ cells.push(cell.trim());
25
+ cell = "";
26
+ } else cell += text[i];
39
27
  }
40
-
41
- return { headers, rows };
28
+ cells.push(cell.trim());
29
+ if (text.startsWith("|")) cells.shift();
30
+ if (text.endsWith("|") && !text.endsWith("\\|")) cells.pop();
31
+ return cells;
42
32
  }
43
33
 
44
- /**
45
- * Formats parsed table data into a clean WhatsApp-friendly list of cards.
46
- */
47
- function formatTableForWhatsApp({ headers, rows }) {
48
- if (!headers.length || !rows.length) return "";
49
-
50
- const cards = [];
34
+ function parseMarkdownTable(lines) {
35
+ if (lines.length < 2 || !lines[0].includes("|")) return null;
36
+ const headers = tableCells(lines[0]);
37
+ const separators = tableCells(lines[1]);
38
+ if (
39
+ !headers.length ||
40
+ headers.length !== separators.length ||
41
+ !separators.every((cell) => /^:?-{3,}:?$/.test(cell))
42
+ )
43
+ return null;
44
+ return { headers, rows: lines.slice(2).map(tableCells) };
45
+ }
51
46
 
52
- rows.forEach((row, rowIndex) => {
53
- const primaryLabel = row[0] || tr(`Item ${rowIndex + 1}`, `عنصر ${rowIndex + 1}`);
54
- const fields = [];
47
+ function bold(value) {
48
+ // The whole heading/label is bold; flatten inner bold spans before wrapping.
49
+ const text = value.trim().replace(/\*([^*\n]+)\*/g, "$1");
50
+ return `*${text}*`;
51
+ }
55
52
 
56
- // If there's more than 1 column, list the remaining fields with bullet points
57
- if (headers.length > 1) {
58
- for (let c = 0; c < headers.length; c++) {
59
- const headerName = headers[c] || tr(`Column ${c + 1}`, `عمود ${c + 1}`);
60
- const cellValue = row[c] || "—";
61
- fields.push(` • *${headerName}:* ${cellValue}`);
53
+ function tableText({ headers, rows }) {
54
+ if (!rows.length) return headers.map(bold).join(" · ");
55
+ if (headers.length === 1 && rows.every((row) => row.length <= 1)) {
56
+ return `${bold(headers[0])}\n${rows.map((row) => `• ${row[0] || "—"}`).join("\n")}`;
57
+ }
58
+ return rows
59
+ .map((row, index) => {
60
+ const title = row[0] || tr(`Item ${index + 1}`, `عنصر ${index + 1}`);
61
+ const fields = [];
62
+ for (let c = 1; c < Math.max(headers.length, row.length); c++) {
63
+ const header = headers[c] || tr(`Column ${c + 1}`, `عمود ${c + 1}`);
64
+ const label = header.replace(/^\*([^*]+)\*$/, "$1");
65
+ const value = (row[c] || "—").replace(/<br\s*\/?>/gi, "\n");
66
+ fields.push(` • ${bold(`${label}:`)} ${value.replace(/\n/g, "\n ")}`);
62
67
  }
63
- } else {
64
- fields.push(` • ${primaryLabel}`);
65
- }
66
-
67
- cards.push(`📋 *${primaryLabel}*\n${fields.join("\n")}`);
68
- });
69
-
70
- return cards.join("\n\n");
68
+ return `📋 ${bold(title)}\n${fields.join("\n")}`;
69
+ })
70
+ .join("\n\n");
71
71
  }
72
72
 
73
- /**
74
- * Replaces markdown table blocks with WhatsApp-friendly formatted cards.
75
- */
73
+ /** Only a header followed by a real delimiter row starts a table. */
76
74
  function convertMarkdownTables(text) {
77
75
  const lines = text.split("\n");
78
76
  const output = [];
79
- let currentTable = [];
80
-
81
- const flushTable = () => {
82
- if (currentTable.length >= 2) {
83
- const parsed = parseMarkdownTable(currentTable);
84
- if (parsed) {
85
- output.push(formatTableForWhatsApp(parsed));
86
- } else {
87
- output.push(...currentTable);
88
- }
89
- } else {
90
- output.push(...currentTable);
77
+ for (let i = 0; i < lines.length; i++) {
78
+ const parsed = parseMarkdownTable(lines.slice(i, i + 2));
79
+ if (!parsed) {
80
+ output.push(lines[i]);
81
+ continue;
82
+ }
83
+ i += 2;
84
+ while (i < lines.length && lines[i].trim() && lines[i].includes("|")) {
85
+ // A second header/delimiter pair belongs to a separate table.
86
+ if (parseMarkdownTable(lines.slice(i, i + 2))) break;
87
+ parsed.rows.push(tableCells(lines[i++]));
91
88
  }
92
- currentTable = [];
93
- };
89
+ i--;
90
+ output.push(tableText(parsed));
91
+ }
92
+ return output.join("\n");
93
+ }
94
94
 
95
- for (const line of lines) {
96
- if (line.trim().startsWith("|") && line.trim().endsWith("|")) {
97
- currentTable.push(line);
95
+ function shieldCode(text, store) {
96
+ const runs = /`+|~{3,}/g;
97
+ let output = "";
98
+ let cursor = 0;
99
+ for (let match = runs.exec(text); match; match = runs.exec(text)) {
100
+ const marker = match[0];
101
+ const start = match.index;
102
+ const lineStart = text.lastIndexOf("\n", start - 1) + 1;
103
+ const lineEnd = text.indexOf("\n", runs.lastIndex);
104
+ const info = lineEnd < 0 ? "" : text.slice(runs.lastIndex, lineEnd);
105
+ const fenced =
106
+ marker.length >= 3 &&
107
+ lineEnd >= 0 &&
108
+ /^ {0,3}$/.test(text.slice(lineStart, start)) &&
109
+ !info.includes(marker[0]);
110
+ let rendered;
111
+ let end;
112
+ if (fenced) {
113
+ const close = new RegExp(`^ {0,3}${marker[0]}{${marker.length},}[ \\t]*$`, "gm");
114
+ close.lastIndex = lineEnd + 1;
115
+ const ending = close.exec(text);
116
+ let body = text.slice(lineEnd + 1, ending ? ending.index : text.length);
117
+ if (ending && body.endsWith("\n")) body = body.slice(0, -1);
118
+ rendered = `\`\`\`\n${body}\n\`\`\``;
119
+ end = ending ? ending.index + ending[0].length : text.length;
98
120
  } else {
99
- flushTable();
100
- output.push(line);
121
+ if (marker[0] !== "`") continue;
122
+ const close = new RegExp(`(?<!\`)\`{${marker.length}}(?!\`)`, "g");
123
+ close.lastIndex = runs.lastIndex;
124
+ const ending = close.exec(text);
125
+ if (!ending) continue;
126
+ end = ending.index + marker.length;
127
+ rendered =
128
+ marker.length === 2
129
+ ? `\`\`\`${text.slice(runs.lastIndex, ending.index)}\`\`\``
130
+ : text.slice(start, end);
101
131
  }
132
+ output += text.slice(cursor, start) + store(rendered);
133
+ cursor = end;
134
+ runs.lastIndex = end;
102
135
  }
103
- flushTable();
136
+ return output + text.slice(cursor);
137
+ }
104
138
 
105
- return output.join("\n");
139
+ function links(text, store) {
140
+ let output = "";
141
+ let cursor = 0;
142
+ let skipUntil = 0;
143
+ for (const match of text.matchAll(/!?\[([^[\]\n]*)\]\(/g)) {
144
+ if (match.index < cursor || match.index < skipUntil) continue;
145
+ const start = match.index + match[0].length;
146
+ let depth = 1;
147
+ let end = start;
148
+ for (; end < text.length && text[end] !== "\n"; end++) {
149
+ if (text[end] === "\\") {
150
+ end++;
151
+ continue;
152
+ }
153
+ if (text[end] === "(") depth++;
154
+ if (text[end] === ")" && --depth === 0) break;
155
+ }
156
+ if (depth) {
157
+ skipUntil = end;
158
+ continue;
159
+ }
160
+ const destination = text.slice(start, end).match(/^<?(\S+?)>?(?:[ \t]+["'][\s\S]*["'])?$/)?.[1];
161
+ if (!destination) continue;
162
+ const name = match[1].trim();
163
+ output +=
164
+ text.slice(cursor, match.index) +
165
+ (name && name !== destination ? `${name}: ` : "") +
166
+ store(destination);
167
+ cursor = end + 1;
168
+ }
169
+ return output + text.slice(cursor);
106
170
  }
107
171
 
108
- /**
109
- * Main parser: converts standard Markdown to WhatsApp formatting.
110
- */
111
172
  function formatMarkdownForWhatsApp(content) {
112
- if (!content || typeof content !== "string") return content;
113
-
114
- // 1. Protect code blocks and inline code using null-byte tokens
115
- const codeBlocks = [];
116
- let masked = content.replace(/```([\s\S]*?)```/g, (_, code) => {
117
- const placeholder = `\x00CODEBLOCK${codeBlocks.length}\x00`;
118
- codeBlocks.push(`\`\`\`${code}\`\`\``);
119
- return placeholder;
120
- });
121
-
122
- const inlineCodes = [];
123
- masked = masked.replace(/`([^`]+)`/g, (_, code) => {
124
- const placeholder = `\x00INLINECODE${inlineCodes.length}\x00`;
125
- inlineCodes.push(`\`${code}\``);
126
- return placeholder;
127
- });
128
-
129
- // 2. Convert markdown tables into WhatsApp cards
130
- masked = convertMarkdownTables(masked);
131
-
132
- // 3. Convert Headings: # Heading -> *Heading*
133
- masked = masked.replace(/(^|\n|\s)(#{1,6})\s+([^\n]+)/g, (match, lead, hashes, title) => {
134
- return `${lead}*${title.trim()}*`;
135
- });
136
-
137
- // 4. Convert Strikethrough: ~~text~~ -> ~text~
138
- masked = masked.replace(/~~([^~\n]+)~~/g, "~$1~");
139
-
140
- // 5. Convert Bold: **text** -> *text*
141
- masked = masked.replace(/\*\*([^*\n]+)\*\*/g, "*$1*");
142
-
143
- // 6. Convert Italic: __text__ -> _text_
144
- masked = masked.replace(/__([^_\n]+)__/g, "_$1_");
145
-
146
- // 7. Restore inline code and code blocks safely
147
- inlineCodes.forEach((code, idx) => {
148
- masked = masked.split(`\x00INLINECODE${idx}\x00`).join(code);
149
- });
150
- codeBlocks.forEach((block, idx) => {
151
- masked = masked.split(`\x00CODEBLOCK${idx}\x00`).join(block);
173
+ if (typeof content !== "string" || !content) return content;
174
+ // Pick an unused namespace: user text resembling a placeholder is not lost.
175
+ let prefix = "\x00WA";
176
+ while (content.includes(prefix)) prefix += "X";
177
+ const saved = [];
178
+ const store = (text) => `${prefix}${saved.push(text) - 1}\x00`;
179
+ let text = shieldCode(content.replace(/\r\n?/g, "\n"), store);
180
+ text = links(text, store);
181
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: Stop URLs before internal shielding tokens.
182
+ text = text.replace(/(?:https?:\/\/|www\.)[^\s<>\x00]+/gi, (url, offset, source) => {
183
+ for (const marker of ["***", "**", "__", "~~", "*", "_"]) {
184
+ if (source.slice(0, offset).endsWith(marker) && url.endsWith(marker)) {
185
+ return store(url.slice(0, -marker.length)) + marker;
186
+ }
187
+ }
188
+ return store(url);
152
189
  });
153
-
154
- return masked;
190
+ text = text.replace(/\\([\\`*{}[\]()#+.!_>~|-])/g, (_, literal) => store(literal));
191
+ text = text
192
+ .replace(/\*\*\*([^*\n]+)\*\*\*/g, "*_$1_*")
193
+ .replace(/(?<![\p{L}\p{N}_])___([^_\n]+)___(?![\p{L}\p{N}_])/gu, "*_$1_*")
194
+ .replace(/\*\*([^*\n]+)\*\*/g, "*$1*")
195
+ .replace(/(?<![\p{L}\p{N}_])__([^_\n]+)__(?![\p{L}\p{N}_])/gu, "_$1_")
196
+ .replace(/~~([^~\n]+)~~/g, "~$1~");
197
+ text = convertMarkdownTables(text);
198
+ const lines = text.split("\n");
199
+ const output = [];
200
+ for (let i = 0; i < lines.length; i++) {
201
+ let line = lines[i];
202
+ const heading = line.match(/^ {0,3}#{1,6}[ \t]+(.+?)(?:[ \t]+#+)?[ \t]*$/);
203
+ if (heading) line = bold(heading[1]);
204
+ else if (
205
+ line.trim() &&
206
+ !line.includes(prefix) &&
207
+ /^ {0,3}(?:={3,}|-{3,})[ \t]*$/.test(lines[i + 1] || "")
208
+ ) {
209
+ line = bold(line);
210
+ i++;
211
+ } else if (/^ {0,3}(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,})$/.test(line)) {
212
+ line = "──────────";
213
+ } else {
214
+ line = line
215
+ .replace(
216
+ /^(\s*)[-+*][ \t]+\[([ xX])\][ \t]+/,
217
+ (_, indent, check) => `${indent}${check === " " ? "☐" : "☑"} `,
218
+ )
219
+ .replace(/^(\s*)[-+*][ \t]+/, "$1• ")
220
+ .replace(/^(\s*)(\d+)\)[ \t]+/, "$1$2. ")
221
+ .replace(/^ {0,3}(?:>[ \t]*)+/, "> ");
222
+ }
223
+ output.push(line.replace(/[ \t]+$/, ""));
224
+ }
225
+ text = output.join("\n");
226
+ // A link can contain an already-protected code label, so restore inside out.
227
+ const token = new RegExp(`${prefix}(\\d+)\x00`, "g");
228
+ const restore = (whole, id) => saved[id]?.replace(token, restore) ?? whole;
229
+ text = text.replace(token, restore);
230
+ return text;
155
231
  }
156
232
 
157
- module.exports = {
158
- formatMarkdownForWhatsApp,
159
- convertMarkdownTables,
160
- parseMarkdownTable,
161
- };
233
+ module.exports = { formatMarkdownForWhatsApp, convertMarkdownTables, parseMarkdownTable };
@@ -102,6 +102,7 @@ module.exports = {
102
102
  resetAutoDeleteCounter: store.resetAutoDeleteCounter,
103
103
  insertAutoDeleteLog: store.insertAutoDeleteLog,
104
104
  listAutoDeleteLog: store.listAutoDeleteLog,
105
+ countAutoDeleteLog: store.countAutoDeleteLog,
105
106
  clearAutoDeleteLog: store.clearAutoDeleteLog,
106
107
  clearAutoDeleteOnUnlink: store.clearAutoDeleteOnUnlink,
107
108
  };
@@ -118,5 +118,6 @@ export const incrementAutoDeleteCounter = store.incrementAutoDeleteCounter;
118
118
  export const resetAutoDeleteCounter = store.resetAutoDeleteCounter;
119
119
  export const insertAutoDeleteLog = store.insertAutoDeleteLog;
120
120
  export const listAutoDeleteLog = store.listAutoDeleteLog;
121
+ export const countAutoDeleteLog = store.countAutoDeleteLog;
121
122
  export const clearAutoDeleteLog = store.clearAutoDeleteLog;
122
123
  export const clearAutoDeleteOnUnlink = store.clearAutoDeleteOnUnlink;
@@ -1,6 +1,7 @@
1
1
  <!DOCTYPE html>
2
- <html lang="en" data-theme="dark">
2
+ <html>
3
3
  <head>
4
+ <script src="/ui-boot.js"></script>
4
5
  <meta charset="UTF-8" />
5
6
  <meta name="viewport" content="width=device-width, initial-scale=1" />
6
7
  <title><%= brand.name %> · Control panel</title>