@alexsun-top/opencode-chat-room 0.8.4 → 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.
- package/CHANGELOG.md +5 -1
- package/README.md +4 -4
- package/README.zh-CN.md +4 -4
- package/cli.ts +1 -1
- package/dist/cli.js +35 -16
- package/package.json +14 -11
- package/packages/chat-room-channeld/README.md +3 -2
- package/packages/chat-room-channeld/src/inbound.ts +3 -6
- package/packages/chat-room-channeld/src/pg/sql.mjs +157 -142
- package/packages/chat-room-channeld/src/pg/sql.mts +175 -0
- package/packages/chat-room-channeld/src/pg/worker.mjs +107 -99
- package/packages/chat-room-channeld/src/pg/worker.mts +166 -0
- package/plugin.ts +1 -1
- package/server/assets/chat/admin.js +4 -1
- package/server/assets/chat/announcements.js +3 -0
- package/server/assets/chat/attachments.js +3 -3
- package/server/assets/chat/clipboard.js +1 -1
- package/server/assets/chat/copy-action.js +1 -1
- package/server/assets/chat/doc-preview.js +1 -1
- package/server/assets/chat/editor.js +11 -7
- package/server/assets/chat/events.js +7 -5
- package/server/assets/chat/files.js +5 -1
- package/server/assets/chat/integration-editor.js +2 -0
- package/server/assets/chat/integrations.js +1 -1
- package/server/assets/chat/join-requests.js +1 -1
- package/server/assets/chat/markdown-footnotes.js +8 -7
- package/server/assets/chat/markdown.js +6 -5
- package/server/assets/chat/members.js +3 -3
- package/server/assets/chat/mentions.js +8 -5
- package/server/assets/chat/message-cache.js +1 -1
- package/server/assets/chat/message-polling.js +2 -2
- package/server/assets/chat/message-window.js +1 -1
- package/server/assets/chat/messages.js +17 -3
- package/server/assets/chat/office-preview.js +3 -3
- package/server/assets/chat/palette.js +5 -3
- package/server/assets/chat/panels.js +19 -0
- package/server/assets/chat/questions.js +3 -2
- package/server/assets/chat/reminders.js +3 -0
- package/server/assets/chat/replies.js +6 -3
- package/server/assets/chat/resource-format.js +2 -2
- package/server/assets/chat/rooms.js +7 -4
- package/server/assets/chat/settings.js +4 -3
- package/server/assets/chat/shortcuts.js +3 -1
- package/server/assets/chat/side-pane.js +1 -1
- package/server/assets/chat/state.js +30 -11
- package/server/assets/chat/system-events.js +3 -2
- package/server/assets/chat/system-messages.js +15 -15
- package/server/assets/chat/tasks.js +4 -3
- package/server/assets/chat/window-controls.js +3 -2
- package/server/assets/conversation.css +14 -2
- package/server/assets/doc-preview.css +6 -0
- package/server/assets/office/THIRD-PARTY-NOTICES.txt +23 -1
- package/server/assets/office/licenses/docx-preview-HEADER.txt +6 -0
- package/server/assets/office/licenses/echarts-LICENSE-d3.txt +27 -0
- package/server/assets/office/licenses/echarts-NOTICE.txt +5 -0
- package/server/assets/office/licenses/immediate-LICENSE.txt +20 -0
- package/server/assets/office/licenses/lie-LICENSE.txt +7 -0
- package/server/assets/office/licenses/pako-LICENSE.txt +21 -0
- package/server/assets/office/licenses/pako-ZLIB.txt +18 -0
- package/server/assets/office/licenses/setimmediate-LICENSE.txt +20 -0
- package/server/sw.js +3 -1
- package/skills/chatroom/SKILL.md +9 -0
- package/src/README.md +1 -1
- package/src/agent/agent-receipts.ts +12 -4
- package/src/center/center-client.ts +1 -1
- package/src/state/pg-sql.mjs +157 -142
- package/src/state/pg-sql.mts +175 -0
- package/src/state/pg-worker.mjs +107 -99
- package/src/state/pg-worker.mts +166 -0
- package/src/state/store.ts +21 -7
- package/src/tui/app-types.mjs +1 -0
- package/src/tui/app-types.mts +175 -0
- package/src/tui/app.mjs +426 -423
- package/src/tui/app.mts +500 -0
- package/src/tui/config.mjs +166 -158
- package/src/tui/config.mts +247 -0
- package/src/tui/http.mjs +239 -206
- package/src/tui/http.mts +412 -0
- package/src/tui/i18n.mjs +17 -20
- package/src/tui/i18n.mts +42 -0
- package/src/tui/info-app.mjs +431 -427
- package/src/tui/info-app.mts +500 -0
- package/src/tui/info-content.mjs +205 -199
- package/src/tui/info-content.mts +341 -0
- package/src/tui/join-app.mjs +75 -64
- package/src/tui/join-app.mts +99 -0
- package/src/tui/locales/en.mjs +302 -320
- package/src/tui/locales/en.mts +349 -0
- package/src/tui/locales/zh-CN.mjs +302 -320
- package/src/tui/locales/zh-CN.mts +349 -0
- package/src/tui/main.mjs +219 -213
- package/src/tui/main.mts +255 -0
- package/src/tui/markdown.mjs +128 -272
- package/src/tui/markdown.mts +152 -0
- package/src/tui/mentions.mjs +181 -184
- package/src/tui/mentions.mts +251 -0
- package/src/tui/message-app.mjs +181 -173
- package/src/tui/message-app.mts +213 -0
- package/src/tui/message-view.mjs +90 -111
- package/src/tui/message-view.mts +151 -0
- package/src/tui/messages.mjs +55 -49
- package/src/tui/messages.mts +106 -0
- package/src/tui/palette-app.mjs +364 -344
- package/src/tui/palette-app.mts +388 -0
- package/src/tui/palette.mjs +100 -98
- package/src/tui/palette.mts +176 -0
- package/src/tui/prefs.mjs +56 -63
- package/src/tui/prefs.mts +126 -0
- package/src/tui/reminder-app.mjs +191 -181
- package/src/tui/reminder-app.mts +228 -0
- package/src/tui/room-settings-app.mjs +257 -242
- package/src/tui/room-settings-app.mts +301 -0
- package/src/tui/session.mjs +30 -29
- package/src/tui/session.mts +51 -0
- package/src/tui/settings-app.mjs +151 -138
- package/src/tui/settings-app.mts +179 -0
- package/src/tui/ui-content.mjs +426 -421
- package/src/tui/ui-content.mts +499 -0
- package/src/tui/ui-help.mjs +58 -67
- package/src/tui/ui-help.mts +87 -0
- package/src/tui/ui-mentions.mjs +34 -36
- package/src/tui/ui-mentions.mts +60 -0
- package/src/tui/ui-overlay-view.mjs +259 -267
- package/src/tui/ui-overlay-view.mts +322 -0
- package/src/tui/ui-suggest-view.mjs +54 -57
- package/src/tui/ui-suggest-view.mts +78 -0
- package/src/tui/ui-suggest.mjs +164 -167
- package/src/tui/ui-suggest.mts +248 -0
- package/src/tui/ui-theme.mjs +52 -57
- package/src/tui/ui-theme.mts +98 -0
- package/src/tui/ui-view.mjs +267 -275
- package/src/tui/ui-view.mts +308 -0
- package/src/tui/ui.mjs +372 -359
- package/src/tui/ui.mts +440 -0
- package/src/tui/wire.mjs +215 -0
- package/src/tui/wire.mts +383 -0
package/src/tui/main.mts
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// CLI entry for `opencode-chat-room cli`. Config, HTTP and session helpers are
|
|
2
|
+
// imported lazily so --help/--version work without a terminal or OpenTUI.
|
|
3
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { createApp } from "./app.mjs";
|
|
6
|
+
import { DEFAULT_LANGUAGE, getLanguage, normalizeLanguage, setLanguage, t } from "./i18n.mjs";
|
|
7
|
+
import type { CliRenderer } from "@opentui/core";
|
|
8
|
+
import type { ResolvedTuiConfig, TuiConfigFlags } from "./config.mjs";
|
|
9
|
+
import type { Tui } from "./ui.mjs";
|
|
10
|
+
|
|
11
|
+
const NAME = "opencode-chat-room cli";
|
|
12
|
+
const FALLBACK_VERSION = "0.8.1";
|
|
13
|
+
const LANGUAGE_VALUES = ["en", "zh", "zh-cn", "zh_cn"];
|
|
14
|
+
|
|
15
|
+
export const HELP = t("cli.help");
|
|
16
|
+
|
|
17
|
+
interface CliOptions extends TuiConfigFlags {
|
|
18
|
+
help: boolean;
|
|
19
|
+
setup: boolean;
|
|
20
|
+
version: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function helpText(language: unknown): string {
|
|
24
|
+
const previous = getLanguage();
|
|
25
|
+
setLanguage(language);
|
|
26
|
+
const text = t("cli.help");
|
|
27
|
+
setLanguage(previous);
|
|
28
|
+
return text;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function parseArgs(argv: readonly string[] = []): CliOptions | { error: string } {
|
|
32
|
+
const options: CliOptions = {
|
|
33
|
+
help: false,
|
|
34
|
+
version: false,
|
|
35
|
+
setup: false,
|
|
36
|
+
name: undefined,
|
|
37
|
+
language: undefined,
|
|
38
|
+
theme: undefined,
|
|
39
|
+
server: undefined,
|
|
40
|
+
};
|
|
41
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
42
|
+
const arg = argv[index]!;
|
|
43
|
+
if (arg === "--help" || arg === "-h") options.help = true;
|
|
44
|
+
else if (arg === "--version" || arg === "-v") options.version = true;
|
|
45
|
+
else if (arg === "--setup") options.setup = true;
|
|
46
|
+
else if (arg.startsWith("--")) {
|
|
47
|
+
const split = arg.indexOf("=");
|
|
48
|
+
const key = split === -1 ? arg : arg.slice(0, split);
|
|
49
|
+
if (!["--name", "--language", "--theme", "--server"].includes(key)) {
|
|
50
|
+
return { error: `unknown option: ${arg}` };
|
|
51
|
+
}
|
|
52
|
+
let value = split === -1 ? argv[index + 1] : arg.slice(split + 1);
|
|
53
|
+
if (split === -1) {
|
|
54
|
+
if (!value || value.startsWith("-")) return { error: `${key} requires a value` };
|
|
55
|
+
index += 1;
|
|
56
|
+
}
|
|
57
|
+
if (!value) return { error: `${key} requires a value` };
|
|
58
|
+
if (key === "--language") {
|
|
59
|
+
if (!LANGUAGE_VALUES.includes(value.trim().toLowerCase())) {
|
|
60
|
+
return { error: "--language must be en or zh-CN" };
|
|
61
|
+
}
|
|
62
|
+
options.language = normalizeLanguage(value);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (key === "--theme") {
|
|
66
|
+
value = value.trim().toLowerCase();
|
|
67
|
+
if (value !== "dark" && value !== "light") return { error: "--theme must be dark or light" };
|
|
68
|
+
options.theme = value;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
if (key === "--name") options.name = value;
|
|
72
|
+
else options.server = value;
|
|
73
|
+
} else {
|
|
74
|
+
return { error: `unknown option: ${arg}` };
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return options;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function runtimeProblem() {
|
|
81
|
+
const [bunMajor = 0, bunMinor = 0, bunPatch = 0] = (process.versions.bun ?? "").split(".").map(Number);
|
|
82
|
+
if (bunMajor > 1 || (bunMajor === 1 && (bunMinor > 3 || (bunMinor === 3 && bunPatch >= 14)))) {
|
|
83
|
+
return "";
|
|
84
|
+
}
|
|
85
|
+
return "the terminal client requires Bun 1.3.14+. Install Bun and run bunx --bun @alexsun-top/opencode-chat-room cli.";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function version() {
|
|
89
|
+
try {
|
|
90
|
+
for (const candidate of ["../../package.json", "../package.json"]) {
|
|
91
|
+
try {
|
|
92
|
+
const data = JSON.parse(readFileSync(new URL(candidate, import.meta.url), "utf8"));
|
|
93
|
+
if (typeof data.version === "string") return data.version;
|
|
94
|
+
} catch {
|
|
95
|
+
// Source runs from src/tui, bundled CLI runs from dist.
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return FALLBACK_VERSION;
|
|
99
|
+
} catch {
|
|
100
|
+
return FALLBACK_VERSION;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function redact(text: string, token?: string): string {
|
|
105
|
+
if (!token) return text;
|
|
106
|
+
return String(text).split(token).join("***");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function errorText(error: unknown): string {
|
|
110
|
+
return error instanceof Error ? error.message : String(error ?? "unknown error");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async function helpLanguage(options: CliOptions) {
|
|
114
|
+
const explicit = options.language ?? process.env.CHAT_ROOM_TUI_LANGUAGE;
|
|
115
|
+
if (explicit) return normalizeLanguage(explicit);
|
|
116
|
+
try {
|
|
117
|
+
const { loadTuiPrefs } = await import("./prefs.mjs");
|
|
118
|
+
return normalizeLanguage(loadTuiPrefs(process.env).language ?? DEFAULT_LANGUAGE);
|
|
119
|
+
} catch {
|
|
120
|
+
return DEFAULT_LANGUAGE;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** --setup always opens settings; a missing tui.json means first run. */
|
|
125
|
+
export function shouldOpenSetup(options: Partial<Pick<CliOptions, "setup">> = {}, prefsExists = true): boolean {
|
|
126
|
+
return Boolean(options.setup) || !prefsExists;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function main(argv = process.argv.slice(2)): Promise<number> {
|
|
130
|
+
const options = parseArgs(argv);
|
|
131
|
+
if ("error" in options) {
|
|
132
|
+
process.stderr.write(`${NAME}: ${options.error}\n`);
|
|
133
|
+
return 1;
|
|
134
|
+
}
|
|
135
|
+
if (options.help) {
|
|
136
|
+
process.stdout.write(helpText(await helpLanguage(options)));
|
|
137
|
+
return 0;
|
|
138
|
+
}
|
|
139
|
+
if (options.version) {
|
|
140
|
+
process.stdout.write(`${NAME} ${version()}\n`);
|
|
141
|
+
return 0;
|
|
142
|
+
}
|
|
143
|
+
const unsupported = runtimeProblem();
|
|
144
|
+
if (unsupported) {
|
|
145
|
+
process.stderr.write(`${NAME}: ${unsupported}\n`);
|
|
146
|
+
return 1;
|
|
147
|
+
}
|
|
148
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY) {
|
|
149
|
+
process.stderr.write(`${NAME}: requires an interactive terminal. Use --help for usage.\n`);
|
|
150
|
+
return 1;
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
import.meta.resolve("@opentui/core");
|
|
154
|
+
} catch {
|
|
155
|
+
process.stderr.write(`${NAME}: OpenTUI is not installed. Run bunx --bun @alexsun-top/opencode-chat-room cli.\n`);
|
|
156
|
+
return 1;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
let config: ResolvedTuiConfig | undefined;
|
|
160
|
+
let renderer: CliRenderer | undefined;
|
|
161
|
+
let ui: Tui | undefined;
|
|
162
|
+
try {
|
|
163
|
+
const [
|
|
164
|
+
{ resolveTuiConfig, findTuiConfigFile, loadTuiConfig },
|
|
165
|
+
{ loadSession, saveSession },
|
|
166
|
+
{ createApi },
|
|
167
|
+
{ loadTuiPrefs, saveTuiPrefs, resolveTuiPreferences, tuiPrefsFile },
|
|
168
|
+
] = await Promise.all([
|
|
169
|
+
import("./config.mjs"),
|
|
170
|
+
import("./session.mjs"),
|
|
171
|
+
import("./http.mjs"),
|
|
172
|
+
import("./prefs.mjs"),
|
|
173
|
+
]);
|
|
174
|
+
const prefs = loadTuiPrefs(process.env);
|
|
175
|
+
const project = loadTuiConfig(process.env, process.cwd());
|
|
176
|
+
config = resolveTuiConfig(process.env, process.cwd(), { flags: options, prefs, project });
|
|
177
|
+
const preferences = resolveTuiPreferences(process.env, process.cwd(), options, { prefs, project });
|
|
178
|
+
setLanguage(preferences.language);
|
|
179
|
+
const requested = options.name?.trim();
|
|
180
|
+
const session = loadSession(config.stateFile, requested || config.participant);
|
|
181
|
+
const participant = requested || session.name || config.participant;
|
|
182
|
+
if (session.name !== participant) {
|
|
183
|
+
try {
|
|
184
|
+
saveSession(config.stateFile, { ...session, name: participant });
|
|
185
|
+
} catch {
|
|
186
|
+
// A read-only state file only costs a new identity next time.
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const api = createApi({ url: config.url, token: config.token, requestTimeoutMs: config.requestTimeoutMs });
|
|
190
|
+
let host = config.url;
|
|
191
|
+
try {
|
|
192
|
+
host = new URL(config.url).host;
|
|
193
|
+
} catch {
|
|
194
|
+
// Keep the raw URL when it is not parseable.
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const [{ createCliRenderer }, { createTui }] = await Promise.all([
|
|
198
|
+
import("@opentui/core"),
|
|
199
|
+
import("./ui.mjs"),
|
|
200
|
+
]);
|
|
201
|
+
renderer = await createCliRenderer({
|
|
202
|
+
screenMode: "alternate-screen",
|
|
203
|
+
exitOnCtrlC: false,
|
|
204
|
+
useMouse: true,
|
|
205
|
+
targetFps: 30,
|
|
206
|
+
clearOnShutdown: true,
|
|
207
|
+
});
|
|
208
|
+
const app = createApp({
|
|
209
|
+
api,
|
|
210
|
+
sessionID: session.sessionID,
|
|
211
|
+
participant,
|
|
212
|
+
host,
|
|
213
|
+
settings: {
|
|
214
|
+
server: config.url,
|
|
215
|
+
participant,
|
|
216
|
+
sessionFile: config.stateFile,
|
|
217
|
+
configFile: findTuiConfigFile(process.env, process.cwd()) ?? "",
|
|
218
|
+
prefsFile: tuiPrefsFile(process.env),
|
|
219
|
+
language: preferences.language,
|
|
220
|
+
theme: preferences.theme,
|
|
221
|
+
requestTimeoutMs: config.requestTimeoutMs,
|
|
222
|
+
pollTimeoutMs: 20000,
|
|
223
|
+
token: config.token,
|
|
224
|
+
},
|
|
225
|
+
savePrefs: (next) => saveTuiPrefs(process.env, next),
|
|
226
|
+
downloadDir: path.join(process.cwd(), "downloads"),
|
|
227
|
+
});
|
|
228
|
+
ui = createTui(renderer, app);
|
|
229
|
+
await app.init();
|
|
230
|
+
ui.sync();
|
|
231
|
+
// Settings need a working server to boot, so first run and --setup open
|
|
232
|
+
// here, right after the room list loads. Without a configured server the
|
|
233
|
+
// config error above still exits before the terminal is touched.
|
|
234
|
+
if (shouldOpenSetup(options, existsSync(preferences.prefsFile))) app.openSettingsEditor();
|
|
235
|
+
|
|
236
|
+
return await new Promise<number>((resolve) => {
|
|
237
|
+
const finish = () => resolve(0);
|
|
238
|
+
if (app.state.quit) {
|
|
239
|
+
finish();
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const unsubscribe = app.subscribe(() => {
|
|
243
|
+
if (!app.state.quit) return;
|
|
244
|
+
unsubscribe();
|
|
245
|
+
finish();
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
} catch (error) {
|
|
249
|
+
process.stderr.write(`${NAME}: ${redact(errorText(error), config?.token)}\n`);
|
|
250
|
+
return 1;
|
|
251
|
+
} finally {
|
|
252
|
+
ui?.destroy();
|
|
253
|
+
renderer?.destroy();
|
|
254
|
+
}
|
|
255
|
+
}
|
package/src/tui/markdown.mjs
CHANGED
|
@@ -1,296 +1,152 @@
|
|
|
1
1
|
// Mention and Markdown helpers shared by the TUI message renderer and tests.
|
|
2
2
|
// Mentions become a private inline-code marker before parsing: default
|
|
3
|
-
// Markdown rendering shows just @name, and
|
|
4
|
-
//
|
|
5
|
-
import {
|
|
6
|
-
StyledText,
|
|
7
|
-
SyntaxStyle,
|
|
8
|
-
TextRenderable,
|
|
9
|
-
bg,
|
|
10
|
-
bold,
|
|
11
|
-
dim,
|
|
12
|
-
fg,
|
|
13
|
-
italic,
|
|
14
|
-
strikethrough,
|
|
15
|
-
underline,
|
|
16
|
-
} from "@opentui/core";
|
|
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";
|
|
17
6
|
import { t } from "./i18n.mjs";
|
|
18
|
-
|
|
19
7
|
export const MENTION_SENTINEL = "\u200B";
|
|
20
8
|
const MENTION_TAG = /^<at>(@[^\n<>`\\]{1,64})<\/at>/u;
|
|
21
9
|
const RESOURCE_MARKER = /\[\[resource:[^\]\s]+\]\]/gi;
|
|
22
|
-
|
|
23
10
|
/** Show attachment markers as a readable label and never leak the UUID. */
|
|
24
11
|
export function formatAttachments(text) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
12
|
+
const source = String(text ?? "");
|
|
13
|
+
const markers = source.match(RESOURCE_MARKER);
|
|
14
|
+
if (!markers)
|
|
15
|
+
return source;
|
|
16
|
+
const clean = source
|
|
17
|
+
.replace(RESOURCE_MARKER, " ")
|
|
18
|
+
.replace(/[ \t]+\n/g, "\n")
|
|
19
|
+
.replace(/\n[ \t]+/g, "\n")
|
|
20
|
+
.replace(/[ \t]{2,}/g, " ")
|
|
21
|
+
.trim();
|
|
22
|
+
if (!clean)
|
|
23
|
+
return t("markdown.attachments");
|
|
24
|
+
return `${clean} · ${t("markdown.attachments")}`;
|
|
36
25
|
}
|
|
37
|
-
|
|
38
26
|
function closingBackticks(source, from, end, length) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
27
|
+
let i = from;
|
|
28
|
+
while (i < end) {
|
|
29
|
+
if (source.charAt(i) !== "`") {
|
|
30
|
+
i += 1;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
let run = 0;
|
|
34
|
+
while (i + run < end && source.charAt(i + run) === "`")
|
|
35
|
+
run += 1;
|
|
36
|
+
if (run === length)
|
|
37
|
+
return i;
|
|
38
|
+
i += run;
|
|
44
39
|
}
|
|
45
|
-
|
|
46
|
-
while (i + run < end && source[i + run] === "`") run += 1;
|
|
47
|
-
if (run === length) return i;
|
|
48
|
-
i += run;
|
|
49
|
-
}
|
|
50
|
-
return -1;
|
|
40
|
+
return -1;
|
|
51
41
|
}
|
|
52
|
-
|
|
53
42
|
function inlineMentions(line) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
43
|
+
let out = "";
|
|
44
|
+
let index = 0;
|
|
45
|
+
while (index < line.length) {
|
|
46
|
+
if (line.charAt(index) === "`") {
|
|
47
|
+
let run = 0;
|
|
48
|
+
while (line.charAt(index + run) === "`")
|
|
49
|
+
run += 1;
|
|
50
|
+
const close = closingBackticks(line, index + run, line.length, run);
|
|
51
|
+
if (close === -1) {
|
|
52
|
+
out += line.slice(index);
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
out += line.slice(index, close + run);
|
|
56
|
+
index = close + run;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const match = line.slice(index).match(MENTION_TAG);
|
|
60
|
+
if (match) {
|
|
61
|
+
out += `\`${MENTION_SENTINEL}${match[1] ?? ""}\``;
|
|
62
|
+
index += (match[0] ?? "").length;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
out += line.charAt(index);
|
|
66
|
+
index += 1;
|
|
74
67
|
}
|
|
75
|
-
out
|
|
76
|
-
index += 1;
|
|
77
|
-
}
|
|
78
|
-
return out;
|
|
68
|
+
return out;
|
|
79
69
|
}
|
|
80
|
-
|
|
81
70
|
/** Rewrite <at>@name</at> in prose, leaving fenced and inline code untouched. */
|
|
82
71
|
export function prepareMarkdown(text) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (!line.trim() || /^(?: {4,}|\t)/.test(line)) return line;
|
|
98
|
-
indented = false;
|
|
99
|
-
}
|
|
100
|
-
if (marker) {
|
|
101
|
-
fence = { char: marker[2][0], length: marker[2].length, indent: marker[1].length };
|
|
102
|
-
return line;
|
|
103
|
-
}
|
|
104
|
-
if (/^(?: {4,}|\t)/.test(line)) {
|
|
105
|
-
indented = true;
|
|
106
|
-
return line;
|
|
107
|
-
}
|
|
108
|
-
return inlineMentions(line);
|
|
109
|
-
})
|
|
110
|
-
.join("\n");
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function isMentionCode(token) {
|
|
114
|
-
return token?.type === "codespan" && String(token.text ?? "").startsWith(MENTION_SENTINEL);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export function containsMention(token) {
|
|
118
|
-
if (!token) return false;
|
|
119
|
-
if (isMentionCode(token)) return true;
|
|
120
|
-
if (Array.isArray(token.tokens) && token.tokens.some(containsMention)) return true;
|
|
121
|
-
return typeof token.raw === "string" && token.raw.includes(MENTION_SENTINEL);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
function chunk(text, color, style = {}) {
|
|
125
|
-
let value = fg(color)(text);
|
|
126
|
-
if (style.bold) value = bold(value);
|
|
127
|
-
if (style.italic) value = italic(value);
|
|
128
|
-
if (style.underline) value = underline(value);
|
|
129
|
-
if (style.strikethrough) value = strikethrough(value);
|
|
130
|
-
if (style.bg) value = bg(style.bg)(value);
|
|
131
|
-
if (style.dim) value = dim(value);
|
|
132
|
-
return value;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function inlineTokens(tokens, theme, style = {}) {
|
|
136
|
-
const chunks = [];
|
|
137
|
-
for (const token of tokens ?? []) {
|
|
138
|
-
switch (token.type) {
|
|
139
|
-
case "text":
|
|
140
|
-
case "escape":
|
|
141
|
-
if (Array.isArray(token.tokens) && token.tokens.length) {
|
|
142
|
-
chunks.push(...inlineTokens(token.tokens, theme, style));
|
|
143
|
-
} else {
|
|
144
|
-
chunks.push(chunk(token.text ?? "", style.color ?? theme.text, style));
|
|
72
|
+
let fence = null;
|
|
73
|
+
let indented = false;
|
|
74
|
+
return String(text ?? "")
|
|
75
|
+
.split("\n")
|
|
76
|
+
.map((line) => {
|
|
77
|
+
const clean = line.replace(/\r$/, "");
|
|
78
|
+
const marker = clean.match(/^([ \t]*)(`{3,}|~{3,})/);
|
|
79
|
+
const closing = clean.match(/^([ \t]*)(`{3,}|~{3,})[ \t]*$/);
|
|
80
|
+
if (fence) {
|
|
81
|
+
const closeFence = closing?.[2] ?? "";
|
|
82
|
+
if (closing && closeFence.charAt(0) === fence.char && closeFence.length >= fence.length
|
|
83
|
+
&& (closing[1] ?? "").length <= fence.indent + 3)
|
|
84
|
+
fence = null;
|
|
85
|
+
return line;
|
|
145
86
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
case "em":
|
|
151
|
-
chunks.push(...inlineTokens(token.tokens, theme, { ...style, italic: true }));
|
|
152
|
-
break;
|
|
153
|
-
case "del":
|
|
154
|
-
chunks.push(...inlineTokens(token.tokens, theme, { ...style, strikethrough: true }));
|
|
155
|
-
break;
|
|
156
|
-
case "codespan":
|
|
157
|
-
if (isMentionCode(token)) {
|
|
158
|
-
chunks.push(chunk(String(token.text).slice(MENTION_SENTINEL.length), theme.accent, { bold: true }));
|
|
159
|
-
} else {
|
|
160
|
-
chunks.push(chunk(token.text ?? "", theme.text, { ...style, bg: theme.raised }));
|
|
87
|
+
if (indented) {
|
|
88
|
+
if (!line.trim() || /^(?: {4,}|\t)/.test(line))
|
|
89
|
+
return line;
|
|
90
|
+
indented = false;
|
|
161
91
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
color: theme.accent,
|
|
167
|
-
underline: true,
|
|
168
|
-
}));
|
|
169
|
-
if (token.href) chunks.push(chunk(` (${token.href})`, theme.muted, style));
|
|
170
|
-
break;
|
|
171
|
-
}
|
|
172
|
-
case "image":
|
|
173
|
-
chunks.push(chunk(token.text || "image", theme.muted, style));
|
|
174
|
-
break;
|
|
175
|
-
case "br":
|
|
176
|
-
chunks.push(chunk("\n", theme.text, style));
|
|
177
|
-
break;
|
|
178
|
-
case "list":
|
|
179
|
-
chunks.push(...flattenList(token, theme));
|
|
180
|
-
break;
|
|
181
|
-
case "table":
|
|
182
|
-
chunks.push(...flattenTable(token, theme));
|
|
183
|
-
break;
|
|
184
|
-
case "blockquote":
|
|
185
|
-
chunks.push(chunk("│ ", theme.accent), ...inlineTokens(token.tokens, theme, { ...style, color: theme.muted }));
|
|
186
|
-
break;
|
|
187
|
-
default:
|
|
188
|
-
if (Array.isArray(token.tokens) && token.tokens.length) {
|
|
189
|
-
chunks.push(...inlineTokens(token.tokens, theme, style));
|
|
190
|
-
} else if (typeof token.text === "string" || typeof token.raw === "string") {
|
|
191
|
-
chunks.push(chunk(token.text ?? token.raw, style.color ?? theme.text, style));
|
|
92
|
+
if (marker) {
|
|
93
|
+
const fenceText = marker[2] ?? "";
|
|
94
|
+
fence = { char: fenceText.charAt(0), length: fenceText.length, indent: (marker[1] ?? "").length };
|
|
95
|
+
return line;
|
|
192
96
|
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
const items = Array.isArray(token.items) ? token.items : [];
|
|
201
|
-
for (const [index, item] of items.entries()) {
|
|
202
|
-
const marker = token.ordered ? `${(token.start ?? 1) + index}. ` : "• ";
|
|
203
|
-
if (item.task) chunks.push(chunk(item.checked ? "[x] " : "[ ] ", theme.muted));
|
|
204
|
-
chunks.push(chunk(marker, theme.accent), ...inlineTokens(item.tokens, theme));
|
|
205
|
-
chunks.push(chunk("\n", theme.text));
|
|
206
|
-
}
|
|
207
|
-
return chunks;
|
|
97
|
+
if (/^(?: {4,}|\t)/.test(line)) {
|
|
98
|
+
indented = true;
|
|
99
|
+
return line;
|
|
100
|
+
}
|
|
101
|
+
return inlineMentions(line);
|
|
102
|
+
})
|
|
103
|
+
.join("\n");
|
|
208
104
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Recolors the private mention marker inside a prose code span: strip the
|
|
107
|
+
* sentinel, drop the code background and draw it in the accent color.
|
|
108
|
+
*/
|
|
109
|
+
export function createMentionInlineHook(theme) {
|
|
110
|
+
const accent = RGBA.fromHex(theme.accent);
|
|
111
|
+
return (spans) => spans.flatMap((span) => {
|
|
112
|
+
if (!span.text.includes(MENTION_SENTINEL))
|
|
113
|
+
return [span];
|
|
114
|
+
const parts = span.text.split(MENTION_SENTINEL);
|
|
115
|
+
return parts.flatMap((text, index) => {
|
|
116
|
+
if (!text)
|
|
117
|
+
return [];
|
|
118
|
+
if (!index)
|
|
119
|
+
return [{ ...span, text }];
|
|
120
|
+
return [{ ...span, text, code: false, fg: accent, bg: undefined, attributes: TextAttributes.BOLD }];
|
|
121
|
+
});
|
|
219
122
|
});
|
|
220
|
-
chunks.push(chunk("\n", theme.text));
|
|
221
|
-
}
|
|
222
|
-
return chunks;
|
|
223
123
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
export function createMentionRenderNode(renderer, theme, baseID) {
|
|
253
|
-
let counter = 0;
|
|
254
|
-
return (token, context) => {
|
|
255
|
-
if (!containsMention(token)) return context.defaultRender();
|
|
256
|
-
const id = `${baseID}-mention-${counter}`;
|
|
257
|
-
counter += 1;
|
|
258
|
-
return new TextRenderable(renderer, {
|
|
259
|
-
id,
|
|
260
|
-
width: "100%",
|
|
261
|
-
flexShrink: 0,
|
|
262
|
-
wrapMode: "word",
|
|
263
|
-
content: new StyledText(mentionText(token, theme)),
|
|
264
|
-
});
|
|
265
|
-
};
|
|
124
|
+
export function createMarkdownSyntax(renderer, theme) {
|
|
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);
|
|
266
152
|
}
|
|
267
|
-
|
|
268
|
-
export function createMarkdownSyntax(theme) {
|
|
269
|
-
return SyntaxStyle.fromStyles({
|
|
270
|
-
default: { fg: theme.text },
|
|
271
|
-
conceal: { fg: theme.faint },
|
|
272
|
-
"markup.heading": { fg: theme.accent, bold: true },
|
|
273
|
-
"markup.strong": { fg: theme.text, bold: true },
|
|
274
|
-
"markup.italic": { fg: theme.text, italic: true },
|
|
275
|
-
"markup.strikethrough": { fg: theme.muted },
|
|
276
|
-
"markup.raw": { fg: theme.accent, bg: theme.raised, bold: true },
|
|
277
|
-
"markup.list": { fg: theme.accent },
|
|
278
|
-
"markup.quote": { fg: theme.muted, italic: true },
|
|
279
|
-
"markup.link": { fg: theme.muted },
|
|
280
|
-
"markup.link.label": { fg: theme.accent, underline: true },
|
|
281
|
-
"markup.link.url": { fg: theme.muted },
|
|
282
|
-
keyword: { fg: theme.accent, bold: true },
|
|
283
|
-
string: { fg: theme.success },
|
|
284
|
-
comment: { fg: theme.muted, italic: true },
|
|
285
|
-
number: { fg: theme.warning },
|
|
286
|
-
constant: { fg: theme.warning },
|
|
287
|
-
function: { fg: theme.text, bold: true },
|
|
288
|
-
type: { fg: theme.warning },
|
|
289
|
-
variable: { fg: theme.text },
|
|
290
|
-
property: { fg: theme.text },
|
|
291
|
-
operator: { fg: theme.muted },
|
|
292
|
-
punctuation: { fg: theme.muted },
|
|
293
|
-
tag: { fg: theme.accent },
|
|
294
|
-
attribute: { fg: theme.warning },
|
|
295
|
-
});
|
|
296
|
-
}
|