@clawdao/limschain-agent 0.5.0
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/.env.example +41 -0
- package/CHANGELOG.md +223 -0
- package/Dockerfile +50 -0
- package/LICENSE +21 -0
- package/README.md +627 -0
- package/bin/lims-mcp.mjs +101 -0
- package/bin/lims.mjs +1639 -0
- package/docs/MARKETPLACE-GUIDELINES.md +99 -0
- package/docs/clawdao-mcp-listing.md +308 -0
- package/docs/clawdao-mcp-setup.md +294 -0
- package/docs/ddn-hub-postType-issue.md +54 -0
- package/docs/deploy-http.md +192 -0
- package/docs/install-mcp.md +268 -0
- package/docs/limschain-migration-and-readme-backfill.md +212 -0
- package/docs/marketplace-readmes/clawdao-video-factory.md +49 -0
- package/docs/node-free-install.md +196 -0
- package/docs/release-notes/v1.0.30-short.md +63 -0
- package/docs/site-design-studio-api.md +162 -0
- package/lib/articles.mjs +397 -0
- package/lib/client.mjs +588 -0
- package/lib/courses.mjs +169 -0
- package/lib/dao.mjs +195 -0
- package/lib/env.mjs +32 -0
- package/lib/feedback.mjs +68 -0
- package/lib/http-server.mjs +257 -0
- package/lib/marketplace-readme-path.mjs +17 -0
- package/lib/marketplace.mjs +273 -0
- package/lib/materials.mjs +260 -0
- package/lib/mcp-server.mjs +334 -0
- package/lib/site.mjs +471 -0
- package/lib/versions.mjs +904 -0
- package/mcp-listing.http.json +25 -0
- package/mcp-listing.json +34 -0
- package/package.json +77 -0
- package/scripts/build-binary.mjs +104 -0
- package/scripts/build-site-dashboard.mjs +52 -0
- package/scripts/collect-site-articles.mjs +432 -0
- package/scripts/export-ddn-env.mjs +107 -0
- package/scripts/fetch-article-stats.mjs +212 -0
- package/scripts/pack-and-verify.mjs +111 -0
- package/scripts/publisher.mjs +255 -0
- package/scripts/sync-marketplace-readme.mjs +380 -0
- package/scripts/tool-set.mjs +48 -0
- package/scripts/verify-binary.mjs +115 -0
- package/scripts/verify-clawdao-bridge.mjs +41 -0
- package/scripts/verify-e2e.mjs +176 -0
- package/scripts/verify-http-mcp.mjs +94 -0
- package/scripts/verify-listing-sync.mjs +90 -0
- package/scripts/verify-marketplace-readme.mjs +266 -0
- package/scripts/verify-mcp-handshake.mjs +98 -0
- package/scripts/verify-news-publish.mjs +257 -0
- package/scripts/verify-npx-install.mjs +171 -0
- package/scripts/verify-site-dashboard.mjs +87 -0
- package/scripts/verify-site-layout.mjs +194 -0
- package/tests/client-auth.test.mjs +159 -0
- package/tests/dao-profile.test.mjs +126 -0
- package/tests/marketplace-readme-path.test.mjs +16 -0
- package/tests/mcp-server.test.mjs +187 -0
- package/tests/member-submission.test.mjs +150 -0
- package/tests/official-listing.test.mjs +52 -0
- package/tests/smoke.mjs +262 -0
- package/tests/sync-marketplace-readme.test.mjs +50 -0
- package/tests/tool-set.test.mjs +54 -0
- package/tests/versions-sync-dryrun.test.mjs +315 -0
package/lib/site.mjs
ADDED
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 站点设计(Design Studio)操作模块 - 亿检链站点编辑器
|
|
3
|
+
* 提供页面草稿保存 / 发布 / 下线、站点设置、公开读取等能力
|
|
4
|
+
*
|
|
5
|
+
* 接口来源:前端 design-studio 编辑器 `/design-studio/site/:daoId/:slug`
|
|
6
|
+
* 管理端(需鉴权,DAO 作用域):
|
|
7
|
+
* GET /api/v1/daos/:daoId
|
|
8
|
+
* GET /api/v1/daos/:daoId/site/pages
|
|
9
|
+
* POST /api/v1/daos/:daoId/site/pages
|
|
10
|
+
* GET /api/v1/daos/:daoId/site/pages/:slug
|
|
11
|
+
* PUT /api/v1/daos/:daoId/site/pages/:slug/draft
|
|
12
|
+
* DELETE /api/v1/daos/:daoId/site/pages/:slug
|
|
13
|
+
* POST /api/v1/daos/:daoId/site/pages/:slug/publish
|
|
14
|
+
* POST /api/v1/daos/:daoId/site/pages/:slug/unpublish
|
|
15
|
+
* GET /api/v1/daos/:daoId/site/settings
|
|
16
|
+
* PUT /api/v1/daos/:daoId/site/settings
|
|
17
|
+
* POST /api/v1/daos/:daoId/site/publish (整站发布)
|
|
18
|
+
* 公开端(按站点域解析,无需鉴权):
|
|
19
|
+
* GET /api/v1/site/context
|
|
20
|
+
* GET /api/v1/site/pages/:slug
|
|
21
|
+
* GET /api/v1/site/settings
|
|
22
|
+
*
|
|
23
|
+
* 数据形态(schemaVersion=1):
|
|
24
|
+
* content = { blocks: [ { id, type, props: { id, ... } } ] }
|
|
25
|
+
* 保存草稿 body = { title, schemaVersion, content }
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { LimsClient } from './client.mjs';
|
|
29
|
+
|
|
30
|
+
/** 站点内容 schema 版本(与编辑器一致,勿随意改动) */
|
|
31
|
+
export const SITE_SCHEMA_VERSION = '1';
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 编辑器支持的区块类型(含中文标签,便于 CLI 展示)
|
|
35
|
+
* 来源:design-studio 区块库面板
|
|
36
|
+
*/
|
|
37
|
+
export const BLOCK_TYPES = {
|
|
38
|
+
SiteHeaderNav: '顶部导航',
|
|
39
|
+
AnnouncementBar: '公告条',
|
|
40
|
+
Hero: '首屏 Hero',
|
|
41
|
+
PageSectionTitle: '小节标题',
|
|
42
|
+
RichText: '富文本',
|
|
43
|
+
FeatureGrid: '能力网格',
|
|
44
|
+
SplitSection: '图文分栏',
|
|
45
|
+
StatsRow: '数据条',
|
|
46
|
+
MetricCards: '指标卡',
|
|
47
|
+
LogoCloud: 'Logo 墙',
|
|
48
|
+
Testimonials: '用户评价',
|
|
49
|
+
DataList: '内容列表(CMS)',
|
|
50
|
+
ArticleCards: '文章卡片',
|
|
51
|
+
Timeline: '时间线',
|
|
52
|
+
TeamGrid: '成员网格',
|
|
53
|
+
PricingCards: '定价卡',
|
|
54
|
+
ComparisonTable: '对比表',
|
|
55
|
+
ProcessSteps: '流程步骤',
|
|
56
|
+
Faq: '常见问题',
|
|
57
|
+
ImageGallery: '图片画廊',
|
|
58
|
+
VideoSection: '视频区块',
|
|
59
|
+
CtaBanner: '转化横幅',
|
|
60
|
+
ContactCards: '快速入口',
|
|
61
|
+
FooterLinks: '页脚导航',
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** 判断字符串是否非空(去空白) */
|
|
65
|
+
const isFilled = (value) => typeof value === 'string' && value.trim().length > 0;
|
|
66
|
+
|
|
67
|
+
/** 深拷贝(站点内容都是纯 JSON) */
|
|
68
|
+
const clone = (value) => (value === undefined ? undefined : JSON.parse(JSON.stringify(value)));
|
|
69
|
+
|
|
70
|
+
/** 生成区块 id:`Type-时间戳-序号`(与编辑器生成规则一致,仅用于稳定标识) */
|
|
71
|
+
export const makeBlockId = (type, index, at = Date.now()) => `${type}-${at}-${index}`;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 构造一个区块(自动补 props.id,编辑器要求 props.id 与 block.id 一致)
|
|
75
|
+
* @param {string} type 区块类型(见 BLOCK_TYPES)
|
|
76
|
+
* @param {object} props 区块属性
|
|
77
|
+
* @param {{id?: string, index?: number}} [meta]
|
|
78
|
+
*/
|
|
79
|
+
export const block = (type, props = {}, meta = {}) => {
|
|
80
|
+
if (!BLOCK_TYPES[type]) {
|
|
81
|
+
throw new Error(`未知区块类型:${type}\n可用:${Object.keys(BLOCK_TYPES).join(' / ')}`);
|
|
82
|
+
}
|
|
83
|
+
const index = meta.index ?? 1;
|
|
84
|
+
const id = meta.id || makeBlockId(type, index);
|
|
85
|
+
return { id, type, props: { id, ...props } };
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 递归替换所有字符串中的品牌/文案片段(用于批量品牌迁移)
|
|
90
|
+
* @param {any} value 任意 JSON 结构
|
|
91
|
+
* @param {Array<[string|RegExp, string]>} replacements 替换规则
|
|
92
|
+
* @returns {{value: any, hits: number}}
|
|
93
|
+
*/
|
|
94
|
+
export const replaceInStrings = (value, replacements) => {
|
|
95
|
+
let hits = 0;
|
|
96
|
+
const walk = (node) => {
|
|
97
|
+
if (typeof node === 'string') {
|
|
98
|
+
let next = node;
|
|
99
|
+
for (const [from, to] of replacements) {
|
|
100
|
+
const before = next;
|
|
101
|
+
next = typeof from === 'string' ? next.split(from).join(to) : next.replace(from, to);
|
|
102
|
+
if (next !== before) hits += 1;
|
|
103
|
+
}
|
|
104
|
+
return next;
|
|
105
|
+
}
|
|
106
|
+
if (Array.isArray(node)) return node.map(walk);
|
|
107
|
+
if (node && typeof node === 'object') {
|
|
108
|
+
const out = {};
|
|
109
|
+
for (const [k, v] of Object.entries(node)) out[k] = walk(v);
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
return node;
|
|
113
|
+
};
|
|
114
|
+
return { value: walk(clone(value)), hits };
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/** 收集所有字符串(用于校验/查残留) */
|
|
118
|
+
export const collectStrings = (value) => {
|
|
119
|
+
const out = [];
|
|
120
|
+
const walk = (node, path) => {
|
|
121
|
+
if (typeof node === 'string') out.push({ path, text: node });
|
|
122
|
+
else if (Array.isArray(node)) node.forEach((v, i) => walk(v, `${path}[${i}]`));
|
|
123
|
+
else if (node && typeof node === 'object') {
|
|
124
|
+
for (const [k, v] of Object.entries(node)) walk(v, path ? `${path}.${k}` : k);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
walk(value, '');
|
|
128
|
+
return out;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export class SiteManager {
|
|
132
|
+
#client;
|
|
133
|
+
#daoId;
|
|
134
|
+
|
|
135
|
+
constructor(clientOrConfig) {
|
|
136
|
+
this.#client = clientOrConfig instanceof LimsClient
|
|
137
|
+
? clientOrConfig
|
|
138
|
+
: new LimsClient(clientOrConfig);
|
|
139
|
+
this.#daoId = clientOrConfig?.daoId || this.#client.getConfig().daoId || null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** 当前站点所属 DAO id(站点 id === DAO id) */
|
|
143
|
+
getDaoId() {
|
|
144
|
+
// ★ 回退到客户端的当前值:API 访问令牌绑定 DAO 时,客户端在收到
|
|
145
|
+
// 带 currentDao 的响应后会回填 daoId ⇒ 用户不必配 DDN_HUB_DAO_ID。
|
|
146
|
+
return this.#daoId || this.#client.getConfig().daoId || null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 拿到可用的 DAO id(必要时向服务端探测一次)。
|
|
151
|
+
* @returns {Promise<string|null>}
|
|
152
|
+
*/
|
|
153
|
+
async #ensureDaoId() {
|
|
154
|
+
const known = this.getDaoId();
|
|
155
|
+
if (known) return known;
|
|
156
|
+
const client = this.#client;
|
|
157
|
+
if (client && typeof client.ensureDaoId === 'function') {
|
|
158
|
+
const discovered = await client.ensureDaoId();
|
|
159
|
+
return discovered || this.getDaoId() || null;
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
#pagePath(daoId, slug, suffix = '') {
|
|
165
|
+
return `/api/v1/daos/${daoId}/site/pages/${encodeURIComponent(slug)}${suffix}`;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** DAO(站点)基础信息 */
|
|
169
|
+
async getDao() {
|
|
170
|
+
const daoId = await this.#ensureDaoId();
|
|
171
|
+
return this.#client.request({
|
|
172
|
+
method: 'GET',
|
|
173
|
+
path: `/api/v1/daos/${daoId}`,
|
|
174
|
+
authRequired: true,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 页面列表 */
|
|
179
|
+
async listPages() {
|
|
180
|
+
const daoId = await this.#ensureDaoId();
|
|
181
|
+
const result = await this.#client.request({
|
|
182
|
+
method: 'GET',
|
|
183
|
+
path: `/api/v1/daos/${daoId}/site/pages`,
|
|
184
|
+
authRequired: true,
|
|
185
|
+
});
|
|
186
|
+
if (!result.ok) return result;
|
|
187
|
+
const data = result.data || {};
|
|
188
|
+
return { ok: true, message: result.message, items: data.items || [] };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 页面详情:page / draftVersion / publishedVersion
|
|
193
|
+
* @returns 合并出 blocks(优先草稿,其次已发布)
|
|
194
|
+
*/
|
|
195
|
+
async getPage(slug) {
|
|
196
|
+
const daoId = await this.#ensureDaoId();
|
|
197
|
+
const result = await this.#client.request({
|
|
198
|
+
method: 'GET',
|
|
199
|
+
path: this.#pagePath(daoId, slug),
|
|
200
|
+
authRequired: true,
|
|
201
|
+
});
|
|
202
|
+
if (!result.ok) return result;
|
|
203
|
+
const data = result.data || {};
|
|
204
|
+
const draftBlocks = data.draftVersion?.content?.blocks;
|
|
205
|
+
const publishedBlocks = data.publishedVersion?.content?.blocks;
|
|
206
|
+
const blocks = Array.isArray(draftBlocks) && draftBlocks.length > 0
|
|
207
|
+
? draftBlocks
|
|
208
|
+
: (Array.isArray(publishedBlocks) ? publishedBlocks : []);
|
|
209
|
+
return {
|
|
210
|
+
ok: true,
|
|
211
|
+
message: result.message,
|
|
212
|
+
page: data.page || null,
|
|
213
|
+
blocks,
|
|
214
|
+
hasDraft: Boolean(data.draftVersion),
|
|
215
|
+
hasPublished: Boolean(data.publishedVersion),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** 新建页面(content.blocks 一并提交) */
|
|
220
|
+
async createPage({ slug, title, blocks = [] }) {
|
|
221
|
+
if (!isFilled(slug)) return { ok: false, message: 'slug 不能为空' };
|
|
222
|
+
const daoId = await this.#ensureDaoId();
|
|
223
|
+
return this.#client.request({
|
|
224
|
+
method: 'POST',
|
|
225
|
+
path: `/api/v1/daos/${daoId}/site/pages`,
|
|
226
|
+
authRequired: true,
|
|
227
|
+
body: {
|
|
228
|
+
slug,
|
|
229
|
+
title: title || slug,
|
|
230
|
+
schemaVersion: SITE_SCHEMA_VERSION,
|
|
231
|
+
content: { blocks },
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 保存草稿(不改变线上内容)
|
|
238
|
+
* @param {string} slug 页面 slug
|
|
239
|
+
* @param {{title?: string, blocks: object[]}} payload
|
|
240
|
+
*/
|
|
241
|
+
async saveDraft(slug, { title, blocks }) {
|
|
242
|
+
if (!Array.isArray(blocks)) return { ok: false, message: 'blocks 必须是数组' };
|
|
243
|
+
const daoId = await this.#ensureDaoId();
|
|
244
|
+
return this.#client.request({
|
|
245
|
+
method: 'PUT',
|
|
246
|
+
path: this.#pagePath(daoId, slug, '/draft'),
|
|
247
|
+
authRequired: true,
|
|
248
|
+
body: {
|
|
249
|
+
title: title || undefined,
|
|
250
|
+
schemaVersion: SITE_SCHEMA_VERSION,
|
|
251
|
+
content: { blocks },
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** 发布单个页面(草稿 → 线上) */
|
|
257
|
+
async publishPage(slug) {
|
|
258
|
+
const daoId = await this.#ensureDaoId();
|
|
259
|
+
return this.#client.request({
|
|
260
|
+
method: 'POST',
|
|
261
|
+
path: this.#pagePath(daoId, slug, '/publish'),
|
|
262
|
+
authRequired: true,
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** 下线单个页面 */
|
|
267
|
+
async unpublishPage(slug) {
|
|
268
|
+
const daoId = await this.#ensureDaoId();
|
|
269
|
+
return this.#client.request({
|
|
270
|
+
method: 'POST',
|
|
271
|
+
path: this.#pagePath(daoId, slug, '/unpublish'),
|
|
272
|
+
authRequired: true,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** 删除页面 */
|
|
277
|
+
async deletePage(slug) {
|
|
278
|
+
const daoId = await this.#ensureDaoId();
|
|
279
|
+
return this.#client.request({
|
|
280
|
+
method: 'DELETE',
|
|
281
|
+
path: this.#pagePath(daoId, slug),
|
|
282
|
+
authRequired: true,
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** 站点设置 */
|
|
287
|
+
async getSettings() {
|
|
288
|
+
const daoId = await this.#ensureDaoId();
|
|
289
|
+
const result = await this.#client.request({
|
|
290
|
+
method: 'GET',
|
|
291
|
+
path: `/api/v1/daos/${daoId}/site/settings`,
|
|
292
|
+
authRequired: true,
|
|
293
|
+
});
|
|
294
|
+
if (!result.ok) return result;
|
|
295
|
+
const data = result.data || {};
|
|
296
|
+
return {
|
|
297
|
+
ok: true,
|
|
298
|
+
message: result.message,
|
|
299
|
+
schemaVersion: data.schemaVersion || SITE_SCHEMA_VERSION,
|
|
300
|
+
settings: data.settings || {},
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** 更新站点设置(整体替换 settings 对象) */
|
|
305
|
+
async updateSettings(settings) {
|
|
306
|
+
if (!settings || typeof settings !== 'object') {
|
|
307
|
+
return { ok: false, message: 'settings 必须是对象' };
|
|
308
|
+
}
|
|
309
|
+
const daoId = await this.#ensureDaoId();
|
|
310
|
+
return this.#client.request({
|
|
311
|
+
method: 'PUT',
|
|
312
|
+
path: `/api/v1/daos/${daoId}/site/settings`,
|
|
313
|
+
authRequired: true,
|
|
314
|
+
body: { schemaVersion: SITE_SCHEMA_VERSION, settings },
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** 整站发布(把各页面已发布内容汇总上线) */
|
|
319
|
+
async publishSite() {
|
|
320
|
+
const daoId = await this.#ensureDaoId();
|
|
321
|
+
return this.#client.request({
|
|
322
|
+
method: 'POST',
|
|
323
|
+
path: `/api/v1/daos/${daoId}/site/publish`,
|
|
324
|
+
authRequired: true,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// ===== 公开端读取(回读校验用,无需鉴权) =====
|
|
329
|
+
|
|
330
|
+
/** 公开端页面内容(线上已发布版本) */
|
|
331
|
+
async publicGetPage(slug, domain) {
|
|
332
|
+
const result = await this.#client.request({
|
|
333
|
+
method: 'GET',
|
|
334
|
+
path: `/api/v1/site/pages/${encodeURIComponent(slug)}`,
|
|
335
|
+
query: domain ? { domain } : undefined,
|
|
336
|
+
});
|
|
337
|
+
if (!result.ok) return result;
|
|
338
|
+
const data = result.data || {};
|
|
339
|
+
return {
|
|
340
|
+
ok: true,
|
|
341
|
+
message: result.message,
|
|
342
|
+
slug: data.slug || slug,
|
|
343
|
+
title: data.title || null,
|
|
344
|
+
blocks: data.content?.blocks || [],
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** 公开端站点上下文(DAO 品牌信息) */
|
|
349
|
+
async publicContext(domain) {
|
|
350
|
+
return this.#client.request({
|
|
351
|
+
method: 'GET',
|
|
352
|
+
path: '/api/v1/site/context',
|
|
353
|
+
query: domain ? { domain } : undefined,
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** 公开端站点设置 */
|
|
358
|
+
async publicSettings(domain) {
|
|
359
|
+
return this.#client.request({
|
|
360
|
+
method: 'GET',
|
|
361
|
+
path: '/api/v1/site/settings',
|
|
362
|
+
query: domain ? { domain } : undefined,
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* 批量替换某类区块的 props(用于站点头部导航、页脚等跨页一致性维护)
|
|
368
|
+
* @param {string} type 区块类型,如 SiteHeaderNav
|
|
369
|
+
* @param {object} props 需要覆盖的 props(与原 props 浅合并)
|
|
370
|
+
* @param {{pages?: string[], publish?: boolean}} options
|
|
371
|
+
*/
|
|
372
|
+
async setBlockProps(type, props, { pages, publish = false } = {}) {
|
|
373
|
+
if (!BLOCK_TYPES[type]) return { ok: false, message: `未知区块类型:${type}` };
|
|
374
|
+
const list = await this.listPages();
|
|
375
|
+
if (!list.ok) return list;
|
|
376
|
+
const targets = Array.isArray(pages) && pages.length > 0 && !pages.includes('all')
|
|
377
|
+
? pages
|
|
378
|
+
: list.items.map((i) => i.slug);
|
|
379
|
+
|
|
380
|
+
const report = [];
|
|
381
|
+
for (const slug of targets) {
|
|
382
|
+
const detail = await this.getPage(slug);
|
|
383
|
+
if (!detail.ok) {
|
|
384
|
+
report.push({ slug, ok: false, message: detail.message });
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
let changed = false;
|
|
388
|
+
const blocks = detail.blocks.map((b) => {
|
|
389
|
+
if (b.type !== type) return b;
|
|
390
|
+
changed = true;
|
|
391
|
+
return { ...b, props: { ...b.props, ...clone(props) } };
|
|
392
|
+
});
|
|
393
|
+
if (!changed) {
|
|
394
|
+
report.push({ slug, ok: true, skipped: true, message: `无 ${type} 区块` });
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
const saved = await this.saveDraft(slug, { title: detail.page?.title, blocks });
|
|
398
|
+
if (!saved.ok) {
|
|
399
|
+
report.push({ slug, ok: false, message: saved.message });
|
|
400
|
+
continue;
|
|
401
|
+
}
|
|
402
|
+
if (publish) {
|
|
403
|
+
const published = await this.publishPage(slug);
|
|
404
|
+
if (!published.ok) {
|
|
405
|
+
report.push({ slug, ok: false, message: published.message });
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
report.push({ slug, ok: true, published: Boolean(publish) });
|
|
410
|
+
}
|
|
411
|
+
const failed = report.filter((r) => !r.ok);
|
|
412
|
+
return {
|
|
413
|
+
ok: failed.length === 0,
|
|
414
|
+
message: failed.length === 0 ? `已处理 ${report.length} 个页面` : `${failed.length}/${report.length} 个页面处理失败`,
|
|
415
|
+
report,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* 批量替换品牌字符串并逐页保存草稿(可选发布)
|
|
421
|
+
* @param {{replacements: Array<[string|RegExp,string]>, pages?: string[], publish?: boolean}} options
|
|
422
|
+
*/
|
|
423
|
+
async replaceBrand({ replacements, pages, publish = false } = {}) {
|
|
424
|
+
if (!Array.isArray(replacements) || replacements.length === 0) {
|
|
425
|
+
return { ok: false, message: '需要 replace 规则' };
|
|
426
|
+
}
|
|
427
|
+
const list = await this.listPages();
|
|
428
|
+
if (!list.ok) return list;
|
|
429
|
+
const targets = Array.isArray(pages) && pages.length > 0 && !pages.includes('all')
|
|
430
|
+
? pages
|
|
431
|
+
: list.items.map((i) => i.slug);
|
|
432
|
+
|
|
433
|
+
const report = [];
|
|
434
|
+
for (const slug of targets) {
|
|
435
|
+
const detail = await this.getPage(slug);
|
|
436
|
+
if (!detail.ok) {
|
|
437
|
+
report.push({ slug, ok: false, message: detail.message });
|
|
438
|
+
continue;
|
|
439
|
+
}
|
|
440
|
+
const { value: blocks, hits } = replaceInStrings(detail.blocks, replacements);
|
|
441
|
+
if (hits === 0) {
|
|
442
|
+
report.push({ slug, ok: true, hits: 0, skipped: true });
|
|
443
|
+
continue;
|
|
444
|
+
}
|
|
445
|
+
const saved = await this.saveDraft(slug, {
|
|
446
|
+
title: detail.page?.title,
|
|
447
|
+
blocks,
|
|
448
|
+
});
|
|
449
|
+
if (!saved.ok) {
|
|
450
|
+
report.push({ slug, ok: false, hits, message: saved.message });
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
if (publish) {
|
|
454
|
+
const published = await this.publishPage(slug);
|
|
455
|
+
if (!published.ok) {
|
|
456
|
+
report.push({ slug, ok: false, hits, message: published.message });
|
|
457
|
+
continue;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
report.push({ slug, ok: true, hits, published: Boolean(publish) });
|
|
461
|
+
}
|
|
462
|
+
const failed = report.filter((r) => !r.ok);
|
|
463
|
+
return {
|
|
464
|
+
ok: failed.length === 0,
|
|
465
|
+
message: failed.length === 0
|
|
466
|
+
? `已处理 ${report.length} 个页面`
|
|
467
|
+
: `${failed.length}/${report.length} 个页面处理失败`,
|
|
468
|
+
report,
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
}
|