@jackwener/opencli 1.7.2 → 1.7.3

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 (75) hide show
  1. package/README.md +4 -1
  2. package/README.zh-CN.md +5 -2
  3. package/cli-manifest.json +658 -31
  4. package/clis/barchart/flow.js +1 -1
  5. package/clis/barchart/greeks.js +2 -2
  6. package/clis/barchart/options.js +2 -2
  7. package/clis/barchart/quote.js +1 -1
  8. package/clis/bilibili/feed.js +202 -48
  9. package/clis/boss/utils.js +2 -1
  10. package/clis/chatgpt/image.js +97 -0
  11. package/clis/chatgpt/utils.js +297 -0
  12. package/clis/{chatgpt → chatgpt-app}/ask.js +1 -1
  13. package/clis/{chatgpt → chatgpt-app}/model.js +1 -1
  14. package/clis/{chatgpt → chatgpt-app}/new.js +1 -1
  15. package/clis/{chatgpt → chatgpt-app}/read.js +1 -1
  16. package/clis/{chatgpt → chatgpt-app}/send.js +1 -1
  17. package/clis/{chatgpt → chatgpt-app}/status.js +1 -1
  18. package/clis/discord-app/delete.js +114 -0
  19. package/clis/douban/utils.js +29 -2
  20. package/clis/douban/utils.test.js +121 -1
  21. package/clis/ke/chengjiao.js +77 -0
  22. package/clis/ke/ershoufang.js +100 -0
  23. package/clis/ke/utils.js +104 -0
  24. package/clis/ke/xiaoqu.js +77 -0
  25. package/clis/ke/zufang.js +94 -0
  26. package/clis/maimai/search-talents.js +172 -0
  27. package/clis/mubu/doc.js +40 -0
  28. package/clis/mubu/docs.js +43 -0
  29. package/clis/mubu/notes.js +244 -0
  30. package/clis/mubu/recent.js +27 -0
  31. package/clis/mubu/search.js +62 -0
  32. package/clis/mubu/utils.js +304 -0
  33. package/clis/reuters/search.js +1 -1
  34. package/clis/xiaohongshu/comments.js +18 -6
  35. package/clis/xiaohongshu/comments.test.js +36 -0
  36. package/clis/xiaohongshu/creator-note-detail.js +2 -0
  37. package/clis/xiaohongshu/creator-note-detail.test.js +32 -0
  38. package/clis/xiaohongshu/creator-notes-summary.js +4 -0
  39. package/clis/xiaohongshu/creator-notes-summary.test.js +39 -1
  40. package/clis/xiaohongshu/creator-notes.js +1 -0
  41. package/clis/xiaohongshu/creator-profile.js +1 -0
  42. package/clis/xiaohongshu/creator-stats.js +1 -0
  43. package/clis/xiaohongshu/download.js +12 -0
  44. package/clis/xiaohongshu/download.test.js +30 -0
  45. package/clis/xiaohongshu/navigation.test.js +34 -0
  46. package/clis/xiaohongshu/note.js +14 -5
  47. package/clis/xiaohongshu/note.test.js +28 -0
  48. package/clis/xiaohongshu/publish.js +1 -0
  49. package/clis/xiaohongshu/search.js +1 -0
  50. package/clis/xiaohongshu/user.js +1 -0
  51. package/clis/yahoo-finance/quote.js +1 -1
  52. package/dist/src/browser/base-page.d.ts +9 -0
  53. package/dist/src/browser/base-page.js +19 -0
  54. package/dist/src/browser/cdp.js +10 -2
  55. package/dist/src/browser/daemon-client.d.ts +1 -0
  56. package/dist/src/cli.js +4 -2
  57. package/dist/src/daemon.js +5 -0
  58. package/dist/src/doctor.d.ts +1 -0
  59. package/dist/src/doctor.js +51 -2
  60. package/dist/src/electron-apps.js +1 -1
  61. package/dist/src/errors.d.ts +1 -0
  62. package/dist/src/errors.js +13 -0
  63. package/dist/src/execution.js +36 -9
  64. package/dist/src/execution.test.js +23 -0
  65. package/dist/src/logger.d.ts +2 -2
  66. package/dist/src/logger.js +4 -9
  67. package/dist/src/registry.js +3 -4
  68. package/dist/src/types.d.ts +2 -0
  69. package/dist/src/update-check.d.ts +14 -0
  70. package/dist/src/update-check.js +48 -3
  71. package/dist/src/update-check.test.d.ts +1 -0
  72. package/dist/src/update-check.test.js +31 -0
  73. package/package.json +1 -1
  74. package/scripts/fetch-adapters.js +35 -8
  75. /package/clis/{chatgpt → chatgpt-app}/ax.js +0 -0
@@ -0,0 +1,43 @@
1
+ import { cli, Strategy } from '@jackwener/opencli/registry';
2
+ import { formatDate, mubuPost } from './utils.js';
3
+
4
+ cli({
5
+ site: 'mubu',
6
+ name: 'docs',
7
+ description: '列出幕布文档(默认根目录,--starred 查看快速访问列表)',
8
+ domain: 'mubu.com',
9
+ strategy: Strategy.COOKIE,
10
+ args: [
11
+ { name: 'folder', default: '0', help: '文件夹 ID(默认根目录 0)' },
12
+ { name: 'starred', type: 'bool', default: false, help: '只显示快速访问的文档和文件夹' },
13
+ { name: 'limit', type: 'int', default: 50, help: '最多显示条数' },
14
+ ],
15
+ columns: ['type', 'id', 'name', 'updated', 'stared'],
16
+ func: async (page, kwargs) => {
17
+ const folderId = kwargs.folder;
18
+ const starred = kwargs.starred;
19
+ const limit = kwargs.limit;
20
+
21
+ await page.goto('https://mubu.com/app');
22
+ const body = starred ? { source: 'star' } : { folderId };
23
+ const data = await mubuPost(page, '/list/get', body);
24
+
25
+ const folders = (data.folders ?? []).map((f) => ({
26
+ type: '📁',
27
+ id: f.id,
28
+ name: f.name,
29
+ updated: formatDate(f.updateTime),
30
+ stared: f.stared ? '★' : '',
31
+ }));
32
+
33
+ const docs = (data.documents ?? []).map((doc) => ({
34
+ type: '📄',
35
+ id: doc.id,
36
+ name: doc.name,
37
+ updated: formatDate(doc.updateTime),
38
+ stared: doc.stared ? '★' : '',
39
+ }));
40
+
41
+ return [...folders, ...docs].slice(0, limit);
42
+ },
43
+ });
@@ -0,0 +1,244 @@
1
+ import { cli, Strategy } from '@jackwener/opencli/registry';
2
+ import { ArgumentError } from '@jackwener/opencli/errors';
3
+ import { mubuPost, nodesToMarkdown, nodesToText, htmlToText } from './utils.js';
4
+
5
+ // ── 日期工具 ──────────────────────────────────────────────
6
+
7
+ function localToday() {
8
+ const d = new Date();
9
+ return { year: d.getFullYear(), month: d.getMonth() + 1, day: d.getDate() };
10
+ }
11
+
12
+ function lastDayOfMonth(year, month) {
13
+ return new Date(year, month, 0).getDate();
14
+ }
15
+
16
+ function validateYear(year, label = '年份') {
17
+ if (!Number.isInteger(year) || year < 1) {
18
+ throw new ArgumentError(`${label} 非法:${year},应为正整数`);
19
+ }
20
+ }
21
+
22
+ function validateMonth(month) {
23
+ if (!Number.isInteger(month) || month < 1 || month > 12) {
24
+ throw new ArgumentError(`月份非法:${month},应为 1-12`);
25
+ }
26
+ }
27
+
28
+ function validateDay(year, month, day) {
29
+ const maxDay = lastDayOfMonth(year, month);
30
+ if (!Number.isInteger(day) || day < 1 || day > maxDay) {
31
+ throw new ArgumentError(`日期非法:${year}-${month}-${day}(${year} 年 ${month} 月共 ${maxDay} 天)`);
32
+ }
33
+ }
34
+
35
+ function parseDate(s) {
36
+ const parts = s.split('-').map(Number);
37
+ if (parts.length !== 3 || parts.some(isNaN)) {
38
+ throw new ArgumentError(`日期格式错误:${s},应为 YYYY-MM-DD`);
39
+ }
40
+ const [year, month, day] = parts;
41
+ validateYear(year);
42
+ validateMonth(month);
43
+ validateDay(year, month, day);
44
+ return { year, month, day };
45
+ }
46
+
47
+ function parseMonth(s) {
48
+ const parts = s.split('-').map(Number);
49
+ if (parts.length !== 2 || parts.some(isNaN)) {
50
+ throw new ArgumentError(`月份格式错误:${s},应为 YYYY-MM`);
51
+ }
52
+ const [year, month] = parts;
53
+ validateYear(year);
54
+ validateMonth(month);
55
+ return { year, month };
56
+ }
57
+
58
+ function dateToKey(d) {
59
+ return `${d.year}-${String(d.month).padStart(2, '0')}-${String(d.day).padStart(2, '0')}`;
60
+ }
61
+
62
+ /** 将各种时间参数统一解析为 {start, end} */
63
+ function resolveRange(kwargs) {
64
+ const dateStr = kwargs.date;
65
+ const monthStr = kwargs.month;
66
+ const yearArg = kwargs.year;
67
+ const fromStr = kwargs.from;
68
+ const toStr = kwargs.to;
69
+
70
+ // --from / --to 优先级最高
71
+ if (fromStr || toStr) {
72
+ if (!fromStr) throw new ArgumentError('使用 --to 时必须同时指定 --from');
73
+ const start = parseDate(fromStr);
74
+ const end = toStr ? parseDate(toStr) : localToday();
75
+ if (dateToKey(start) > dateToKey(end)) throw new ArgumentError('--from 不能晚于 --to');
76
+ return { start, end };
77
+ }
78
+
79
+ if (yearArg !== undefined && yearArg !== null) {
80
+ validateYear(yearArg, '--year');
81
+ return {
82
+ start: { year: yearArg, month: 1, day: 1 },
83
+ end: { year: yearArg, month: 12, day: 31 },
84
+ };
85
+ }
86
+
87
+ if (monthStr) {
88
+ const { year, month } = parseMonth(monthStr);
89
+ return {
90
+ start: { year, month, day: 1 },
91
+ end: { year, month, day: lastDayOfMonth(year, month) },
92
+ };
93
+ }
94
+
95
+ if (dateStr) {
96
+ const d = parseDate(dateStr);
97
+ return { start: d, end: d };
98
+ }
99
+
100
+ // 默认:今天
101
+ const today = localToday();
102
+ return { start: today, end: today };
103
+ }
104
+
105
+ // ── API 工具 ──────────────────────────────────────────────
106
+
107
+ async function getYearDocId(page, year) {
108
+ const raw = await page.evaluate(`localStorage.getItem('daily_notes_doc_list')`);
109
+ if (!raw) return null;
110
+ const list = JSON.parse(raw);
111
+ return list.find((d) => d.name === `${year}年`)?.id ?? null;
112
+ }
113
+
114
+ async function getYearNodes(page, docId) {
115
+ const data = await mubuPost(page, '/document/edit/get', { docId });
116
+ const def = JSON.parse(data.definition);
117
+ return def.nodes ?? [];
118
+ }
119
+
120
+ /** 加载某年的所有 day 节点,返回带 dateKey 的列表 */
121
+ async function loadYearEntries(page, year) {
122
+ const docId = await getYearDocId(page, year);
123
+ if (!docId) return [];
124
+
125
+ const yearNodes = await getYearNodes(page, docId);
126
+ const entries = [];
127
+
128
+ for (const monthNode of yearNodes) {
129
+ const monthNum = parseInt(htmlToText(monthNode.text), 10);
130
+ if (!monthNode.children?.length) continue;
131
+
132
+ for (const dayNode of monthNode.children) {
133
+ const plain = htmlToText(dayNode.text).replace(/\s+/g, ' ').trim();
134
+ const compact = plain.replace(/\s/g, '');
135
+ const match = compact.match(/^(\d+)月(\d+)日/);
136
+ if (!match) continue;
137
+ const m = parseInt(match[1], 10);
138
+ const d = parseInt(match[2], 10);
139
+ if (m !== monthNum) continue;
140
+
141
+ const dateKey = dateToKey({ year, month: m, day: d });
142
+ entries.push({ dateKey, label: plain, node: dayNode });
143
+ }
144
+ }
145
+
146
+ return entries;
147
+ }
148
+
149
+ /** 收集 [start, end] 范围内涉及的所有年份 */
150
+ function yearsInRange(start, end) {
151
+ const years = [];
152
+ for (let y = start.year; y <= end.year; y++) years.push(y);
153
+ return years;
154
+ }
155
+
156
+ // ── 命令 ──────────────────────────────────────────────────
157
+
158
+ cli({
159
+ site: 'mubu',
160
+ name: 'notes',
161
+ description: '读取幕布速记(默认今天)。支持 --date/--month/--year/--from/--to 指定时间范围,--list 为概览模式(日期+条数)。',
162
+ domain: 'mubu.com',
163
+ strategy: Strategy.COOKIE,
164
+ args: [
165
+ {
166
+ name: 'list',
167
+ type: 'bool',
168
+ default: false,
169
+ help: '概览模式:只输出日期和条数,不含速记内容。可与任意时间范围参数组合。',
170
+ },
171
+ {
172
+ name: 'date',
173
+ help: '单日,格式 YYYY-MM-DD。不指定时间范围则默认今天(系统本地时间)。',
174
+ },
175
+ {
176
+ name: 'month',
177
+ help: '整月,格式 YYYY-MM。',
178
+ },
179
+ {
180
+ name: 'year',
181
+ type: 'int',
182
+ help: '整年,格式 YYYY(整数)。',
183
+ },
184
+ {
185
+ name: 'from',
186
+ help: '范围起始日,格式 YYYY-MM-DD。须与 --to 同时使用。',
187
+ },
188
+ {
189
+ name: 'to',
190
+ help: '范围截止日,格式 YYYY-MM-DD。须与 --from 同时使用。',
191
+ },
192
+ {
193
+ name: 'output',
194
+ default: 'md',
195
+ help: '输出格式:md(默认,Markdown)或 text(纯文本)',
196
+ },
197
+ ],
198
+ columns: ['date', 'content'],
199
+ func: async (page, kwargs) => {
200
+ const isList = kwargs.list;
201
+ const format = kwargs.output;
202
+ if (format !== 'md' && format !== 'text') {
203
+ throw new ArgumentError(`--output 只接受 md 或 text,收到:${format}`);
204
+ }
205
+
206
+ await page.goto('https://mubu.com/app');
207
+
208
+ const { start, end } = resolveRange(kwargs);
209
+ const startKey = dateToKey(start);
210
+ const endKey = dateToKey(end);
211
+
212
+ // 并行加载所有涉及年份的 day 节点,按范围过滤
213
+ const yearResults = await Promise.all(
214
+ yearsInRange(start, end).map((year) => loadYearEntries(page, year)),
215
+ );
216
+ const allEntries = yearResults
217
+ .flat()
218
+ .filter((e) => e.dateKey >= startKey && e.dateKey <= endKey);
219
+
220
+ if (allEntries.length === 0) {
221
+ const label = startKey === endKey ? startKey : `${startKey} ~ ${endKey}`;
222
+ return [{ date: label, content: '该时间段暂无速记' }];
223
+ }
224
+
225
+ // 概览模式
226
+ if (isList) {
227
+ return allEntries.map((e) => ({
228
+ date: e.label,
229
+ content: `${e.node.children?.length ?? 0} 条记录`,
230
+ }));
231
+ }
232
+
233
+ // 内容模式
234
+ const render = (children) =>
235
+ format === 'text' ? nodesToText(children) : nodesToMarkdown(children);
236
+
237
+ return allEntries
238
+ .filter((e) => e.node.children?.length)
239
+ .map((e) => ({
240
+ date: e.label,
241
+ content: render(e.node.children ?? []) || '(空)',
242
+ }));
243
+ },
244
+ });
@@ -0,0 +1,27 @@
1
+ import { cli, Strategy } from '@jackwener/opencli/registry';
2
+ import { formatDate, mubuPost } from './utils.js';
3
+
4
+ cli({
5
+ site: 'mubu',
6
+ name: 'recent',
7
+ description: '最近编辑的幕布文档',
8
+ domain: 'mubu.com',
9
+ strategy: Strategy.COOKIE,
10
+ args: [
11
+ { name: 'limit', type: 'int', default: 20, help: '最多显示条数' },
12
+ ],
13
+ columns: ['id', 'name', 'updated'],
14
+ func: async (page, kwargs) => {
15
+ const limit = kwargs.limit;
16
+
17
+ await page.goto('https://mubu.com/app');
18
+
19
+ const data = await mubuPost(page, '/list/get', { folderId: 'recent' });
20
+
21
+ return (data.documents ?? []).slice(0, limit).map((doc) => ({
22
+ id: doc.id,
23
+ name: doc.name,
24
+ updated: formatDate(doc.updateTime),
25
+ }));
26
+ },
27
+ });
@@ -0,0 +1,62 @@
1
+ import { cli, Strategy } from '@jackwener/opencli/registry';
2
+ import { mubuPost, htmlToText } from './utils.js';
3
+
4
+ cli({
5
+ site: 'mubu',
6
+ name: 'search',
7
+ description: '全局搜索幕布文档和文件夹(标题+内容,服务端全量匹配)。结果含 type/id/name/path/hits/snippet 字段。',
8
+ domain: 'mubu.com',
9
+ strategy: Strategy.COOKIE,
10
+ args: [
11
+ { name: 'query', positional: true, required: true, help: '搜索关键词' },
12
+ { name: 'limit', type: 'int', default: 100, help: '最多显示条数(默认 100,结果被截断时用 --limit N 调大)' },
13
+ ],
14
+ columns: ['type', 'id', 'name', 'path', 'hits', 'snippet'],
15
+ func: async (page, kwargs) => {
16
+ const query = kwargs.query;
17
+ const limit = kwargs.limit ?? 100;
18
+
19
+ await page.goto('https://mubu.com/app');
20
+
21
+ const data = await mubuPost(page, '/list/search', { keywords: query });
22
+
23
+ const formatPath = (paths) => paths.map((p) => p.name).join(' > ');
24
+
25
+ const folders = (data.folders ?? []).map((f) => ({
26
+ type: 'folder',
27
+ id: f.id,
28
+ name: f.name,
29
+ path: formatPath(f.paths),
30
+ hits: '',
31
+ snippet: '',
32
+ }));
33
+
34
+ const docs = (data.documents ?? []).map((d) => ({
35
+ type: 'doc',
36
+ id: d.id,
37
+ name: d.name,
38
+ path: formatPath(d.paths),
39
+ hits: d.total > 0 ? String(d.total) : '',
40
+ snippet: d.nodes
41
+ .map((n) => htmlToText(n.text))
42
+ .filter(Boolean)
43
+ .join(' | '),
44
+ }));
45
+
46
+ const all = [...folders, ...docs];
47
+ const result = all.slice(0, limit);
48
+
49
+ if (all.length > limit) {
50
+ result.push({
51
+ type: '...',
52
+ id: '',
53
+ name: `还有 ${all.length - limit} 条未显示,用 --limit ${all.length} 查看全部`,
54
+ path: '',
55
+ hits: '',
56
+ snippet: '',
57
+ });
58
+ }
59
+
60
+ return result;
61
+ },
62
+ });
@@ -0,0 +1,304 @@
1
+ import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
2
+
3
+ export const API_BASE = 'https://api2.mubu.com/v3/api';
4
+ const MUBU_DOMAIN = 'mubu.com';
5
+ const AUTH_HINT = 'Mubu requires a logged-in browser session at mubu.com';
6
+
7
+ function isAuthFailure(code, message) {
8
+ if (code === 401 || code === 403) return true;
9
+ if (!message) return false;
10
+ return /not logged in|login required|unauthorized|未登录|请先登录|需要登录|login expired/i.test(message);
11
+ }
12
+
13
+ /**
14
+ * 在浏览器页面上下文里用 XHR 发 POST 请求(参考 zsxq 适配器模式)。
15
+ * mubu app 自身也是这个机制:从 localStorage 读 Jwt-Token,通过同名 header 发到 api2.mubu.com。
16
+ * 不经过 Node.js 进程发网络请求,避免 CORS 问题和 extension fetch 拦截。
17
+ */
18
+ export async function mubuPost(page, path, body) {
19
+ const url = `${API_BASE}${path}`;
20
+
21
+ const result = await page.evaluate(`
22
+ (async () => {
23
+ const token = localStorage.getItem('Jwt-Token');
24
+ if (!token) return { ok: false, status: 0, data: null, error: 'no token' };
25
+ return await new Promise((resolve) => {
26
+ const xhr = new XMLHttpRequest();
27
+ xhr.open('POST', ${JSON.stringify(url)}, true);
28
+ xhr.setRequestHeader('Content-Type', 'application/json');
29
+ xhr.setRequestHeader('Jwt-Token', token);
30
+ xhr.onload = () => {
31
+ let data = null;
32
+ try { data = JSON.parse(xhr.responseText); } catch {}
33
+ resolve({ ok: xhr.status >= 200 && xhr.status < 300, status: xhr.status, data });
34
+ };
35
+ xhr.onerror = () => resolve({ ok: false, status: 0, data: null, error: 'network error' });
36
+ xhr.send(${JSON.stringify(JSON.stringify(body))});
37
+ });
38
+ })()
39
+ `);
40
+
41
+ if (!result || result.error === 'no token') {
42
+ throw new AuthRequiredError(MUBU_DOMAIN, AUTH_HINT);
43
+ }
44
+ if (!result.ok || !result.data) {
45
+ throw new CommandExecutionError(`mubu: ${path}: HTTP ${result.status} ${result.error ?? ''}`);
46
+ }
47
+
48
+ const { data } = result;
49
+ if (data.code !== 0) {
50
+ if (isAuthFailure(data.code, data.message)) {
51
+ throw new AuthRequiredError(MUBU_DOMAIN, AUTH_HINT);
52
+ }
53
+ throw new CommandExecutionError(`mubu: ${path}: code=${data.code} ${data.message ?? ''}`);
54
+ }
55
+
56
+ return data.data;
57
+ }
58
+
59
+ export function formatDate(ts) {
60
+ if (!ts) return '';
61
+ const d = new Date(ts);
62
+ const pad = (n) => String(n).padStart(2, '0');
63
+ return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
64
+ }
65
+
66
+ const NAMED_ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'", nbsp: ' ' };
67
+
68
+ function decodeHtmlEntities(s) {
69
+ return s
70
+ .replace(/&#x([0-9a-f]+);/gi, (_, h) => String.fromCodePoint(parseInt(h, 16)))
71
+ .replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(parseInt(n, 10)))
72
+ .replace(/&(amp|lt|gt|quot|apos|nbsp);/g, (_, n) => NAMED_ENTITIES[n]);
73
+ }
74
+
75
+ /** 解析幕布 HTML 表格为行列二维数组(保留内部 HTML) */
76
+ function parseTableRows(tableHtml) {
77
+ const rows = [];
78
+ const rowMatches = tableHtml.match(/<tr[^>]*>[\s\S]*?<\/tr>/gi) ?? [];
79
+ for (const row of rowMatches) {
80
+ const cells = [];
81
+ const cellMatches = row.match(/<(?:td|th)[^>]*>[\s\S]*?<\/(?:td|th)>/gi) ?? [];
82
+ for (const cell of cellMatches) {
83
+ // 只剥离最外层的 <td> 或 <th>,保留内部的加粗、链接和 <br>
84
+ let innerHtml = cell.replace(/^<(?:td|th)[^>]*>|<\/(?:td|th)>$/gi, '');
85
+ cells.push(innerHtml.trim());
86
+ }
87
+ rows.push(cells);
88
+ }
89
+ return rows;
90
+ }
91
+
92
+ /** 将幕布 HTML text 转为纯文本 */
93
+ export function htmlToText(html) {
94
+ let text = html;
95
+ // 表格 → 纯文本(tab 分隔);用 </table>\s*</div> 作结束锚,跳过 th/td 内部嵌套 div
96
+ text = text.replace(/<div class="table-container">[\s\S]*?<\/table>\s*<\/div>/g, (m) => {
97
+ return parseTableRows(m).map((r) => r.map(c => {
98
+ // 纯文本环境:<br> 换空格,清空所有标签
99
+ let plainCell = c.replace(/<br\s*\/?>/gi, ' ').replace(/<[^>]+>/g, '');
100
+ return decodeHtmlEntities(plainCell).trim();
101
+ }).join('\t')).join('\n');
102
+ });
103
+ text = text
104
+ .replace(/<br\s*\/?>/gi, '\n')
105
+ .replace(/<[^>]+>/g, '');
106
+ return decodeHtmlEntities(text).trim();
107
+ }
108
+
109
+ /** 将幕布 HTML 表格转为 Markdown 表格 */
110
+ function tableToMarkdown(tableHtml) {
111
+ const rows = parseTableRows(tableHtml);
112
+ if (rows.length === 0) return '';
113
+
114
+ const processRow = (row) => row.map(cellHtml => {
115
+ // 把表格内的 <br> 换成占位符,防止稍后被全局替换为 \n 导致表格断裂
116
+ return cellHtml.replace(/<br\s*\/?>/gi, '[[BR]]');
117
+ }).join(' | ');
118
+
119
+ const lines = [`| ${processRow(rows[0])} |`, `| ${rows[0].map(() => '---').join(' | ')} |`];
120
+ for (let i = 1; i < rows.length; i++) {
121
+ lines.push(`| ${processRow(rows[i])} |`);
122
+ }
123
+ return lines.join('\n');
124
+ }
125
+
126
+ /** 将幕布 HTML text 转为 Markdown inline 标记 */
127
+ export function htmlToMarkdown(html) {
128
+ let md = html;
129
+
130
+ // 1. 表格 → Markdown 表格
131
+ md = md.replace(/<div class="table-container">[\s\S]*?<\/table>\s*<\/div>/g, (m) => tableToMarkdown(m));
132
+
133
+ // 2. <br> → 换行
134
+ md = md.replace(/<br\s*\/?>/gi, '\n');
135
+
136
+ // 3. 统一处理样式标签,支持多 class 组合
137
+ md = md.replace(/<span class="([^"]+)"[^>]*>([\s\S]*?)<\/span>/gi, (match, classes, inner) => {
138
+ // 允许正常处理超链接内部的 bold 和 italic 样式
139
+ if (classes.includes('node-mention')) {
140
+ return match;
141
+ }
142
+ let res = inner;
143
+ if (/\bbold\b/.test(classes)) res = `**${res}**`;
144
+ if (/\bitalic\b/.test(classes)) res = `*${res}*`;
145
+ if (/\bstrikethrough\b/.test(classes)) res = `~~${res}~~`;
146
+ if (/\bunderline\b/.test(classes)) res = `\uFFFEU_OPEN\uFFFE${res}\uFFFEU_CLOSE\uFFFE`;
147
+ return res;
148
+ });
149
+
150
+ // 4. node-mention(主题链接 → Markdown 链接,支持组合 class 并继承自身样式)
151
+ md = md.replace(
152
+ /<span([^>]*\bclass="[^"]*\bnode-mention\b[^"]*"[^>]*)>([\s\S]*?)<\/span>/gi,
153
+ (match, attrs, inner) => {
154
+ const docMatch = attrs.match(/\bdata-doc="([^"]+)"/i);
155
+ const docId = docMatch ? docMatch[1] : '';
156
+ if (!docId) return match;
157
+
158
+ const classMatch = attrs.match(/\bclass="([^"]+)"/i);
159
+ const classes = classMatch ? classMatch[1] : '';
160
+
161
+ let res = inner.replace(/<[^>]+>/g, '').trim();
162
+
163
+ // 继承标签自身的样式
164
+ if (/\bbold\b/.test(classes)) res = `**${res}**`;
165
+ if (/\bitalic\b/.test(classes)) res = `*${res}*`;
166
+ if (/\bstrikethrough\b/.test(classes)) res = `~~${res}~~`;
167
+ if (/\bunderline\b/.test(classes)) res = `\uFFFEU_OPEN\uFFFE${res}\uFFFEU_CLOSE\uFFFE`;
168
+
169
+ return `[${res}](https://mubu.com/app/edit/${docId})`;
170
+ }
171
+ );
172
+
173
+ // 5. links (外部链接 → Markdown 链接,继承 a 标签自身样式)
174
+ md = md.replace(/<a([^>]*)>([\s\S]*?)<\/a>/gi, (match, attrs, inner) => {
175
+ const hrefMatch = attrs.match(/\bhref="([^"]+)"/i);
176
+ if (!hrefMatch) return match;
177
+ const href = hrefMatch[1];
178
+
179
+ const classMatch = attrs.match(/\bclass="([^"]+)"/i);
180
+ const classes = classMatch ? classMatch[1] : '';
181
+
182
+ let res = inner;
183
+
184
+ // 继承 a 标签自身的样式
185
+ if (/\bbold\b/.test(classes)) res = `**${res}**`;
186
+ if (/\bitalic\b/.test(classes)) res = `*${res}*`;
187
+ if (/\bstrikethrough\b/.test(classes)) res = `~~${res}~~`;
188
+ if (/\bunderline\b/.test(classes)) res = `\uFFFEU_OPEN\uFFFE${res}\uFFFEU_CLOSE\uFFFE`;
189
+
190
+ return `[${res}](${href})`;
191
+ });
192
+
193
+ // 6. 普通 span
194
+ md = md.replace(/<span[^>]*>([\s\S]*?)<\/span>/gi, '$1');
195
+
196
+ // 7. 清理其余标签
197
+ md = md.replace(/<[^>]+>/g, '');
198
+
199
+ // 8. HTML 实体解码
200
+ md = decodeHtmlEntities(md);
201
+
202
+ // 9. 还原 underline 占位符
203
+ md = md.replace(/\uFFFEU_OPEN\uFFFE/g, '<u>').replace(/\uFFFEU_CLOSE\uFFFE/g, '</u>');
204
+
205
+ // 10. 还原表格内的换行符
206
+ md = md.replace(/\[\[BR\]\]/g, '<br>');
207
+
208
+ return md.trim();
209
+ }
210
+
211
+ const IMAGE_BASE = 'https://api2.mubu.com/v3';
212
+
213
+ function imageUrl(uri) {
214
+ return uri.startsWith('http') ? uri : `${IMAGE_BASE}/${uri}`;
215
+ }
216
+
217
+ function taskPrefix(node) {
218
+ if (!node.taskStatus) return '';
219
+ return node.taskStatus === 2 ? '[x] ' : '[ ] ';
220
+ }
221
+
222
+ function taskMeta(node) {
223
+ const parts = [];
224
+ if (node.deadline) {
225
+ const ts = formatDate(node.deadline * 1000);
226
+ parts.push(`📅 ${node.deadlineType === 'date' ? ts.slice(0, 10) : ts}`);
227
+ }
228
+ if (node.remindAt) parts.push(`⏰ ${formatDate(node.remindAt * 1000)}`);
229
+ return parts.length ? ' ' + parts.join(' ') : '';
230
+ }
231
+
232
+ /** 递归将节点树渲染为缩进纯文本 */
233
+ export function nodesToText(nodes, depth = 0) {
234
+ const lines = [];
235
+ for (const node of nodes) {
236
+ const indent = ' '.repeat(depth);
237
+ const emoji = node.emoji ? node.emoji + ' ' : '';
238
+ const text = htmlToText(node.text);
239
+ const prefix = taskPrefix(node);
240
+ const meta = taskMeta(node);
241
+ if (text || emoji || prefix) {
242
+ if (text.includes('\n')) {
243
+ const [first, ...rest] = text.split('\n');
244
+ lines.push(indent + prefix + emoji + first);
245
+ for (const line of rest) lines.push(indent + ' ' + line);
246
+ if (meta) lines.push(indent + ' ' + meta.trim());
247
+ } else {
248
+ lines.push(indent + prefix + emoji + text + meta);
249
+ }
250
+ }
251
+ if (node.note) {
252
+ const noteText = htmlToText(node.note);
253
+ for (const line of noteText.split('\n')) lines.push(indent + ' ' + line);
254
+ }
255
+ if (node.images?.length) {
256
+ for (const img of node.images) {
257
+ lines.push(indent + `[图片: ${imageUrl(img.uri)}]`);
258
+ }
259
+ }
260
+ if (node.children?.length) {
261
+ lines.push(nodesToText(node.children, depth + 1));
262
+ }
263
+ }
264
+ return lines.filter(Boolean).join('\n');
265
+ }
266
+
267
+ /** 递归将节点树渲染为 Markdown(大纲 = 缩进列表,不映射为标题) */
268
+ export function nodesToMarkdown(nodes, depth = 0) {
269
+ const lines = [];
270
+ for (const node of nodes) {
271
+ const text = htmlToMarkdown(node.text);
272
+ if (!text && !node.images?.length && !node.note && !node.emoji) continue;
273
+
274
+ const indent = ' '.repeat(depth);
275
+ const emoji = node.emoji ? node.emoji + ' ' : '';
276
+ const prefix = taskPrefix(node);
277
+ const meta = taskMeta(node);
278
+ if (text || emoji || prefix) {
279
+ if (text.includes('\n')) {
280
+ const [first, ...rest] = text.split('\n');
281
+ lines.push(indent + '- ' + prefix + emoji + first);
282
+ const continuation = indent + ' ';
283
+ for (const line of rest) lines.push(continuation + line);
284
+ if (meta) lines.push(continuation + meta.trim());
285
+ } else {
286
+ lines.push(indent + '- ' + prefix + emoji + text + meta);
287
+ }
288
+ }
289
+ if (node.note) {
290
+ const noteLines = htmlToMarkdown(node.note).split('\n');
291
+ for (const line of noteLines) lines.push(indent + ' > ' + line);
292
+ }
293
+ if (node.images?.length) {
294
+ for (const img of node.images) {
295
+ lines.push(indent + ` ![image](${imageUrl(img.uri)})`);
296
+ }
297
+ }
298
+
299
+ if (node.children?.length) {
300
+ lines.push(nodesToMarkdown(node.children, depth + 1));
301
+ }
302
+ }
303
+ return lines.filter(Boolean).join('\n');
304
+ }
@@ -21,7 +21,7 @@ cli({
21
21
  (async () => {
22
22
  const count = ${count};
23
23
  const apiQuery = JSON.stringify({
24
- keyword: '${kwargs.query.replace(/'/g, "\\'")}',
24
+ keyword: ${JSON.stringify(kwargs.query)},
25
25
  offset: 0, orderby: 'display_date:desc', size: count, website: 'reuters'
26
26
  });
27
27
  const apiUrl = 'https://www.reuters.com/pf/api/v3/content/fetch/articles-by-search-v2?query=' + encodeURIComponent(apiQuery);