levix-bot 4.7.0 → 4.9.1

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 (62) hide show
  1. package/README.md +21 -7
  2. package/SETUP.md +21 -2
  3. package/app.cjs +69 -23
  4. package/package.json +3 -2
  5. package/public/dashboard/assets/index-B1sOW2VX.js +561 -0
  6. package/public/dashboard/assets/index-CK8osB4o.css +2 -0
  7. package/public/dashboard/index.html +2 -2
  8. package/public/dashboard.css +50 -0
  9. package/public/dashboard.js +25 -2
  10. package/public/gateway-i18n.js +26 -0
  11. package/src/bootstrap/session-startup-policy.js +6 -6
  12. package/src/commands/group/antilink.cjs +82 -59
  13. package/src/commands/group/antispam.cjs +112 -0
  14. package/src/commands/group/clearwarns.cjs +6 -6
  15. package/src/commands/group/demote.cjs +11 -13
  16. package/src/commands/group/flagged.cjs +52 -0
  17. package/src/commands/group/kick.cjs +25 -15
  18. package/src/commands/group/media.cjs +65 -29
  19. package/src/commands/group/policy.cjs +215 -0
  20. package/src/commands/group/promote.cjs +11 -12
  21. package/src/commands/group/rules.cjs +9 -6
  22. package/src/commands/group/setwarn.cjs +17 -27
  23. package/src/commands/group/warn.cjs +13 -63
  24. package/src/commands/group.cjs +5 -2
  25. package/src/commands/md.cjs +44 -0
  26. package/src/commands/mod.cjs +101 -26
  27. package/src/commands/translate.cjs +585 -0
  28. package/src/config/ai-identity.cjs +53 -1
  29. package/src/config/ai-persona.md +32 -73
  30. package/src/config/defaults.cjs +3 -0
  31. package/src/config/secrets.cjs +71 -3
  32. package/src/config/settings.cjs +22 -1
  33. package/src/core/socket.js +27 -5
  34. package/src/db/db.cjs +66 -0
  35. package/src/db/store.cjs +264 -16
  36. package/src/diagnostics/crashReports.cjs +35 -9
  37. package/src/diagnostics/debugLog.cjs +405 -0
  38. package/src/handlers/command.handler.js +3 -1
  39. package/src/handlers/group.handler.js +32 -1
  40. package/src/handlers/message.handler.js +53 -17
  41. package/src/middleware/antispam.middleware.js +95 -48
  42. package/src/panel/feedback.cjs +32 -5
  43. package/src/panel/one-time-code.cjs +156 -0
  44. package/src/panel/password-reset.cjs +187 -91
  45. package/src/panel/recovery-email.cjs +333 -0
  46. package/src/routes/dashboard.api.esm.js +341 -29
  47. package/src/services/aiAgent.cjs +25 -3
  48. package/src/services/moderationPolicy.cjs +895 -0
  49. package/src/services/settingsTransfer.cjs +4 -2
  50. package/src/utils/linkedCreds.cjs +22 -0
  51. package/src/utils/logger.cjs +93 -34
  52. package/src/utils/markdownParser.cjs +208 -136
  53. package/src/utils/moderationAction.cjs +637 -0
  54. package/src/utils/moderationSummary.cjs +225 -0
  55. package/src/utils/permissions.cjs +70 -0
  56. package/src/utils/permissions.esm.js +48 -8
  57. package/src/utils/storage.cjs +8 -0
  58. package/src/utils/storage.esm.js +4 -0
  59. package/views/login.ejs +100 -28
  60. package/public/dashboard/assets/index-RnT7DAde.js +0 -540
  61. package/public/dashboard/assets/index-UcqCdCvG.css +0 -2
  62. package/src/commands/group/antiSpam.cjs +0 -97
@@ -15,8 +15,10 @@
15
15
  // language, delays, thumbnails, port and proxy shape, …) plus the command
16
16
  // table — prefix, permission overrides, alias overrides, disabled commands.
17
17
  // Not covered on purpose: per-group config and the memory files (they are
18
- // personal data, not settings) and the AI persona (a behaviour file the
19
- // operator authors, shared by copy-paste if wanted).
18
+ // personal data, not settings), the AI persona (a behaviour file the
19
+ // operator authors, shared by copy-paste if wanted), and account security
20
+ // (`auth:password`, `auth:recovery-email`). The recovery address is sealed
21
+ // outside the settings list and must never be added to this file.
20
22
 
21
23
  const settings = require("../config/settings.cjs");
22
24
  const runtimeConfig = require("../config/runtime-config.cjs");
@@ -0,0 +1,22 @@
1
+ // Whether Baileys credentials describe a finished link to a WhatsApp account.
2
+ //
3
+ // One definition, because three places need the answer (the boot policy that
4
+ // resumes a paired install, the session manager, and the socket factory) and
5
+ // they used to disagree with Baileys instead of with each other.
6
+ //
7
+ // me.id requestPairingCode writes it, with name "~", before the phone
8
+ // accepts the code. Alone it proves nothing.
9
+ // registered Baileys sets it true only in the pairing-code flow. A link
10
+ // made by scanning a QR code stays `registered: false` forever,
11
+ // so `false` cannot mean "unfinished".
12
+ // account the signed device identity, written by pair-success in both
13
+ // flows and in neither unfinished state.
14
+ //
15
+ // So a link is finished when there is a me.id and either the code flow said so
16
+ // or the account identity arrived.
17
+ function isLinkedCreds(creds) {
18
+ if (!creds?.me?.id) return false;
19
+ return creds.registered !== false || Boolean(creds.account);
20
+ }
21
+
22
+ module.exports = { isLinkedCreds };
@@ -1,16 +1,15 @@
1
1
  const path = require("path");
2
+ const fs = require("node:fs");
2
3
  const pino = require("pino");
3
4
 
4
5
  const { ensureDataDir } = require("../config/paths.cjs");
6
+ const debugLog = require("../diagnostics/debugLog.cjs");
5
7
 
6
8
  // Logs go in the data directory with everything else the bot writes, so a
7
9
  // global install never tries to write next to its own code.
8
10
  const logDir = ensureDataDir("logs");
9
11
 
10
- // Pino's `transport` option runs each target in a worker thread, and a worker
11
- // loads its target by module path — which a single-file executable can't
12
- // provide. Same three destinations either way; packaged builds just wire them
13
- // as plain streams in this thread.
12
+ // A packaged executable cannot load a worker target by module path.
14
13
  function isPackaged() {
15
14
  try {
16
15
  return require("node:sea").isSea() === true;
@@ -44,35 +43,95 @@ const PRETTY = {
44
43
  const combined = path.join(logDir, "combined.log");
45
44
  const errors = path.join(logDir, "error.log");
46
45
 
47
- const logger = useInProcessLogs()
48
- ? pino(
49
- base,
50
- pino.multistream([
51
- { level: "trace", stream: pino.destination({ dest: combined, mkdir: true }) },
52
- { level: "error", stream: pino.destination({ dest: errors, mkdir: true }) },
53
- { level: "trace", stream: require("pino-pretty")(PRETTY) },
54
- ]),
55
- )
56
- : pino({
57
- ...base,
58
- transport: {
59
- targets: [
60
- // Combined log file (all levels)
61
- {
62
- target: "pino/file",
63
- level: "trace",
64
- options: { destination: combined, mkdir: true },
65
- },
66
- // Error log file (errors only)
67
- {
68
- target: "pino/file",
69
- level: "error",
70
- options: { destination: errors, mkdir: true },
71
- },
72
- // Pretty console output
73
- { target: "pino-pretty", level: "trace", options: PRETTY },
74
- ],
75
- },
76
- });
46
+ // Own file writes in this thread so clearing has a precise boundary. Async
47
+ // SonicBoom flushSync() does not wait for an already in-flight write, which
48
+ // could otherwise restore an old entry after truncation. Sync destinations
49
+ // are ready immediately and leave no pending file writes behind.
50
+ const combinedStream = pino.destination({ dest: combined, mkdir: true, sync: true });
51
+ const errorStream = pino.destination({ dest: errors, mkdir: true, sync: true });
52
+ const logger = pino(
53
+ base,
54
+ pino.multistream([
55
+ { level: "trace", stream: combinedStream },
56
+ // Detailed, sanitized, local only. See src/diagnostics/debugLog.cjs.
57
+ { level: "debug", stream: debugLog.stream },
58
+ { level: "error", stream: errorStream },
59
+ {
60
+ level: "trace",
61
+ stream: useInProcessLogs()
62
+ ? require("pino-pretty")(PRETTY)
63
+ : pino.transport({ target: "pino-pretty", options: PRETTY }),
64
+ },
65
+ ]),
66
+ );
67
+
68
+ const MAX_READ_BYTES = 256 * 1024;
69
+
70
+ /** Read only the bounded tail of the operational log, never arbitrary files. */
71
+ logger.readLogs = function readLogs(limit = 500) {
72
+ const requested = Number(limit);
73
+ const count = Number.isFinite(requested)
74
+ ? Math.max(1, Math.min(500, Math.trunc(requested)))
75
+ : 500;
76
+ const fd = fs.openSync(combined, "r");
77
+ let text;
78
+ try {
79
+ const size = fs.fstatSync(fd).size;
80
+ const start = Math.max(0, size - MAX_READ_BYTES);
81
+ const buffer = Buffer.alloc(Math.min(size, MAX_READ_BYTES));
82
+ const bytesRead = fs.readSync(fd, buffer, 0, buffer.length, start);
83
+ text = buffer.toString("utf8", 0, bytesRead);
84
+ // The byte boundary can land inside a UTF-8 character or a JSON record.
85
+ if (start > 0) {
86
+ const newline = text.indexOf("\n");
87
+ text = newline < 0 ? "" : text.slice(newline + 1);
88
+ }
89
+ } finally {
90
+ fs.closeSync(fd);
91
+ }
92
+
93
+ const entries = [];
94
+ for (const line of text.split("\n")) {
95
+ if (!line) continue;
96
+ let entry;
97
+ try {
98
+ entry = JSON.parse(line);
99
+ } catch {
100
+ continue; // A truncated or malformed record must not hide newer logs.
101
+ }
102
+ if (!entry || typeof entry !== "object" || Array.isArray(entry)) continue;
103
+ const level = typeof entry.level === "number" ? entry.level : 30;
104
+ const normalizedLevel =
105
+ level >= 50 ? "error" : level >= 40 ? "warn" : level >= 30 ? "info" : "debug";
106
+ const date =
107
+ typeof entry.time === "number" || typeof entry.time === "string"
108
+ ? new Date(entry.time)
109
+ : new Date(NaN);
110
+ const time = Number.isFinite(date.getTime()) ? date.toISOString() : "";
111
+ let msg =
112
+ typeof entry.msg === "string"
113
+ ? entry.msg
114
+ : entry.msg == null
115
+ ? ""
116
+ : JSON.stringify(entry.msg);
117
+ const error = entry.err ?? entry.error;
118
+ if (error && typeof error === "object") {
119
+ const detail = error.stack || error.message;
120
+ if (typeof detail === "string" && detail !== msg) msg = msg ? `${msg}\n${detail}` : detail;
121
+ }
122
+ entries.push({ time, level: normalizedLevel, msg });
123
+ }
124
+ return entries.slice(-count);
125
+ };
126
+
127
+ /** Truncate the open files in place, keeping later writes on the same files. */
128
+ logger.clearLogs = function clearLogs() {
129
+ combinedStream.flushSync();
130
+ errorStream.flushSync();
131
+ fs.ftruncateSync(combinedStream.fd, 0);
132
+ fs.ftruncateSync(errorStream.fd, 0);
133
+ // The debug log is part of "the logs": one Clear removes it too.
134
+ debugLog.clearDebugLogs();
135
+ };
77
136
 
78
137
  module.exports = logger;
@@ -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 };