@alexsun-top/opencode-chat-room 0.8.3 → 0.8.5

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 (156) hide show
  1. package/CHANGELOG.md +7 -1
  2. package/README.md +5 -5
  3. package/README.zh-CN.md +5 -5
  4. package/cli.ts +1 -1
  5. package/dist/cli.js +35 -16
  6. package/package.json +15 -9
  7. package/packages/chat-room-channeld/README.md +3 -2
  8. package/packages/chat-room-channeld/src/inbound.ts +3 -6
  9. package/packages/chat-room-channeld/src/pg/sql.mjs +157 -142
  10. package/packages/chat-room-channeld/src/pg/sql.mts +175 -0
  11. package/packages/chat-room-channeld/src/pg/worker.mjs +107 -99
  12. package/packages/chat-room-channeld/src/pg/worker.mts +166 -0
  13. package/plugin.ts +1 -1
  14. package/server/assets/chat/admin-badge.js +1 -1
  15. package/server/assets/chat/admin.js +16 -10
  16. package/server/assets/chat/announcements.js +12 -2
  17. package/server/assets/chat/attachment-store.js +15 -6
  18. package/server/assets/chat/attachments.js +48 -33
  19. package/server/assets/chat/chat-background.js +8 -14
  20. package/server/assets/chat/clipboard.js +2 -1
  21. package/server/assets/chat/confirm-dialog.js +3 -3
  22. package/server/assets/chat/copy-action.js +1 -2
  23. package/server/assets/chat/diagram-viewer.js +7 -9
  24. package/server/assets/chat/doc-preview.js +19 -12
  25. package/server/assets/chat/editor.js +87 -61
  26. package/server/assets/chat/events.js +49 -34
  27. package/server/assets/chat/files.js +45 -13
  28. package/server/assets/chat/history-loader.js +5 -3
  29. package/server/assets/chat/image-zoom.js +21 -17
  30. package/server/assets/chat/install-prompt.js +10 -9
  31. package/server/assets/chat/integration-account-schema.js +5 -3
  32. package/server/assets/chat/integration-editor.js +2 -2
  33. package/server/assets/chat/integration-values.js +17 -11
  34. package/server/assets/chat/integrations.js +138 -81
  35. package/server/assets/chat/join-requests.js +20 -13
  36. package/server/assets/chat/markdown-footnotes.js +8 -8
  37. package/server/assets/chat/markdown.js +48 -38
  38. package/server/assets/chat/members.js +48 -25
  39. package/server/assets/chat/mentions.js +39 -12
  40. package/server/assets/chat/message-cache.js +2 -2
  41. package/server/assets/chat/message-polling.js +40 -21
  42. package/server/assets/chat/message-window.js +10 -6
  43. package/server/assets/chat/messages.js +70 -46
  44. package/server/assets/chat/office-preview.js +3 -3
  45. package/server/assets/chat/palette.js +45 -29
  46. package/server/assets/chat/panels.js +19 -1
  47. package/server/assets/chat/pwa.js +5 -4
  48. package/server/assets/chat/questions.js +25 -22
  49. package/server/assets/chat/recall.js +39 -21
  50. package/server/assets/chat/receipts.js +9 -7
  51. package/server/assets/chat/reminders.js +25 -8
  52. package/server/assets/chat/replies.js +34 -15
  53. package/server/assets/chat/resource-format.js +2 -2
  54. package/server/assets/chat/room-list.js +2 -2
  55. package/server/assets/chat/room-watcher.js +8 -4
  56. package/server/assets/chat/rooms.js +51 -40
  57. package/server/assets/chat/search.js +8 -6
  58. package/server/assets/chat/settings.js +16 -14
  59. package/server/assets/chat/shiki.js +1 -1
  60. package/server/assets/chat/shortcuts.js +3 -3
  61. package/server/assets/chat/side-pane.js +4 -3
  62. package/server/assets/chat/state.js +65 -37
  63. package/server/assets/chat/system-events.js +6 -5
  64. package/server/assets/chat/system-messages.js +15 -15
  65. package/server/assets/chat/task-form.js +5 -5
  66. package/server/assets/chat/task-row.js +1 -1
  67. package/server/assets/chat/tasks.js +41 -28
  68. package/server/assets/chat/window-controls.js +3 -2
  69. package/server/assets/conversation.css +14 -2
  70. package/server/assets/doc-preview.css +6 -0
  71. package/server/assets/office/THIRD-PARTY-NOTICES.txt +23 -1
  72. package/server/assets/office/licenses/docx-preview-HEADER.txt +6 -0
  73. package/server/assets/office/licenses/echarts-LICENSE-d3.txt +27 -0
  74. package/server/assets/office/licenses/echarts-NOTICE.txt +5 -0
  75. package/server/assets/office/licenses/immediate-LICENSE.txt +20 -0
  76. package/server/assets/office/licenses/lie-LICENSE.txt +7 -0
  77. package/server/assets/office/licenses/pako-LICENSE.txt +21 -0
  78. package/server/assets/office/licenses/pako-ZLIB.txt +18 -0
  79. package/server/assets/office/licenses/setimmediate-LICENSE.txt +20 -0
  80. package/server/chat.html +1 -0
  81. package/server/sw.js +3 -1
  82. package/skills/chatroom/SKILL.md +9 -0
  83. package/src/README.md +1 -1
  84. package/src/agent/agent-receipts.ts +12 -4
  85. package/src/center/center-client.ts +1 -1
  86. package/src/state/pg-sql.mjs +157 -142
  87. package/src/state/pg-sql.mts +175 -0
  88. package/src/state/pg-worker.mjs +107 -99
  89. package/src/state/pg-worker.mts +166 -0
  90. package/src/state/store.ts +21 -7
  91. package/src/tui/app-types.mjs +1 -0
  92. package/src/tui/app-types.mts +175 -0
  93. package/src/tui/app.mjs +426 -423
  94. package/src/tui/app.mts +500 -0
  95. package/src/tui/config.mjs +166 -158
  96. package/src/tui/config.mts +247 -0
  97. package/src/tui/http.mjs +239 -206
  98. package/src/tui/http.mts +412 -0
  99. package/src/tui/i18n.mjs +17 -20
  100. package/src/tui/i18n.mts +42 -0
  101. package/src/tui/info-app.mjs +431 -427
  102. package/src/tui/info-app.mts +500 -0
  103. package/src/tui/info-content.mjs +205 -199
  104. package/src/tui/info-content.mts +341 -0
  105. package/src/tui/join-app.mjs +75 -64
  106. package/src/tui/join-app.mts +99 -0
  107. package/src/tui/locales/en.mjs +302 -320
  108. package/src/tui/locales/en.mts +349 -0
  109. package/src/tui/locales/zh-CN.mjs +302 -320
  110. package/src/tui/locales/zh-CN.mts +349 -0
  111. package/src/tui/main.mjs +219 -213
  112. package/src/tui/main.mts +255 -0
  113. package/src/tui/markdown.mjs +128 -272
  114. package/src/tui/markdown.mts +152 -0
  115. package/src/tui/mentions.mjs +181 -184
  116. package/src/tui/mentions.mts +251 -0
  117. package/src/tui/message-app.mjs +181 -173
  118. package/src/tui/message-app.mts +213 -0
  119. package/src/tui/message-view.mjs +90 -111
  120. package/src/tui/message-view.mts +151 -0
  121. package/src/tui/messages.mjs +55 -49
  122. package/src/tui/messages.mts +106 -0
  123. package/src/tui/palette-app.mjs +364 -344
  124. package/src/tui/palette-app.mts +388 -0
  125. package/src/tui/palette.mjs +100 -98
  126. package/src/tui/palette.mts +176 -0
  127. package/src/tui/prefs.mjs +56 -63
  128. package/src/tui/prefs.mts +126 -0
  129. package/src/tui/reminder-app.mjs +191 -181
  130. package/src/tui/reminder-app.mts +228 -0
  131. package/src/tui/room-settings-app.mjs +257 -242
  132. package/src/tui/room-settings-app.mts +301 -0
  133. package/src/tui/session.mjs +30 -29
  134. package/src/tui/session.mts +51 -0
  135. package/src/tui/settings-app.mjs +151 -138
  136. package/src/tui/settings-app.mts +179 -0
  137. package/src/tui/ui-content.mjs +426 -421
  138. package/src/tui/ui-content.mts +499 -0
  139. package/src/tui/ui-help.mjs +58 -67
  140. package/src/tui/ui-help.mts +87 -0
  141. package/src/tui/ui-mentions.mjs +34 -36
  142. package/src/tui/ui-mentions.mts +60 -0
  143. package/src/tui/ui-overlay-view.mjs +259 -267
  144. package/src/tui/ui-overlay-view.mts +322 -0
  145. package/src/tui/ui-suggest-view.mjs +54 -57
  146. package/src/tui/ui-suggest-view.mts +78 -0
  147. package/src/tui/ui-suggest.mjs +164 -167
  148. package/src/tui/ui-suggest.mts +248 -0
  149. package/src/tui/ui-theme.mjs +52 -57
  150. package/src/tui/ui-theme.mts +98 -0
  151. package/src/tui/ui-view.mjs +267 -275
  152. package/src/tui/ui-view.mts +308 -0
  153. package/src/tui/ui.mjs +372 -359
  154. package/src/tui/ui.mts +440 -0
  155. package/src/tui/wire.mjs +215 -0
  156. package/src/tui/wire.mts +383 -0
@@ -0,0 +1,152 @@
1
+ // Mention and Markdown helpers shared by the TUI message renderer and tests.
2
+ // Mentions become a private inline-code marker before parsing: default
3
+ // Markdown rendering shows just @name, and an inline hook recolors the code
4
+ // span to accent styling. Fenced/inline code is never rewritten.
5
+ import { RGBA, SyntaxStyle, TextAttributes } from "@opentui/core";
6
+ import type { CliRenderer, MarkdownInlineHook } from "@opentui/core";
7
+ import { t } from "./i18n.mjs";
8
+ import type { TuiTheme } from "./ui-theme.mjs";
9
+
10
+ export const MENTION_SENTINEL = "\u200B";
11
+ const MENTION_TAG = /^<at>(@[^\n<>`\\]{1,64})<\/at>/u;
12
+ const RESOURCE_MARKER = /\[\[resource:[^\]\s]+\]\]/gi;
13
+
14
+ /** Show attachment markers as a readable label and never leak the UUID. */
15
+ export function formatAttachments(text: unknown): string {
16
+ const source = String(text ?? "");
17
+ const markers = source.match(RESOURCE_MARKER);
18
+ if (!markers) return source;
19
+ const clean = source
20
+ .replace(RESOURCE_MARKER, " ")
21
+ .replace(/[ \t]+\n/g, "\n")
22
+ .replace(/\n[ \t]+/g, "\n")
23
+ .replace(/[ \t]{2,}/g, " ")
24
+ .trim();
25
+ if (!clean) return t("markdown.attachments");
26
+ return `${clean} · ${t("markdown.attachments")}`;
27
+ }
28
+
29
+ function closingBackticks(source: string, from: number, end: number, length: number): number {
30
+ let i = from;
31
+ while (i < end) {
32
+ if (source.charAt(i) !== "`") {
33
+ i += 1;
34
+ continue;
35
+ }
36
+ let run = 0;
37
+ while (i + run < end && source.charAt(i + run) === "`") run += 1;
38
+ if (run === length) return i;
39
+ i += run;
40
+ }
41
+ return -1;
42
+ }
43
+
44
+ function inlineMentions(line: string): string {
45
+ let out = "";
46
+ let index = 0;
47
+ while (index < line.length) {
48
+ if (line.charAt(index) === "`") {
49
+ let run = 0;
50
+ while (line.charAt(index + run) === "`") run += 1;
51
+ const close = closingBackticks(line, index + run, line.length, run);
52
+ if (close === -1) {
53
+ out += line.slice(index);
54
+ break;
55
+ }
56
+ out += line.slice(index, close + run);
57
+ index = close + run;
58
+ continue;
59
+ }
60
+ const match = line.slice(index).match(MENTION_TAG);
61
+ if (match) {
62
+ out += `\`${MENTION_SENTINEL}${match[1] ?? ""}\``;
63
+ index += (match[0] ?? "").length;
64
+ continue;
65
+ }
66
+ out += line.charAt(index);
67
+ index += 1;
68
+ }
69
+ return out;
70
+ }
71
+
72
+ /** Rewrite <at>@name</at> in prose, leaving fenced and inline code untouched. */
73
+ export function prepareMarkdown(text: unknown): string {
74
+ let fence: { char: string; length: number; indent: number } | null = null;
75
+ let indented = false;
76
+ return String(text ?? "")
77
+ .split("\n")
78
+ .map((line) => {
79
+ const clean = line.replace(/\r$/, "");
80
+ const marker = clean.match(/^([ \t]*)(`{3,}|~{3,})/);
81
+ const closing = clean.match(/^([ \t]*)(`{3,}|~{3,})[ \t]*$/);
82
+ if (fence) {
83
+ const closeFence = closing?.[2] ?? "";
84
+ if (closing && closeFence.charAt(0) === fence.char && closeFence.length >= fence.length
85
+ && (closing[1] ?? "").length <= fence.indent + 3) fence = null;
86
+ return line;
87
+ }
88
+ if (indented) {
89
+ if (!line.trim() || /^(?: {4,}|\t)/.test(line)) return line;
90
+ indented = false;
91
+ }
92
+ if (marker) {
93
+ const fenceText = marker[2] ?? "";
94
+ fence = { char: fenceText.charAt(0), length: fenceText.length, indent: (marker[1] ?? "").length };
95
+ return line;
96
+ }
97
+ if (/^(?: {4,}|\t)/.test(line)) {
98
+ indented = true;
99
+ return line;
100
+ }
101
+ return inlineMentions(line);
102
+ })
103
+ .join("\n");
104
+ }
105
+
106
+ /**
107
+ * Recolors the private mention marker inside a prose code span: strip the
108
+ * sentinel, drop the code background and draw it in the accent color.
109
+ */
110
+ export function createMentionInlineHook(theme: TuiTheme): MarkdownInlineHook {
111
+ const accent = RGBA.fromHex(theme.accent);
112
+ return (spans) =>
113
+ spans.flatMap((span) => {
114
+ if (!span.text.includes(MENTION_SENTINEL)) return [span];
115
+ const parts = span.text.split(MENTION_SENTINEL);
116
+ return parts.flatMap((text, index) => {
117
+ if (!text) return [];
118
+ if (!index) return [{ ...span, text }];
119
+ return [{ ...span, text, code: false, fg: accent, bg: undefined, attributes: TextAttributes.BOLD }];
120
+ });
121
+ });
122
+ }
123
+
124
+ export function createMarkdownSyntax(renderer: CliRenderer, theme: TuiTheme): SyntaxStyle {
125
+ return SyntaxStyle.fromStyles({
126
+ default: { fg: theme.text },
127
+ conceal: { fg: theme.faint },
128
+ "markup.heading": { fg: theme.accent, bold: true },
129
+ "markup.strong": { fg: theme.text, bold: true },
130
+ "markup.italic": { fg: theme.text, italic: true },
131
+ "markup.strikethrough": { fg: theme.muted },
132
+ "markup.raw": { fg: theme.accent, bg: theme.raised, bold: true },
133
+ "markup.list": { fg: theme.accent },
134
+ "markup.quote": { fg: theme.muted, italic: true },
135
+ "markup.link": { fg: theme.muted },
136
+ "markup.link.label": { fg: theme.accent, underline: true },
137
+ "markup.link.url": { fg: theme.muted },
138
+ keyword: { fg: theme.accent, bold: true },
139
+ string: { fg: theme.success },
140
+ comment: { fg: theme.muted, italic: true },
141
+ number: { fg: theme.warning },
142
+ constant: { fg: theme.warning },
143
+ function: { fg: theme.text, bold: true },
144
+ type: { fg: theme.warning },
145
+ variable: { fg: theme.text },
146
+ property: { fg: theme.text },
147
+ operator: { fg: theme.muted },
148
+ punctuation: { fg: theme.muted },
149
+ tag: { fg: theme.accent },
150
+ attribute: { fg: theme.warning },
151
+ }, renderer.nativeScene);
152
+ }
@@ -1,220 +1,217 @@
1
1
  // Mention helpers shared by the composer suggestions and outgoing messages.
2
2
  // Display names stay visible while typing; the wire format is applied on send.
3
3
  import { t } from "./i18n.mjs";
4
-
5
4
  const escapeRegExp = (value) => String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
6
-
7
5
  export function mentionNames(participants = []) {
8
- const names = [];
9
- const seen = new Set(["all"]);
10
- for (const participant of Array.isArray(participants) ? participants : []) {
11
- const name = String(participant?.name ?? participant?.id ?? "").trim();
12
- if (!name || seen.has(name.toLowerCase())) continue;
13
- seen.add(name.toLowerCase());
14
- names.push(name);
15
- }
16
- return names;
6
+ const names = [];
7
+ const seen = new Set(["all"]);
8
+ const list = Array.isArray(participants) ? participants : [];
9
+ for (const participant of list) {
10
+ const name = String(participant?.name ?? participant?.id ?? "").trim();
11
+ if (!name || seen.has(name.toLowerCase()))
12
+ continue;
13
+ seen.add(name.toLowerCase());
14
+ names.push(name);
15
+ }
16
+ return names;
17
17
  }
18
-
19
18
  export function mentionItems(participants, query = "") {
20
- const needle = String(query ?? "").toLowerCase();
21
- const items = mentionNames(participants).map((name) => ({
22
- name,
23
- insert: `@${name}`,
24
- label: name,
25
- }));
26
- items.push({ name: "all", insert: "@all", label: t("mentions.everyone") });
27
- return items.filter((item) => {
28
- const haystack = `${item.label} ${item.name}`.toLowerCase();
29
- return !needle || haystack.includes(needle);
30
- });
19
+ const needle = String(query ?? "").toLowerCase();
20
+ const items = mentionNames(participants).map((name) => ({
21
+ name,
22
+ insert: `@${name}`,
23
+ label: name,
24
+ }));
25
+ items.push({ name: "all", insert: "@all", label: t("mentions.everyone") });
26
+ return items.filter((item) => {
27
+ const haystack = `${item.label} ${item.name}`.toLowerCase();
28
+ return !needle || haystack.includes(needle);
29
+ });
31
30
  }
32
-
33
31
  /** Locate the @token at the end of the draft, outside email-like tokens. */
34
32
  export function mentionTrigger(text) {
35
- const source = String(text ?? "");
36
- const match = source.match(/(^|[^\p{L}\p{N}_@])@([^\n@]{0,64})$/u);
37
- if (!match) return null;
38
- const query = match[2];
39
- return { start: source.length - query.length - 1, query, cursor: source.length };
33
+ const source = String(text ?? "");
34
+ const match = source.match(/(^|[^\p{L}\p{N}_@])@([^\n@]{0,64})$/u);
35
+ if (!match)
36
+ return null;
37
+ const query = match[2] ?? "";
38
+ return { start: source.length - query.length - 1, query, cursor: source.length };
40
39
  }
41
-
42
40
  export function insertMention(draft, trigger, insert) {
43
- const source = String(draft ?? "");
44
- const at = trigger ?? mentionTrigger(source);
45
- if (!at) return source;
46
- const before = source.slice(0, at.start);
47
- const after = source.slice(at.cursor ?? source.length);
48
- return `${before}${insert} ${after}`;
41
+ const source = String(draft ?? "");
42
+ const at = trigger ?? mentionTrigger(source);
43
+ if (!at)
44
+ return source;
45
+ const before = source.slice(0, at.start);
46
+ const after = source.slice(at.cursor ?? source.length);
47
+ return `${before}${insert} ${after}`;
49
48
  }
50
-
51
49
  /** Find a closing backtick run of exactly `length`, never a longer run. */
52
50
  function closingBackticks(source, from, end, length) {
53
- let i = from;
54
- while (i < end) {
55
- if (source[i] !== "`") {
56
- i += 1;
57
- continue;
51
+ let i = from;
52
+ while (i < end) {
53
+ if (source.charAt(i) !== "`") {
54
+ i += 1;
55
+ continue;
56
+ }
57
+ let run = 0;
58
+ while (i + run < end && source.charAt(i + run) === "`")
59
+ run += 1;
60
+ if (run === length)
61
+ return i;
62
+ i += run;
58
63
  }
59
- let run = 0;
60
- while (i + run < end && source[i + run] === "`") run += 1;
61
- if (run === length) return i;
62
- i += run;
63
- }
64
- return -1;
64
+ return -1;
65
65
  }
66
-
67
66
  /** Split Markdown prose from fenced/indented code and inline code spans. */
68
67
  function codeSegments(source) {
69
- const segments = [];
70
- let proseStart = 0;
71
- let lineStart = 0;
72
- const push = (start, end, code) => {
73
- if (end > start) segments.push({ text: source.slice(start, end), code, start, end });
74
- };
75
- while (lineStart < source.length) {
76
- const lineEnd = source.indexOf("\n", lineStart);
77
- const end = lineEnd < 0 ? source.length : lineEnd;
78
- const line = source.slice(lineStart, end).replace(/\r$/, "");
79
- const opener = /^([ \t]*)(`{3,}|~{3,})/.exec(line);
80
- if (opener) {
81
- if (lineStart > proseStart) splitInlineCode(source, proseStart, lineStart, segments);
82
- const fence = opener[2];
83
- const indent = opener[1].length;
84
- let scan = lineEnd < 0 ? source.length : lineEnd + 1;
85
- let closed = source.length;
86
- while (scan < source.length) {
87
- const nextEnd = source.indexOf("\n", scan);
88
- const stop = nextEnd < 0 ? source.length : nextEnd;
89
- const close = /^([ \t]*)(`{3,}|~{3,})[ \t]*$/.exec(source.slice(scan, stop).replace(/\r$/, ""));
90
- if (close && close[2][0] === fence[0] && close[2].length >= fence.length
91
- && close[1].length <= indent + 3) {
92
- closed = nextEnd < 0 ? source.length : nextEnd + 1;
93
- break;
68
+ const segments = [];
69
+ let proseStart = 0;
70
+ let lineStart = 0;
71
+ const push = (start, end, code) => {
72
+ if (end > start)
73
+ segments.push({ text: source.slice(start, end), code, start, end });
74
+ };
75
+ while (lineStart < source.length) {
76
+ const lineEnd = source.indexOf("\n", lineStart);
77
+ const end = lineEnd < 0 ? source.length : lineEnd;
78
+ const line = source.slice(lineStart, end).replace(/\r$/, "");
79
+ const opener = /^([ \t]*)(`{3,}|~{3,})/.exec(line);
80
+ if (opener) {
81
+ if (lineStart > proseStart)
82
+ splitInlineCode(source, proseStart, lineStart, segments);
83
+ const fence = opener[2] ?? "";
84
+ const indent = (opener[1] ?? "").length;
85
+ let scan = lineEnd < 0 ? source.length : lineEnd + 1;
86
+ let closed = source.length;
87
+ while (scan < source.length) {
88
+ const nextEnd = source.indexOf("\n", scan);
89
+ const stop = nextEnd < 0 ? source.length : nextEnd;
90
+ const close = /^([ \t]*)(`{3,}|~{3,})[ \t]*$/.exec(source.slice(scan, stop).replace(/\r$/, ""));
91
+ const closeFence = close?.[2] ?? "";
92
+ if (close && closeFence.charAt(0) === fence.charAt(0) && closeFence.length >= fence.length
93
+ && (close[1] ?? "").length <= indent + 3) {
94
+ closed = nextEnd < 0 ? source.length : nextEnd + 1;
95
+ break;
96
+ }
97
+ scan = nextEnd < 0 ? source.length : nextEnd + 1;
98
+ }
99
+ push(lineStart, closed, true);
100
+ proseStart = closed;
101
+ lineStart = closed;
102
+ continue;
94
103
  }
95
- scan = nextEnd < 0 ? source.length : nextEnd + 1;
96
- }
97
- push(lineStart, closed, true);
98
- proseStart = closed;
99
- lineStart = closed;
100
- continue;
101
- }
102
- // CommonMark indented code is also literal. Treat four spaces/tab as code
103
- // rather than risking a mention rewrite inside it.
104
- if (/^(?: {4,}|\t)/.test(line)) {
105
- if (lineStart > proseStart) splitInlineCode(source, proseStart, lineStart, segments);
106
- let scan = lineStart;
107
- let closed = source.length;
108
- while (scan < source.length) {
109
- const nextEnd = source.indexOf("\n", scan);
110
- const stop = nextEnd < 0 ? source.length : nextEnd;
111
- const current = source.slice(scan, stop).replace(/\r$/, "");
112
- if (current.trim() && !/^(?: {4,}|\t)/.test(current)) {
113
- closed = scan;
114
- break;
104
+ // CommonMark indented code is also literal. Treat four spaces/tab as code
105
+ // rather than risking a mention rewrite inside it.
106
+ if (/^(?: {4,}|\t)/.test(line)) {
107
+ if (lineStart > proseStart)
108
+ splitInlineCode(source, proseStart, lineStart, segments);
109
+ let scan = lineStart;
110
+ let closed = source.length;
111
+ while (scan < source.length) {
112
+ const nextEnd = source.indexOf("\n", scan);
113
+ const stop = nextEnd < 0 ? source.length : nextEnd;
114
+ const current = source.slice(scan, stop).replace(/\r$/, "");
115
+ if (current.trim() && !/^(?: {4,}|\t)/.test(current)) {
116
+ closed = scan;
117
+ break;
118
+ }
119
+ scan = nextEnd < 0 ? source.length : nextEnd + 1;
120
+ }
121
+ push(lineStart, closed, true);
122
+ proseStart = closed;
123
+ lineStart = closed;
124
+ continue;
115
125
  }
116
- scan = nextEnd < 0 ? source.length : nextEnd + 1;
117
- }
118
- push(lineStart, closed, true);
119
- proseStart = closed;
120
- lineStart = closed;
121
- continue;
126
+ lineStart = lineEnd < 0 ? source.length : lineEnd + 1;
122
127
  }
123
- lineStart = lineEnd < 0 ? source.length : lineEnd + 1;
124
- }
125
- splitInlineCode(source, proseStart, source.length, segments);
126
- return segments;
128
+ splitInlineCode(source, proseStart, source.length, segments);
129
+ return segments;
127
130
  }
128
-
129
131
  function splitInlineCode(source, start, end, segments) {
130
- let prose = start;
131
- let i = start;
132
- while (i < end) {
133
- if (source[i] !== "`") {
134
- i += 1;
135
- continue;
136
- }
137
- let run = 1;
138
- while (i + run < end && source[i + run] === "`") run += 1;
139
- const close = closingBackticks(source, i + run, end, run);
140
- if (close < 0) {
141
- i += run;
142
- continue;
132
+ let prose = start;
133
+ let i = start;
134
+ while (i < end) {
135
+ if (source.charAt(i) !== "`") {
136
+ i += 1;
137
+ continue;
138
+ }
139
+ let run = 1;
140
+ while (i + run < end && source.charAt(i + run) === "`")
141
+ run += 1;
142
+ const close = closingBackticks(source, i + run, end, run);
143
+ if (close < 0) {
144
+ i += run;
145
+ continue;
146
+ }
147
+ if (i > prose)
148
+ segments.push({ text: source.slice(prose, i), code: false, start: prose, end: i });
149
+ segments.push({ text: source.slice(i, close + run), code: true, start: i, end: close + run });
150
+ prose = close + run;
151
+ i = prose;
143
152
  }
144
- if (i > prose) segments.push({ text: source.slice(prose, i), code: false, start: prose, end: i });
145
- segments.push({ text: source.slice(i, close + run), code: true, start: i, end: close + run });
146
- prose = close + run;
147
- i = prose;
148
- }
149
- if (end > prose) segments.push({ text: source.slice(prose, end), code: false, start: prose, end });
153
+ if (end > prose)
154
+ segments.push({ text: source.slice(prose, end), code: false, start: prose, end });
150
155
  }
151
-
152
156
  function convertPlain(segment, names) {
153
- if (!segment) return segment;
154
- const candidates = ["all", ...names].sort((a, b) => b.length - a.length);
155
- const pattern = new RegExp(
156
- `(?<![\\p{L}\\p{N}_.@])@(${candidates.map(escapeRegExp).join("|")})(?![\\p{L}\\p{N}_])`,
157
- "gu",
158
- );
159
- return segment.replace(pattern, (_match, name) => `<at>@${name}</at>`);
157
+ if (!segment)
158
+ return segment;
159
+ const candidates = ["all", ...names].sort((a, b) => b.length - a.length);
160
+ const pattern = new RegExp(`(?<![\\p{L}\\p{N}_.@])@(${candidates.map(escapeRegExp).join("|")})(?![\\p{L}\\p{N}_])`, "gu");
161
+ return segment.replace(pattern, (_match, name) => `<at>@${name}</at>`);
160
162
  }
161
-
162
163
  function convertProse(source, names) {
163
- const tag = /<at>[\s\S]*?<\/at>/giu;
164
- let output = "";
165
- let last = 0;
166
- for (const match of source.matchAll(tag)) {
167
- output += convertPlain(source.slice(last, match.index), names);
168
- output += match[0];
169
- last = match.index + match[0].length;
170
- }
171
- return output + convertPlain(source.slice(last), names);
164
+ const tag = /<at>[\s\S]*?<\/at>/giu;
165
+ let output = "";
166
+ let last = 0;
167
+ for (const match of source.matchAll(tag)) {
168
+ output += convertPlain(source.slice(last, match.index), names);
169
+ output += match[0];
170
+ last = match.index + match[0].length;
171
+ }
172
+ return output + convertPlain(source.slice(last), names);
172
173
  }
173
-
174
174
  /** Convert known @names and @all to <at> tags, preserving code and existing tags. */
175
175
  export function canonicalizeMentions(text, participants = []) {
176
- const source = String(text ?? "");
177
- const names = mentionNames(participants);
178
- return codeSegments(source)
179
- .map((segment) => (segment.code ? segment.text : convertProse(segment.text, names)))
180
- .join("");
176
+ const source = String(text ?? "");
177
+ const names = mentionNames(participants);
178
+ return codeSegments(source)
179
+ .map((segment) => (segment.code ? segment.text : convertProse(segment.text, names)))
180
+ .join("");
181
181
  }
182
-
183
182
  /** Character ranges for visible mention chips, excluding email-like tokens. */
184
183
  export function mentionRanges(text, participants = []) {
185
- const source = String(text ?? "");
186
- const ranges = [];
187
- const candidates = ["all", ...mentionNames(participants)].sort((a, b) => b.length - a.length);
188
- const pattern = new RegExp(
189
- `(?<![\\p{L}\\p{N}_.@])@(${candidates.map(escapeRegExp).join("|")})(?![\\p{L}\\p{N}_])`,
190
- "gu",
191
- );
192
- for (const segment of codeSegments(source)) {
193
- if (segment.code) continue;
194
- const tag = /<at>([\s\S]*?)<\/at>/giu;
195
- let last = 0;
196
- const plain = [];
197
- for (const match of segment.text.matchAll(tag)) {
198
- const inner = match[1];
199
- const trimmed = inner.trim();
200
- const at = trimmed.indexOf("@");
201
- if (at >= 0) {
202
- const innerStart = segment.start + match.index + 4 + (inner.length - inner.trimStart().length);
203
- ranges.push({ start: innerStart + at, end: innerStart + trimmed.length });
204
- }
205
- plain.push([last, match.index]);
206
- last = match.index + match[0].length;
207
- }
208
- plain.push([last, segment.text.length]);
209
- for (const [start, end] of plain) {
210
- const piece = segment.text.slice(start, end);
211
- for (const match of piece.matchAll(pattern)) {
212
- ranges.push({
213
- start: segment.start + start + match.index,
214
- end: segment.start + start + match.index + match[0].length,
215
- });
216
- }
184
+ const source = String(text ?? "");
185
+ const ranges = [];
186
+ const candidates = ["all", ...mentionNames(participants)].sort((a, b) => b.length - a.length);
187
+ const pattern = new RegExp(`(?<![\\p{L}\\p{N}_.@])@(${candidates.map(escapeRegExp).join("|")})(?![\\p{L}\\p{N}_])`, "gu");
188
+ for (const segment of codeSegments(source)) {
189
+ if (segment.code)
190
+ continue;
191
+ const tag = /<at>([\s\S]*?)<\/at>/giu;
192
+ let last = 0;
193
+ const plain = [];
194
+ for (const match of segment.text.matchAll(tag)) {
195
+ const inner = match[1] ?? "";
196
+ const trimmed = inner.trim();
197
+ const at = trimmed.indexOf("@");
198
+ if (at >= 0) {
199
+ const innerStart = segment.start + match.index + 4 + (inner.length - inner.trimStart().length);
200
+ ranges.push({ start: innerStart + at, end: innerStart + trimmed.length });
201
+ }
202
+ plain.push([last, match.index]);
203
+ last = match.index + match[0].length;
204
+ }
205
+ plain.push([last, segment.text.length]);
206
+ for (const [start, end] of plain) {
207
+ const piece = segment.text.slice(start, end);
208
+ for (const match of piece.matchAll(pattern)) {
209
+ ranges.push({
210
+ start: segment.start + start + match.index,
211
+ end: segment.start + start + match.index + match[0].length,
212
+ });
213
+ }
214
+ }
217
215
  }
218
- }
219
- return ranges.sort((a, b) => a.start - b.start);
220
- }
216
+ return ranges.sort((a, b) => a.start - b.start);
217
+ }