@nbtca/prompt 1.5.12 → 1.5.14
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/dist/app/tabs.js +1 -1
- package/dist/app/views/docs-render.js +7 -5
- package/dist/app/views/docs.js +16 -4
- package/dist/app/views/events.js +3 -3
- package/dist/app/views/home.js +8 -19
- package/dist/app/views/schedule-grid-cursor.js +7 -2
- package/dist/app/views/schedule.js +6 -6
- package/dist/app/views/settings.js +21 -36
- package/dist/auth/cookie-transport.js +1 -0
- package/dist/auth/login-page.js +141 -0
- package/dist/auth/nbt-auth.js +7 -12
- package/dist/core/text.js +14 -12
- package/dist/core/theme.js +2 -0
- package/dist/features/docs-client.js +7 -2
- package/dist/features/docs-markdown.js +265 -0
- package/dist/features/docs-store.js +37 -27
- package/dist/features/docs.js +31 -132
- package/dist/features/schedule-render.js +5 -3
- package/dist/features/schedule-view.js +1 -1
- package/dist/i18n/locales/en.json +7 -4
- package/dist/i18n/locales/zh.json +7 -4
- package/package.json +3 -5
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { c, glyph, type } from '../core/theme.js';
|
|
3
|
+
import { pickIcon } from '../core/icons.js';
|
|
4
|
+
import { padEndV, sanitizeTerminalText, stripAnsi, visualWidth, wrapAnsiToVisualWidth, } from '../core/text.js';
|
|
5
|
+
export function isInternalHref(href) {
|
|
6
|
+
return /^\.{0,2}\/./.test(href);
|
|
7
|
+
}
|
|
8
|
+
const ENTITIES = {
|
|
9
|
+
amp: '&',
|
|
10
|
+
lt: '<',
|
|
11
|
+
gt: '>',
|
|
12
|
+
quot: '"',
|
|
13
|
+
apos: "'",
|
|
14
|
+
nbsp: ' ',
|
|
15
|
+
};
|
|
16
|
+
function decodeEntities(text) {
|
|
17
|
+
return text.replace(/&(?:#(\d{1,7})|#x([\da-f]{1,6})|([a-z]+));/gi, (match, dec, hex, name) => {
|
|
18
|
+
if (name)
|
|
19
|
+
return ENTITIES[name.toLowerCase()] ?? match;
|
|
20
|
+
const codePoint = dec ? Number(dec) : parseInt(hex ?? '', 16);
|
|
21
|
+
return codePoint <= 0x10ffff ? sanitizeTerminalText(String.fromCodePoint(codePoint)) : match;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
const CJK = '[\\p{Script=Han}\\p{Script=Hiragana}\\p{Script=Katakana}\\p{Script=Hangul}\\u3000-\\u303f\\uff00-\\uffef]';
|
|
25
|
+
const CJK_SOFT_BREAK = new RegExp(`(?<=${CJK})[ \\t]*\\n[ \\t]*(?=${CJK})`, 'gu');
|
|
26
|
+
function joinSoftBreaks(text) {
|
|
27
|
+
return text.replace(CJK_SOFT_BREAK, '').replace(/[ \t]*\n[ \t]*/g, ' ');
|
|
28
|
+
}
|
|
29
|
+
function renderInline(tokens, hrefs) {
|
|
30
|
+
return (tokens ?? []).map((token) => renderInlineToken(token, hrefs)).join('');
|
|
31
|
+
}
|
|
32
|
+
function renderLink(token, hrefs) {
|
|
33
|
+
const text = renderInline(token.tokens, hrefs);
|
|
34
|
+
const bare = (href) => href.replace(/^(?:https?:\/\/|mailto:)/, '').replace(/\/$/, '');
|
|
35
|
+
const selfLabelled = bare(stripAnsi(text)) === bare(token.href);
|
|
36
|
+
if (isInternalHref(token.href) || token.href.startsWith('#') || selfLabelled) {
|
|
37
|
+
return chalk.underline(c.accent(text));
|
|
38
|
+
}
|
|
39
|
+
return hrefs ? `${c.accent(text)} ${c.url(`(${token.href})`)}` : c.accent(text);
|
|
40
|
+
}
|
|
41
|
+
function renderInlineToken(token, hrefs) {
|
|
42
|
+
switch (token.type) {
|
|
43
|
+
case 'text':
|
|
44
|
+
return token.tokens
|
|
45
|
+
? renderInline(token.tokens, hrefs)
|
|
46
|
+
: joinSoftBreaks(decodeEntities(token.text));
|
|
47
|
+
case 'escape':
|
|
48
|
+
return token.text;
|
|
49
|
+
case 'strong':
|
|
50
|
+
return chalk.bold(renderInline(token.tokens, hrefs));
|
|
51
|
+
case 'em':
|
|
52
|
+
return chalk.italic(renderInline(token.tokens, hrefs));
|
|
53
|
+
case 'del':
|
|
54
|
+
return chalk.strikethrough(c.muted(renderInline(token.tokens, hrefs)));
|
|
55
|
+
case 'codespan':
|
|
56
|
+
return c.code(token.text);
|
|
57
|
+
case 'br':
|
|
58
|
+
return '\n';
|
|
59
|
+
case 'link':
|
|
60
|
+
return renderLink(token, hrefs);
|
|
61
|
+
case 'image':
|
|
62
|
+
return `${pickIcon('🖼️', '[image]')} ${token.text || 'image'}`;
|
|
63
|
+
case 'html':
|
|
64
|
+
case 'blockquote':
|
|
65
|
+
case 'code':
|
|
66
|
+
case 'def':
|
|
67
|
+
case 'heading':
|
|
68
|
+
case 'hr':
|
|
69
|
+
case 'list':
|
|
70
|
+
case 'list_item':
|
|
71
|
+
case 'paragraph':
|
|
72
|
+
case 'space':
|
|
73
|
+
case 'table':
|
|
74
|
+
return '';
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function wrap(text, width) {
|
|
78
|
+
return text.split('\n').flatMap((line) => wrapAnsiToVisualWidth(line, width));
|
|
79
|
+
}
|
|
80
|
+
function hang(lines, first, rest) {
|
|
81
|
+
return lines.map((line, index) => {
|
|
82
|
+
if (index === 0)
|
|
83
|
+
return first + line;
|
|
84
|
+
return line ? rest + line : '';
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function styleHeading(text, depth) {
|
|
88
|
+
if (depth === 1)
|
|
89
|
+
return chalk.bold(c.brand(text));
|
|
90
|
+
if (depth === 2)
|
|
91
|
+
return type.heading(text);
|
|
92
|
+
if (depth === 3)
|
|
93
|
+
return c.label(text);
|
|
94
|
+
return chalk.bold(text);
|
|
95
|
+
}
|
|
96
|
+
function renderHeading(token, width) {
|
|
97
|
+
const lines = wrap(styleHeading(renderInline(token.tokens, false), token.depth), width);
|
|
98
|
+
if (token.depth !== 2)
|
|
99
|
+
return lines;
|
|
100
|
+
const underline = Math.min(width, Math.max(...lines.map(visualWidth)));
|
|
101
|
+
return [...lines, c.muted(glyph.rule().repeat(underline))];
|
|
102
|
+
}
|
|
103
|
+
function renderCode(token, width) {
|
|
104
|
+
const gutter = c.muted(glyph.bar());
|
|
105
|
+
const body = token.text
|
|
106
|
+
.replace(/\t/g, ' ')
|
|
107
|
+
.split('\n')
|
|
108
|
+
.flatMap((line) => wrapAnsiToVisualWidth(line, Math.max(1, width - 2)))
|
|
109
|
+
.map((line) => (line ? `${gutter} ${c.code(line)}` : gutter));
|
|
110
|
+
const lang = token.lang?.split(/\s/, 1)[0];
|
|
111
|
+
return lang ? [c.muted(lang), ...body] : body;
|
|
112
|
+
}
|
|
113
|
+
function renderBlockquote(token, width, depth) {
|
|
114
|
+
const bar = c.accent(glyph.bar());
|
|
115
|
+
return renderBlocks(token.tokens, Math.max(1, width - 2), depth, false).map((line) => line ? `${bar} ${line}` : bar);
|
|
116
|
+
}
|
|
117
|
+
const BULLETS = ['•', '◦', '▪'];
|
|
118
|
+
function renderList(token, width, depth) {
|
|
119
|
+
const start = token.start === '' ? 1 : token.start;
|
|
120
|
+
const bullet = pickIcon(BULLETS[depth % BULLETS.length] ?? '•', '-');
|
|
121
|
+
const numbers = token.items.map((_, index) => `${start + index}.`);
|
|
122
|
+
const numberWidth = Math.max(...numbers.map((label) => label.length));
|
|
123
|
+
const lines = [];
|
|
124
|
+
token.items.forEach((item, index) => {
|
|
125
|
+
const marker = token.ordered ? (numbers[index] ?? '').padStart(numberWidth) : bullet;
|
|
126
|
+
const box = item.task
|
|
127
|
+
? item.checked
|
|
128
|
+
? c.success(pickIcon('☑', '[x]'))
|
|
129
|
+
: c.muted(pickIcon('☐', '[ ]'))
|
|
130
|
+
: '';
|
|
131
|
+
const first = `${c.accent(marker)} ${box ? `${box} ` : ''}`;
|
|
132
|
+
const indent = ' '.repeat(visualWidth(first));
|
|
133
|
+
const body = renderBlocks(item.tokens, Math.max(1, width - indent.length), depth + 1, !token.loose);
|
|
134
|
+
if (token.loose && index > 0)
|
|
135
|
+
lines.push('');
|
|
136
|
+
lines.push(...hang(body.length > 0 ? body : [''], first, indent));
|
|
137
|
+
});
|
|
138
|
+
return lines;
|
|
139
|
+
}
|
|
140
|
+
const MIN_COLUMN = 6;
|
|
141
|
+
function alignCell(text, width, align) {
|
|
142
|
+
const pad = Math.max(0, width - visualWidth(text));
|
|
143
|
+
if (align === 'right')
|
|
144
|
+
return ' '.repeat(pad) + text;
|
|
145
|
+
if (align === 'center') {
|
|
146
|
+
const left = Math.floor(pad / 2);
|
|
147
|
+
return ' '.repeat(left) + text + ' '.repeat(pad - left);
|
|
148
|
+
}
|
|
149
|
+
return text + ' '.repeat(pad);
|
|
150
|
+
}
|
|
151
|
+
function cellWidth(text) {
|
|
152
|
+
return Math.max(0, ...text.split('\n').map(visualWidth));
|
|
153
|
+
}
|
|
154
|
+
function renderRecords(header, rows, width) {
|
|
155
|
+
const labelWidth = Math.min(Math.max(...header.map(visualWidth)), Math.max(1, Math.floor(width / 3)));
|
|
156
|
+
const valueWidth = Math.max(1, width - labelWidth - 2);
|
|
157
|
+
const lines = [];
|
|
158
|
+
rows.forEach((row, rowIndex) => {
|
|
159
|
+
if (rowIndex > 0)
|
|
160
|
+
lines.push(c.muted(glyph.rule().repeat(width)));
|
|
161
|
+
header.forEach((label, column) => {
|
|
162
|
+
const value = row[column] ?? '';
|
|
163
|
+
if (!stripAnsi(value).trim())
|
|
164
|
+
return;
|
|
165
|
+
const labels = wrapAnsiToVisualWidth(label, labelWidth);
|
|
166
|
+
const values = wrap(value, valueWidth);
|
|
167
|
+
for (let line = 0; line < Math.max(labels.length, values.length); line += 1) {
|
|
168
|
+
const text = values[line] ?? '';
|
|
169
|
+
lines.push(`${c.muted(padEndV(labels[line] ?? '', labelWidth))}${text ? ` ${text}` : ''}`);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
return lines;
|
|
174
|
+
}
|
|
175
|
+
function renderTable(token, width) {
|
|
176
|
+
const header = token.header.map((cell) => renderInline(cell.tokens, false));
|
|
177
|
+
const rows = token.rows.map((row) => row.map((cell) => renderInline(cell.tokens, false)));
|
|
178
|
+
const natural = header.map((label, column) => Math.max(cellWidth(label), ...rows.map((row) => cellWidth(row[column] ?? ''))));
|
|
179
|
+
const available = width - (3 * header.length + 1);
|
|
180
|
+
const floor = natural.map((columnWidth) => Math.min(columnWidth, MIN_COLUMN));
|
|
181
|
+
if (available < floor.reduce((sum, value) => sum + value, 0)) {
|
|
182
|
+
return renderRecords(header, rows, width);
|
|
183
|
+
}
|
|
184
|
+
const widths = [...natural];
|
|
185
|
+
const total = widths.reduce((sum, value) => sum + value, 0);
|
|
186
|
+
for (let excess = total - available; excess > 0; excess -= 1) {
|
|
187
|
+
const slack = widths.map((value, column) => value - (floor[column] ?? 0));
|
|
188
|
+
const widest = slack.indexOf(Math.max(...slack));
|
|
189
|
+
widths[widest] = (widths[widest] ?? 0) - 1;
|
|
190
|
+
}
|
|
191
|
+
const corners = pickIcon('┌┬┐├┼┤└┴┘', '+++++++++');
|
|
192
|
+
const border = (row) => c.muted(corners.charAt(row * 3) +
|
|
193
|
+
widths.map((w) => glyph.rule().repeat(w + 2)).join(corners.charAt(row * 3 + 1)) +
|
|
194
|
+
corners.charAt(row * 3 + 2));
|
|
195
|
+
const bar = c.muted(glyph.bar());
|
|
196
|
+
const renderRow = (cells) => {
|
|
197
|
+
const wrapped = widths.map((w, column) => wrap(cells[column] ?? '', Math.max(1, w)));
|
|
198
|
+
const height = Math.max(...wrapped.map((lines) => lines.length));
|
|
199
|
+
return Array.from({ length: height }, (_, line) => {
|
|
200
|
+
const parts = wrapped.map((lines, column) => alignCell(lines[line] ?? '', widths[column] ?? 0, token.align[column] ?? null));
|
|
201
|
+
return `${bar} ${parts.join(` ${bar} `)} ${bar}`;
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
const body = rows.map(renderRow);
|
|
205
|
+
const separated = body.some((lines) => lines.length > 1);
|
|
206
|
+
return [
|
|
207
|
+
border(0),
|
|
208
|
+
...renderRow(header.map((label) => chalk.bold(label))),
|
|
209
|
+
border(1),
|
|
210
|
+
...body.flatMap((lines, index) => (separated && index > 0 ? [border(1), ...lines] : lines)),
|
|
211
|
+
border(2),
|
|
212
|
+
];
|
|
213
|
+
}
|
|
214
|
+
function renderBlock(token, width, depth) {
|
|
215
|
+
switch (token.type) {
|
|
216
|
+
case 'heading':
|
|
217
|
+
return renderHeading(token, width);
|
|
218
|
+
case 'paragraph':
|
|
219
|
+
return wrap(renderInline(token.tokens, true), width);
|
|
220
|
+
case 'text':
|
|
221
|
+
return wrap(renderInlineToken(token, true), width);
|
|
222
|
+
case 'code':
|
|
223
|
+
return renderCode(token, width);
|
|
224
|
+
case 'blockquote':
|
|
225
|
+
return renderBlockquote(token, width, depth);
|
|
226
|
+
case 'list':
|
|
227
|
+
return renderList(token, width, depth);
|
|
228
|
+
case 'table':
|
|
229
|
+
return renderTable(token, width);
|
|
230
|
+
case 'hr':
|
|
231
|
+
return [c.muted(glyph.rule().repeat(width))];
|
|
232
|
+
case 'html': {
|
|
233
|
+
const text = decodeEntities(token.text.replace(/<[^>]*>/g, '')).trim();
|
|
234
|
+
return text ? wrap(text, width) : [];
|
|
235
|
+
}
|
|
236
|
+
case 'space':
|
|
237
|
+
case 'def':
|
|
238
|
+
case 'list_item':
|
|
239
|
+
return [];
|
|
240
|
+
case 'br':
|
|
241
|
+
case 'codespan':
|
|
242
|
+
case 'del':
|
|
243
|
+
case 'em':
|
|
244
|
+
case 'escape':
|
|
245
|
+
case 'image':
|
|
246
|
+
case 'link':
|
|
247
|
+
case 'strong':
|
|
248
|
+
return wrap(renderInlineToken(token, true), width);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
function renderBlocks(tokens, width, depth, tight) {
|
|
252
|
+
const lines = [];
|
|
253
|
+
for (const token of tokens) {
|
|
254
|
+
const block = renderBlock(token, width, depth);
|
|
255
|
+
if (block.length === 0)
|
|
256
|
+
continue;
|
|
257
|
+
if (lines.length > 0 && !tight)
|
|
258
|
+
lines.push('');
|
|
259
|
+
lines.push(...block);
|
|
260
|
+
}
|
|
261
|
+
return lines;
|
|
262
|
+
}
|
|
263
|
+
export function renderMarkdown(tokens, width) {
|
|
264
|
+
return renderBlocks(tokens, Math.max(1, Math.floor(width)), 0, false);
|
|
265
|
+
}
|
|
@@ -1,34 +1,44 @@
|
|
|
1
1
|
import fs from 'fs';
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { getStateDir, getWritableStateDir } from '../config/paths.js';
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
const DIR = 'docs';
|
|
5
|
+
const MAX_BYTES = 4 * 1024 * 1024;
|
|
6
|
+
const KEY_RE = /^[\w-]{1,80}$/;
|
|
7
|
+
function keyFile(dir, key) {
|
|
8
|
+
if (!KEY_RE.test(key))
|
|
9
|
+
throw new TypeError('Invalid docs cache key.');
|
|
10
|
+
return path.join(dir, key);
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
12
|
+
function evict(dir, maxBytes) {
|
|
13
|
+
const files = fs
|
|
14
|
+
.readdirSync(dir)
|
|
15
|
+
.map((name) => {
|
|
16
|
+
const file = path.join(dir, name);
|
|
17
|
+
const stat = fs.statSync(file);
|
|
18
|
+
return { file, size: stat.size, usedAt: stat.mtimeMs };
|
|
19
|
+
})
|
|
20
|
+
.sort((left, right) => right.usedAt - left.usedAt);
|
|
21
|
+
let total = 0;
|
|
22
|
+
for (const { file, size } of files) {
|
|
23
|
+
total += size;
|
|
24
|
+
if (total > maxBytes)
|
|
25
|
+
fs.rmSync(file, { force: true });
|
|
21
26
|
}
|
|
22
27
|
}
|
|
23
|
-
export function
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
export function createDocsStore(root, maxBytes = MAX_BYTES) {
|
|
29
|
+
return {
|
|
30
|
+
read(key) {
|
|
31
|
+
const file = keyFile(path.join(root ?? getStateDir(), DIR), key);
|
|
32
|
+
const value = fs.readFileSync(file, 'utf8');
|
|
33
|
+
const now = new Date();
|
|
34
|
+
fs.utimes(file, now, now, () => undefined);
|
|
35
|
+
return value;
|
|
36
|
+
},
|
|
37
|
+
write(key, value) {
|
|
38
|
+
const dir = path.join(root ?? getWritableStateDir(), DIR);
|
|
39
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
40
|
+
fs.writeFileSync(keyFile(dir, key), value, { encoding: 'utf8', mode: 0o600 });
|
|
41
|
+
evict(dir, maxBytes);
|
|
42
|
+
},
|
|
43
|
+
};
|
|
34
44
|
}
|
package/dist/features/docs.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { marked } from 'marked';
|
|
2
|
-
import { markedTerminal } from 'marked-terminal';
|
|
3
2
|
import chalk from 'chalk';
|
|
4
3
|
import { createHash } from 'node:crypto';
|
|
5
4
|
import { runMenu, menuFooter } from '../core/components/menu.js';
|
|
@@ -11,9 +10,9 @@ import { spawn, execFileSync } from 'child_process';
|
|
|
11
10
|
import { URLS } from '../config/data.js';
|
|
12
11
|
import { t, fmt, getCurrentLanguage } from '../i18n/index.js';
|
|
13
12
|
import { enterScreen, breadcrumb } from '../core/transitions.js';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import { clearDocsClients, runDocsClientOperation } from './docs-client.js';
|
|
13
|
+
import { sanitizeTerminalLine, sanitizeTerminalText, truncate } from '../core/text.js';
|
|
14
|
+
import { isInternalHref, renderMarkdown } from './docs-markdown.js';
|
|
15
|
+
import { clearDocsClients, peekDocs, runDocsClientOperation } from './docs-client.js';
|
|
17
16
|
import { launchBrowserUrl } from './links.js';
|
|
18
17
|
function detectTerminalType() {
|
|
19
18
|
const term = (process.env['TERM'] ?? '').toLowerCase();
|
|
@@ -55,93 +54,6 @@ function hasGlow() {
|
|
|
55
54
|
_hasGlow ??= commandExists('glow');
|
|
56
55
|
return _hasGlow;
|
|
57
56
|
}
|
|
58
|
-
function isInternalHref(href) {
|
|
59
|
-
return /^\.{0,2}\/./.test(href);
|
|
60
|
-
}
|
|
61
|
-
let _markedConfigured = false;
|
|
62
|
-
export function ensureMarkedConfigured() {
|
|
63
|
-
if (_markedConfigured)
|
|
64
|
-
return;
|
|
65
|
-
_markedConfigured = true;
|
|
66
|
-
const terminalType = getTerminalType();
|
|
67
|
-
const extension = markedTerminal(getRendererOptions(terminalType));
|
|
68
|
-
const renderer = extension.renderer ?? (extension.renderer = {});
|
|
69
|
-
const renderExternalLink = renderer.link;
|
|
70
|
-
if (renderExternalLink) {
|
|
71
|
-
renderer.link = function (token) {
|
|
72
|
-
if (isInternalHref(token.href))
|
|
73
|
-
return chalk.cyan.underline(token.text);
|
|
74
|
-
return renderExternalLink.call(this, token);
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
const renderPlainText = renderer.text;
|
|
78
|
-
if (renderPlainText) {
|
|
79
|
-
renderer.text = function (token) {
|
|
80
|
-
const withTokens = token;
|
|
81
|
-
if (Array.isArray(withTokens.tokens) && withTokens.tokens.length > 0) {
|
|
82
|
-
return this.parser.parseInline(withTokens.tokens);
|
|
83
|
-
}
|
|
84
|
-
return renderPlainText.call(this, token);
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
marked.use(extension);
|
|
88
|
-
}
|
|
89
|
-
function getRendererOptions(type) {
|
|
90
|
-
const width = 80;
|
|
91
|
-
const unicodeTableChars = {
|
|
92
|
-
top: '─',
|
|
93
|
-
'top-mid': '┬',
|
|
94
|
-
'top-left': '┌',
|
|
95
|
-
'top-right': '┐',
|
|
96
|
-
bottom: '─',
|
|
97
|
-
'bottom-mid': '┴',
|
|
98
|
-
'bottom-left': '└',
|
|
99
|
-
'bottom-right': '┘',
|
|
100
|
-
left: '│',
|
|
101
|
-
'left-mid': '├',
|
|
102
|
-
mid: '─',
|
|
103
|
-
'mid-mid': '┼',
|
|
104
|
-
right: '│',
|
|
105
|
-
'right-mid': '┤',
|
|
106
|
-
middle: '│',
|
|
107
|
-
};
|
|
108
|
-
const asciiTableChars = {
|
|
109
|
-
top: '-',
|
|
110
|
-
'top-mid': '+',
|
|
111
|
-
'top-left': '+',
|
|
112
|
-
'top-right': '+',
|
|
113
|
-
bottom: '-',
|
|
114
|
-
'bottom-mid': '+',
|
|
115
|
-
'bottom-left': '+',
|
|
116
|
-
'bottom-right': '+',
|
|
117
|
-
left: '|',
|
|
118
|
-
'left-mid': '+',
|
|
119
|
-
mid: '-',
|
|
120
|
-
'mid-mid': '+',
|
|
121
|
-
right: '|',
|
|
122
|
-
'right-mid': '+',
|
|
123
|
-
middle: '|',
|
|
124
|
-
};
|
|
125
|
-
return {
|
|
126
|
-
width,
|
|
127
|
-
emoji: true,
|
|
128
|
-
unescape: true,
|
|
129
|
-
showSectionPrefix: false,
|
|
130
|
-
firstHeading: chalk.bold.cyan,
|
|
131
|
-
heading: chalk.bold.white,
|
|
132
|
-
codespan: chalk.yellowBright,
|
|
133
|
-
code: chalk.yellow,
|
|
134
|
-
blockquote: chalk.italic.gray,
|
|
135
|
-
strong: chalk.bold,
|
|
136
|
-
em: chalk.italic,
|
|
137
|
-
del: chalk.dim.strikethrough,
|
|
138
|
-
link: chalk.cyan,
|
|
139
|
-
href: chalk.cyan.underline,
|
|
140
|
-
tableOptions: {
|
|
141
|
-
chars: type === 'basic' ? asciiTableChars : unicodeTableChars,
|
|
142
|
-
},
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
57
|
const RENDER_CACHE_TTL_MS = 10 * 60 * 1000;
|
|
146
58
|
const RENDER_CACHE_MAX = 50;
|
|
147
59
|
const renderCache = new Map();
|
|
@@ -229,15 +141,13 @@ function loadDocMetadata(path, signal) {
|
|
|
229
141
|
return metadata;
|
|
230
142
|
});
|
|
231
143
|
}
|
|
232
|
-
function
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
: line)
|
|
240
|
-
.join('\n');
|
|
144
|
+
function memoizedRender(tokens) {
|
|
145
|
+
let last = null;
|
|
146
|
+
return (width) => {
|
|
147
|
+
if (last?.width !== width)
|
|
148
|
+
last = { width, lines: renderMarkdown(tokens, width) };
|
|
149
|
+
return last.lines;
|
|
150
|
+
};
|
|
241
151
|
}
|
|
242
152
|
async function loadRenderedDoc(filePath, signal) {
|
|
243
153
|
const generation = cacheGeneration;
|
|
@@ -253,11 +163,10 @@ async function loadRenderedDoc(filePath, signal) {
|
|
|
253
163
|
const terminalType = getTerminalType();
|
|
254
164
|
const cleaned = cleanMarkdownContent(rawContent, terminalType);
|
|
255
165
|
const title = sanitizeTerminalLine(page.title) || cleanFileName(filePath.split('/').pop() ?? filePath);
|
|
256
|
-
const markedOutput = normalizeRenderedTasks(await marked(cleaned), terminalType);
|
|
257
166
|
const renderedDoc = {
|
|
258
167
|
fingerprint,
|
|
259
168
|
cleaned,
|
|
260
|
-
|
|
169
|
+
render: memoizedRender(marked.lexer(cleaned)),
|
|
261
170
|
title,
|
|
262
171
|
readTime: estimateReadTime(cleaned),
|
|
263
172
|
};
|
|
@@ -305,9 +214,7 @@ function processFencedCodeBlocks(content) {
|
|
|
305
214
|
result.push(`> ${icon} **${firstToken}** — _${trans.docs.mermaidHint}_`);
|
|
306
215
|
}
|
|
307
216
|
else {
|
|
308
|
-
|
|
309
|
-
result.push(`\`${blockLang}\``);
|
|
310
|
-
result.push(fence);
|
|
217
|
+
result.push(fence + blockLang);
|
|
311
218
|
result.push(...blockBody);
|
|
312
219
|
result.push(fence);
|
|
313
220
|
}
|
|
@@ -600,7 +507,6 @@ export function resolveInternalHref(href, fromPath) {
|
|
|
600
507
|
return target;
|
|
601
508
|
}
|
|
602
509
|
export async function loadDocForReader(filePath, signal) {
|
|
603
|
-
ensureMarkedConfigured();
|
|
604
510
|
const { renderedDoc } = await loadRenderedDoc(filePath, signal);
|
|
605
511
|
const seen = new Set();
|
|
606
512
|
const links = [];
|
|
@@ -614,7 +520,7 @@ export async function loadDocForReader(filePath, signal) {
|
|
|
614
520
|
return {
|
|
615
521
|
path: filePath,
|
|
616
522
|
title: renderedDoc.title,
|
|
617
|
-
|
|
523
|
+
render: renderedDoc.render,
|
|
618
524
|
links,
|
|
619
525
|
};
|
|
620
526
|
}
|
|
@@ -743,20 +649,11 @@ export async function fetchAllDocs(signal) {
|
|
|
743
649
|
return runDocsClientOperation(signal, (client) => client.listAll());
|
|
744
650
|
}
|
|
745
651
|
export async function fetchSections(signal) {
|
|
746
|
-
|
|
747
|
-
saveDocsIndex(docs);
|
|
748
|
-
return buildSections(docs);
|
|
652
|
+
return buildSections(await fetchAllDocs(signal));
|
|
749
653
|
}
|
|
750
654
|
export function peekSections() {
|
|
751
|
-
const docs =
|
|
752
|
-
|
|
753
|
-
return null;
|
|
754
|
-
try {
|
|
755
|
-
return buildSections(docs);
|
|
756
|
-
}
|
|
757
|
-
catch {
|
|
758
|
-
return null;
|
|
759
|
-
}
|
|
655
|
+
const docs = peekDocs();
|
|
656
|
+
return docs ? buildSections(docs) : null;
|
|
760
657
|
}
|
|
761
658
|
async function loadSections() {
|
|
762
659
|
const trans = t();
|
|
@@ -767,8 +664,14 @@ async function loadSections() {
|
|
|
767
664
|
return sections;
|
|
768
665
|
}
|
|
769
666
|
catch {
|
|
770
|
-
|
|
771
|
-
|
|
667
|
+
const cached = peekSections();
|
|
668
|
+
if (!cached) {
|
|
669
|
+
s.error(trans.docs.loadError);
|
|
670
|
+
return null;
|
|
671
|
+
}
|
|
672
|
+
s.stop();
|
|
673
|
+
warning(trans.docs.stale);
|
|
674
|
+
return cached;
|
|
772
675
|
}
|
|
773
676
|
}
|
|
774
677
|
function pipeToPager(command, args, content, plain = false) {
|
|
@@ -814,9 +717,12 @@ export async function displayWithGlow(cleanedMarkdown) {
|
|
|
814
717
|
const cols = String(Math.min(process.stdout.columns || 80, 80));
|
|
815
718
|
return pipeToPager('glow', ['--pager', '--width', cols, '-'], cleanedMarkdown);
|
|
816
719
|
}
|
|
817
|
-
async function displayWithLess(
|
|
720
|
+
async function displayWithLess(doc, filePath) {
|
|
818
721
|
const trans = t();
|
|
819
722
|
const cols = Math.min(process.stdout.columns || 80, 80);
|
|
723
|
+
const { title, readTime } = doc;
|
|
724
|
+
const toc = extractTOC(doc.cleaned);
|
|
725
|
+
const rendered = doc.render(cols).join('\n');
|
|
820
726
|
const rule = chalk.dim('─'.repeat(cols));
|
|
821
727
|
const tocBlock = toc.length >= 3
|
|
822
728
|
? [
|
|
@@ -934,20 +840,13 @@ async function showArchivedSection(files) {
|
|
|
934
840
|
}
|
|
935
841
|
async function viewMarkdownFile(filePath) {
|
|
936
842
|
const trans = t();
|
|
937
|
-
ensureMarkedConfigured();
|
|
938
843
|
const s = createSpinner(`${trans.docs.loadingFile}: ${filePath}`);
|
|
939
844
|
try {
|
|
940
845
|
const { rawContent, renderedDoc } = await loadRenderedDoc(filePath);
|
|
941
846
|
s.stop(`${chalk.bold(renderedDoc.title)} ${chalk.dim(renderedDoc.readTime)}`);
|
|
942
|
-
const
|
|
943
|
-
if (
|
|
944
|
-
|
|
945
|
-
await displayWithLess(renderedDoc.rendered, renderedDoc.title, filePath, renderedDoc.readTime, toc);
|
|
946
|
-
}
|
|
947
|
-
}
|
|
948
|
-
else {
|
|
949
|
-
await displayWithLess(renderedDoc.rendered, renderedDoc.title, filePath, renderedDoc.readTime, toc);
|
|
950
|
-
}
|
|
847
|
+
const shownWithGlow = chalk.level > 0 && hasGlow() && (await displayWithGlow(renderedDoc.cleaned));
|
|
848
|
+
if (!shownWithGlow)
|
|
849
|
+
await displayWithLess(renderedDoc, filePath);
|
|
951
850
|
const needsBrowser = hasMarkdownTable(rawContent) || hasMermaidBlock(rawContent);
|
|
952
851
|
const action = await runMenu({
|
|
953
852
|
title: trans.docs.chooseAction,
|
|
@@ -10,7 +10,7 @@ function span(m, periods) {
|
|
|
10
10
|
const e = periods.find((p) => p.period === m.endPeriod)?.end ?? '';
|
|
11
11
|
return e ? `${s}${pickIcon('–', '-')}${e}` : s;
|
|
12
12
|
}
|
|
13
|
-
export function renderNextClassBanner(next, now, cols = Number.POSITIVE_INFINITY) {
|
|
13
|
+
export function renderNextClassBanner(next, now, cols = Number.POSITIVE_INFINITY, labelled = true) {
|
|
14
14
|
const trans = t();
|
|
15
15
|
if (!next)
|
|
16
16
|
return '';
|
|
@@ -27,7 +27,9 @@ export function renderNextClassBanner(next, now, cols = Number.POSITIVE_INFINITY
|
|
|
27
27
|
const dot = pickIcon('·', '-');
|
|
28
28
|
const marker = type.active(glyph.cursor());
|
|
29
29
|
const separator = ` ${dot} `;
|
|
30
|
-
const detailedPrefix =
|
|
30
|
+
const detailedPrefix = labelled
|
|
31
|
+
? `${space.indent}${marker} ${type.label(trans.timetable.nextClass)}${separator}`
|
|
32
|
+
: `${space.indent}${marker} `;
|
|
31
33
|
const suffix = `${separator}${whenStyled}`;
|
|
32
34
|
const courseName = next.meeting.courseName;
|
|
33
35
|
const location = next.meeting.location ? `${separator}${next.meeting.location}` : '';
|
|
@@ -152,7 +154,7 @@ function renderTimeline(meetings, periods, now, isToday, alwaysShowLocation, cur
|
|
|
152
154
|
if (!last)
|
|
153
155
|
return lines.join('\n');
|
|
154
156
|
const lastEnd = periods.find((p) => p.period === last.endPeriod)?.end ?? '23:59';
|
|
155
|
-
const fullEnd = `${space.indent}
|
|
157
|
+
const fullEnd = `${space.indent} ${type.hint(lastEnd)} ${rule}${bottomConnector}${rule} ${type.hint(trans.timetable.timelineEnd)}`;
|
|
156
158
|
if (!Number.isFinite(cols) || visualWidth(fullEnd) <= cols) {
|
|
157
159
|
lines.push(fullEnd);
|
|
158
160
|
}
|
|
@@ -35,7 +35,7 @@ export function peekNextClassLine(now = new Date()) {
|
|
|
35
35
|
const schedule = createTimetableSchedule(cached.timetable, {
|
|
36
36
|
weekOneMonday: cached.weekOneMonday,
|
|
37
37
|
});
|
|
38
|
-
return renderNextClassBanner(schedule.next(now), now);
|
|
38
|
+
return renderNextClassBanner(schedule.next(now), now, Number.POSITIVE_INFINITY, false);
|
|
39
39
|
}
|
|
40
40
|
catch {
|
|
41
41
|
return '';
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"hint": "? keys"
|
|
29
29
|
},
|
|
30
30
|
"menu": {
|
|
31
|
+
"home": "Home",
|
|
31
32
|
"events": "Events",
|
|
32
33
|
"eventsDesc": "Upcoming activities",
|
|
33
34
|
"docs": "Docs",
|
|
@@ -83,7 +84,9 @@
|
|
|
83
84
|
"noPastEvents": "No past events in the last 30 days",
|
|
84
85
|
"viewPastDetail": "Select an event for details:",
|
|
85
86
|
"next": "Next",
|
|
86
|
-
"recentActivity": "
|
|
87
|
+
"recentActivity": "Coming up",
|
|
88
|
+
"browse": "Browse",
|
|
89
|
+
"next30Days": "Next 30 Days",
|
|
87
90
|
"inPrefix": "in",
|
|
88
91
|
"startingNow": "starting now",
|
|
89
92
|
"thisWeek": "This Week",
|
|
@@ -112,7 +115,7 @@
|
|
|
112
115
|
"chooseCategory": "Docs",
|
|
113
116
|
"refreshCache": "Refresh cache",
|
|
114
117
|
"cacheCleared": "Documentation cache cleared",
|
|
115
|
-
"
|
|
118
|
+
"stale": "offline, showing last known docs",
|
|
116
119
|
"currentDir": "Current directory",
|
|
117
120
|
"chooseDoc": "Choose a document or directory:",
|
|
118
121
|
"emptyDir": "This directory is empty",
|
|
@@ -286,7 +289,7 @@
|
|
|
286
289
|
"classDone": "Done",
|
|
287
290
|
"classLive": "In progress",
|
|
288
291
|
"minutesRemaining": "{minutes}m left",
|
|
289
|
-
"timelineEnd": "
|
|
292
|
+
"timelineEnd": "End of day",
|
|
290
293
|
"termNotStarted": "Term hasn't started yet",
|
|
291
294
|
"termStartsIn": "Classes begin {date} · {days} days to go",
|
|
292
295
|
"hubTermDensity": "Term density",
|
|
@@ -321,7 +324,7 @@
|
|
|
321
324
|
"updated": "Theme setting updated",
|
|
322
325
|
"updatedSessionOnly": "Theme setting updated for this session only (failed to save config)",
|
|
323
326
|
"reset": "Theme settings reset",
|
|
324
|
-
"resetLabel": "Reset",
|
|
327
|
+
"resetLabel": "Reset to defaults",
|
|
325
328
|
"resetSessionOnly": "Theme settings reset for this session only (failed to save config)",
|
|
326
329
|
"usage": "Usage: nbtca theme | nbtca theme icon <auto|ascii|unicode> | nbtca theme color <auto|on|off> | nbtca theme reset",
|
|
327
330
|
"invalidValue": "Invalid value. Use one of:"
|