suppa-mcp-2 1.0.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/LICENSE +21 -0
- package/README.md +59 -0
- package/dist/auth.js +612 -0
- package/dist/bin.js +11 -0
- package/dist/config.js +168 -0
- package/dist/httpClient.js +269 -0
- package/dist/registry.js +111 -0
- package/dist/server.js +1533 -0
- package/dist/store.js +120 -0
- package/dist/tenants.js +408 -0
- package/dist/tools/automation.js +776 -0
- package/dist/tools/docs.js +534 -0
- package/dist/tools/entity.js +831 -0
- package/dist/tools/forms.js +550 -0
- package/dist/tools/migrate.js +936 -0
- package/dist/tools/nameMap.js +134 -0
- package/dist/tools/tasks.js +423 -0
- package/dist/tools/v1Fields.js +0 -0
- package/dist/tools/workspace.js +121 -0
- package/dist/utils.js +391 -0
- package/package.json +26 -0
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Docs, Pages, and PageBlocks tools.
|
|
3
|
+
*
|
|
4
|
+
* Faithful 1:1 port of the Python `suppa_mcp/tools/docs.py`.
|
|
5
|
+
*
|
|
6
|
+
* The pure tool logic mirrors the Python module verbatim. Mutations (including
|
|
7
|
+
* the deletes) are UNCONDITIONAL here, exactly like Python's docs.py — the
|
|
8
|
+
* approval/confirm gate lives one layer up in server.ts (as it does in
|
|
9
|
+
* Python's server.py), so it is applied uniformly across all tools.
|
|
10
|
+
*/
|
|
11
|
+
import { makeRequest, dataUrl, customOrderUrl, searchEntity, selectEntity, insertEntity, updateEntity, removeEntity, } from "../httpClient.js";
|
|
12
|
+
import { makeFilter, stripHtml, jsonResponse, wrapHtml, unescapeUnicode, parseJsonParam, } from "../utils.js";
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// Constants
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
export const ENTITY_DOCS = "Docs";
|
|
17
|
+
export const ENTITY_PAGES = "Pages";
|
|
18
|
+
export const ENTITY_PAGE_BLOCKS = "PageBlocks";
|
|
19
|
+
export const DOC_FIELDS = {
|
|
20
|
+
id: true,
|
|
21
|
+
title: true,
|
|
22
|
+
createdAt: true,
|
|
23
|
+
updatedAt: true,
|
|
24
|
+
createdBy: { id: true, fullName: true },
|
|
25
|
+
isPublic: true,
|
|
26
|
+
icon: true,
|
|
27
|
+
};
|
|
28
|
+
export const PAGE_FIELDS = {
|
|
29
|
+
id: true,
|
|
30
|
+
title: true,
|
|
31
|
+
description: true,
|
|
32
|
+
doc: { id: true, title: true },
|
|
33
|
+
parent: { id: true, title: true },
|
|
34
|
+
createdAt: true,
|
|
35
|
+
updatedAt: true,
|
|
36
|
+
status: true,
|
|
37
|
+
isPublic: true,
|
|
38
|
+
isArchived: true,
|
|
39
|
+
icon: true,
|
|
40
|
+
createdBy: { id: true, fullName: true },
|
|
41
|
+
};
|
|
42
|
+
export const BLOCK_FIELDS = {
|
|
43
|
+
id: true,
|
|
44
|
+
type: true,
|
|
45
|
+
properties: true,
|
|
46
|
+
format: true,
|
|
47
|
+
parent: { id: true },
|
|
48
|
+
children: "#expand_tree(10)",
|
|
49
|
+
createdAt: true,
|
|
50
|
+
};
|
|
51
|
+
// ---------------------------------------------------------------------------
|
|
52
|
+
// Block content helpers
|
|
53
|
+
// ---------------------------------------------------------------------------
|
|
54
|
+
/** Convert a block to readable text. `listIndex` numbers numberedList items. */
|
|
55
|
+
export function _blockToText(block, listIndex = 1) {
|
|
56
|
+
const btype = block.type ?? "";
|
|
57
|
+
const props = block.properties ?? {};
|
|
58
|
+
const title = stripHtml(props.title ?? "");
|
|
59
|
+
if (btype === "head1" || btype === "head2" || btype === "head3") {
|
|
60
|
+
const level = parseInt(btype[btype.length - 1], 10);
|
|
61
|
+
return "#".repeat(level) + " " + title;
|
|
62
|
+
}
|
|
63
|
+
if (btype === "text") {
|
|
64
|
+
return title;
|
|
65
|
+
}
|
|
66
|
+
if (btype === "bulletList") {
|
|
67
|
+
return "• " + title;
|
|
68
|
+
}
|
|
69
|
+
if (btype === "numberedList") {
|
|
70
|
+
return `${listIndex}. ${title}`;
|
|
71
|
+
}
|
|
72
|
+
if (btype === "checkList") {
|
|
73
|
+
const checked = props.checked ?? false;
|
|
74
|
+
return `[${checked ? "x" : " "}] ${title}`;
|
|
75
|
+
}
|
|
76
|
+
if (btype === "quote") {
|
|
77
|
+
return "> " + title;
|
|
78
|
+
}
|
|
79
|
+
if (btype === "code") {
|
|
80
|
+
const lang = props.language ?? "";
|
|
81
|
+
return "```" + lang + "\n" + title + "\n```";
|
|
82
|
+
}
|
|
83
|
+
if (btype === "divider") {
|
|
84
|
+
return "---";
|
|
85
|
+
}
|
|
86
|
+
if (btype === "table") {
|
|
87
|
+
return `[Table: ${title || "untitled"}]`;
|
|
88
|
+
}
|
|
89
|
+
if (btype === "callout") {
|
|
90
|
+
return `💡 ${title}`;
|
|
91
|
+
}
|
|
92
|
+
return title ? `[${btype}] ${title}` : `[${btype}]`;
|
|
93
|
+
}
|
|
94
|
+
/** Build properties object for a block type. */
|
|
95
|
+
export function _buildBlockProperties(btype, title = "", table = null) {
|
|
96
|
+
if (btype === "table" && table) {
|
|
97
|
+
return table;
|
|
98
|
+
}
|
|
99
|
+
if (btype === "divider") {
|
|
100
|
+
return {};
|
|
101
|
+
}
|
|
102
|
+
// Heal \uXXXX-escaped text for prose blocks; leave code/source verbatim.
|
|
103
|
+
const props = { title: wrapHtml(title, btype !== "code") };
|
|
104
|
+
if (btype === "code") {
|
|
105
|
+
props.language = "";
|
|
106
|
+
}
|
|
107
|
+
if (btype === "checkList") {
|
|
108
|
+
props.checked = false;
|
|
109
|
+
}
|
|
110
|
+
return props;
|
|
111
|
+
}
|
|
112
|
+
function isPlainObject(v) {
|
|
113
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
114
|
+
}
|
|
115
|
+
/** Python-ish type name for the malformed-entry error message. */
|
|
116
|
+
function typeName(value) {
|
|
117
|
+
if (value === null)
|
|
118
|
+
return "NoneType";
|
|
119
|
+
if (Array.isArray(value))
|
|
120
|
+
return "list";
|
|
121
|
+
switch (typeof value) {
|
|
122
|
+
case "object":
|
|
123
|
+
return "dict";
|
|
124
|
+
case "string":
|
|
125
|
+
return "str";
|
|
126
|
+
case "number":
|
|
127
|
+
return Number.isInteger(value) ? "int" : "float";
|
|
128
|
+
case "boolean":
|
|
129
|
+
return "bool";
|
|
130
|
+
default:
|
|
131
|
+
return typeof value;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Parse a blocks_json array into PageBlocks insert payloads.
|
|
136
|
+
*
|
|
137
|
+
* Throws Error (with an actionable message) on malformed JSON or a non-array
|
|
138
|
+
* payload — callers turn that into a structured tool error.
|
|
139
|
+
*/
|
|
140
|
+
export function _blocksFromJson(pageId, blocksJson) {
|
|
141
|
+
const parsed = parseJsonParam(blocksJson, "blocks_json", "array");
|
|
142
|
+
const insertFields = [];
|
|
143
|
+
for (const bd of parsed) {
|
|
144
|
+
if (!isPlainObject(bd)) {
|
|
145
|
+
throw new Error('blocks_json entries must be objects like {"type": "text", "title": "..."}, ' +
|
|
146
|
+
`got ${typeName(bd)}.`);
|
|
147
|
+
}
|
|
148
|
+
const btype = bd.type ?? "text";
|
|
149
|
+
let props;
|
|
150
|
+
if (isPlainObject(bd.properties)) {
|
|
151
|
+
props = bd.properties;
|
|
152
|
+
// Heal \uXXXX-escaped title text even on the explicit-properties path
|
|
153
|
+
// (skip code, where such sequences may be genuine source).
|
|
154
|
+
if (btype !== "code" && typeof props.title === "string") {
|
|
155
|
+
props.title = unescapeUnicode(props.title);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
props = _buildBlockProperties(btype, bd.title ?? "", bd.table ?? null);
|
|
160
|
+
}
|
|
161
|
+
insertFields.push({
|
|
162
|
+
page: { id: pageId },
|
|
163
|
+
type: btype,
|
|
164
|
+
properties: props,
|
|
165
|
+
format: bd.format ?? { block_color: "" },
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
return insertFields;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The orderBy that returns PageBlocks in display (custom) order.
|
|
172
|
+
*
|
|
173
|
+
* Block order lives in a custom-order index, not a scalar field, so it must be
|
|
174
|
+
* read with the platform's `#custom_order` orderBy function keyed by the page
|
|
175
|
+
* context — exactly as the Suppa UI does. A plain orderBy returns insertion order.
|
|
176
|
+
*/
|
|
177
|
+
function _customOrderBy(pageId) {
|
|
178
|
+
return [{ field: "page", order: "desc", function: "#custom_order", args: [pageId] }];
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Depth-first flatten of the parent/children tree, preserving order.
|
|
182
|
+
*
|
|
183
|
+
* The custom-order read returns only top-level blocks (parent IS null) with
|
|
184
|
+
* their nested blocks under `children` (#expand_tree). Downstream helpers want
|
|
185
|
+
* a single ordered list, so we inline the children in place and drop the now
|
|
186
|
+
* redundant nested arrays. Defensive against a malformed response: non-dict
|
|
187
|
+
* rows and non-list `children` are skipped, and ids are de-duplicated so an
|
|
188
|
+
* includeDeletedRelations echo can't surface the same block twice.
|
|
189
|
+
*/
|
|
190
|
+
export function _flattenBlockTree(rows, _seen = null) {
|
|
191
|
+
const seen = _seen !== null ? _seen : new Set();
|
|
192
|
+
const out = [];
|
|
193
|
+
for (const r of rows) {
|
|
194
|
+
if (!isPlainObject(r)) {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
const children = r.children;
|
|
198
|
+
delete r.children;
|
|
199
|
+
const bid = r.id;
|
|
200
|
+
if (bid !== null && bid !== undefined && seen.has(bid)) {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (bid !== null && bid !== undefined) {
|
|
204
|
+
seen.add(bid);
|
|
205
|
+
}
|
|
206
|
+
out.push(r);
|
|
207
|
+
if (Array.isArray(children) && children.length) {
|
|
208
|
+
out.push(..._flattenBlockTree(children, seen));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return out;
|
|
212
|
+
}
|
|
213
|
+
/** Fetch a page's blocks in custom (display) order (used by getBlocks/readPage). */
|
|
214
|
+
async function _fetchBlocks(pageId) {
|
|
215
|
+
const body = {
|
|
216
|
+
fields: BLOCK_FIELDS,
|
|
217
|
+
conditions: {
|
|
218
|
+
operator: "and",
|
|
219
|
+
filters: [
|
|
220
|
+
{ field: "page.id", comparator: "=", value: pageId },
|
|
221
|
+
{ field: "parent", comparator: "=", value: null },
|
|
222
|
+
],
|
|
223
|
+
},
|
|
224
|
+
orderBy: _customOrderBy(pageId),
|
|
225
|
+
includeDeletedRelations: true,
|
|
226
|
+
};
|
|
227
|
+
const rows = await makeRequest("POST", dataUrl(ENTITY_PAGE_BLOCKS, "select") + "?markAsView=false", body);
|
|
228
|
+
return _flattenBlockTree(Array.isArray(rows) ? rows : []);
|
|
229
|
+
}
|
|
230
|
+
// ---------------------------------------------------------------------------
|
|
231
|
+
// Docs tools
|
|
232
|
+
// ---------------------------------------------------------------------------
|
|
233
|
+
/** List all documents, optionally filtering by title. */
|
|
234
|
+
export async function listDocs(search = "", limit = 20) {
|
|
235
|
+
const filters = [];
|
|
236
|
+
if (search) {
|
|
237
|
+
filters.push(makeFilter("title", `%${search}%`, "like"));
|
|
238
|
+
}
|
|
239
|
+
const results = await searchEntity(ENTITY_DOCS, filters, DOC_FIELDS, limit);
|
|
240
|
+
return jsonResponse(results);
|
|
241
|
+
}
|
|
242
|
+
/** Get a document by ID, including its pages. */
|
|
243
|
+
export async function getDoc(docId) {
|
|
244
|
+
const result = await selectEntity(ENTITY_DOCS, [makeFilter("id", docId)], {
|
|
245
|
+
...DOC_FIELDS,
|
|
246
|
+
pages: { id: true, title: true, status: true, parent: { id: true } },
|
|
247
|
+
});
|
|
248
|
+
return jsonResponse(result);
|
|
249
|
+
}
|
|
250
|
+
/** Create a new document. */
|
|
251
|
+
export async function createDoc(title, isPublic = false) {
|
|
252
|
+
const result = await insertEntity(ENTITY_DOCS, [
|
|
253
|
+
{ title: unescapeUnicode(title), isPublic: isPublic },
|
|
254
|
+
]);
|
|
255
|
+
return jsonResponse(result.length ? result[0] : null);
|
|
256
|
+
}
|
|
257
|
+
/** Update a document's title or visibility. */
|
|
258
|
+
export async function updateDoc(docId, title = null, isPublic = null) {
|
|
259
|
+
const fields = {};
|
|
260
|
+
if (title !== null && title !== undefined) {
|
|
261
|
+
fields.title = unescapeUnicode(title);
|
|
262
|
+
}
|
|
263
|
+
if (isPublic !== null && isPublic !== undefined) {
|
|
264
|
+
fields.isPublic = isPublic;
|
|
265
|
+
}
|
|
266
|
+
if (Object.keys(fields).length === 0) {
|
|
267
|
+
return jsonResponse({ error: "No fields to update" });
|
|
268
|
+
}
|
|
269
|
+
const result = await updateEntity(ENTITY_DOCS, docId, fields);
|
|
270
|
+
return jsonResponse(result);
|
|
271
|
+
}
|
|
272
|
+
/** Delete a document. */
|
|
273
|
+
export async function deleteDoc(docId) {
|
|
274
|
+
const result = await removeEntity(ENTITY_DOCS, [makeFilter("id", docId)]);
|
|
275
|
+
return jsonResponse(result);
|
|
276
|
+
}
|
|
277
|
+
// ---------------------------------------------------------------------------
|
|
278
|
+
// Pages tools
|
|
279
|
+
// ---------------------------------------------------------------------------
|
|
280
|
+
/** List all pages in a document. */
|
|
281
|
+
export async function listPages(docId, limit = 50) {
|
|
282
|
+
const results = await searchEntity(ENTITY_PAGES, [makeFilter("doc", { id: docId })], PAGE_FIELDS, limit, 0, [{ field: "createdAt", order: "asc" }]);
|
|
283
|
+
return jsonResponse(results);
|
|
284
|
+
}
|
|
285
|
+
/** Get a page by ID with metadata. */
|
|
286
|
+
export async function getPage(pageId) {
|
|
287
|
+
const result = await selectEntity(ENTITY_PAGES, [makeFilter("id", pageId)], PAGE_FIELDS);
|
|
288
|
+
return jsonResponse(result);
|
|
289
|
+
}
|
|
290
|
+
/** Create a new page in a document. */
|
|
291
|
+
export async function createPage(docId, title, parentId = null, description = null, icon = null) {
|
|
292
|
+
const fields = { doc: { id: docId }, title: unescapeUnicode(title) };
|
|
293
|
+
if (parentId) {
|
|
294
|
+
fields.parent = { id: parentId };
|
|
295
|
+
}
|
|
296
|
+
if (description) {
|
|
297
|
+
fields.description = unescapeUnicode(description);
|
|
298
|
+
}
|
|
299
|
+
if (icon) {
|
|
300
|
+
fields.icon = icon;
|
|
301
|
+
}
|
|
302
|
+
const result = await insertEntity(ENTITY_PAGES, [fields]);
|
|
303
|
+
return jsonResponse(result.length ? result[0] : null);
|
|
304
|
+
}
|
|
305
|
+
/** Update a page's metadata. */
|
|
306
|
+
export async function updatePage(pageId, title = null, description = null, icon = null, isPublic = null) {
|
|
307
|
+
const fields = {};
|
|
308
|
+
if (title !== null && title !== undefined) {
|
|
309
|
+
fields.title = unescapeUnicode(title);
|
|
310
|
+
}
|
|
311
|
+
if (description !== null && description !== undefined) {
|
|
312
|
+
fields.description = unescapeUnicode(description);
|
|
313
|
+
}
|
|
314
|
+
if (icon !== null && icon !== undefined) {
|
|
315
|
+
fields.icon = icon;
|
|
316
|
+
}
|
|
317
|
+
if (isPublic !== null && isPublic !== undefined) {
|
|
318
|
+
fields.isPublic = isPublic;
|
|
319
|
+
}
|
|
320
|
+
if (Object.keys(fields).length === 0) {
|
|
321
|
+
return jsonResponse({ error: "No fields to update" });
|
|
322
|
+
}
|
|
323
|
+
const result = await updateEntity(ENTITY_PAGES, pageId, fields);
|
|
324
|
+
return jsonResponse(result);
|
|
325
|
+
}
|
|
326
|
+
/** Delete a page. */
|
|
327
|
+
export async function deletePage(pageId) {
|
|
328
|
+
const result = await removeEntity(ENTITY_PAGES, [makeFilter("id", pageId)]);
|
|
329
|
+
return jsonResponse(result);
|
|
330
|
+
}
|
|
331
|
+
// ---------------------------------------------------------------------------
|
|
332
|
+
// Blocks tools
|
|
333
|
+
// ---------------------------------------------------------------------------
|
|
334
|
+
/** Get all blocks on a page in display (custom) order. */
|
|
335
|
+
export async function getBlocks(pageId) {
|
|
336
|
+
return jsonResponse(await _fetchBlocks(pageId));
|
|
337
|
+
}
|
|
338
|
+
/** Read a page's content as human-readable text (markdown-like). */
|
|
339
|
+
export async function readPage(pageId) {
|
|
340
|
+
const lines = [];
|
|
341
|
+
let number = 0; // running counter for consecutive numberedList blocks
|
|
342
|
+
for (const block of await _fetchBlocks(pageId)) {
|
|
343
|
+
number = block.type === "numberedList" ? number + 1 : 0;
|
|
344
|
+
lines.push(_blockToText(block, Math.max(number, 1)));
|
|
345
|
+
}
|
|
346
|
+
return lines.length ? lines.join("\n\n") : "(empty page)";
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Create blocks on a page. blocks_json is a JSON array of
|
|
350
|
+
* {type, title, properties?, format?} objects.
|
|
351
|
+
* Supported types: text, head1, head2, head3, bulletList, numberedList,
|
|
352
|
+
* checkList, quote, callout, divider, code, table, toggle, image, embed,
|
|
353
|
+
* bookmark, linkToPage, columns, button.
|
|
354
|
+
*/
|
|
355
|
+
export async function createBlocks(pageId, blocksJson) {
|
|
356
|
+
let payload;
|
|
357
|
+
try {
|
|
358
|
+
payload = _blocksFromJson(pageId, blocksJson);
|
|
359
|
+
}
|
|
360
|
+
catch (e) {
|
|
361
|
+
return jsonResponse({ error: e instanceof Error ? e.message : String(e) });
|
|
362
|
+
}
|
|
363
|
+
const result = await insertEntity(ENTITY_PAGE_BLOCKS, payload, { id: true, type: true });
|
|
364
|
+
return jsonResponse(result);
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Insert block(s) at a specific position on a page.
|
|
368
|
+
* Provide after_id to insert after that block, or before_id to insert before it.
|
|
369
|
+
* blocks_json: JSON array of {type, title, properties?, format?}.
|
|
370
|
+
*/
|
|
371
|
+
export async function insertBlock(pageId, blocksJson, afterId = null, beforeId = null) {
|
|
372
|
+
if (!afterId && !beforeId) {
|
|
373
|
+
return jsonResponse({ error: "Provide after_id or before_id to specify position" });
|
|
374
|
+
}
|
|
375
|
+
let payload;
|
|
376
|
+
try {
|
|
377
|
+
payload = _blocksFromJson(pageId, blocksJson);
|
|
378
|
+
}
|
|
379
|
+
catch (e) {
|
|
380
|
+
return jsonResponse({ error: e instanceof Error ? e.message : String(e) });
|
|
381
|
+
}
|
|
382
|
+
const res = await makeRequest("POST", dataUrl(ENTITY_PAGE_BLOCKS, "insert"), {
|
|
383
|
+
fields: payload,
|
|
384
|
+
returning: { id: true, type: true },
|
|
385
|
+
});
|
|
386
|
+
// The insert action normally returns a list of inserted rows, but an empty
|
|
387
|
+
// body unwraps to None and some endpoints return a bare dict — coerce before
|
|
388
|
+
// iterating so a successful-but-empty response can't crash the tool.
|
|
389
|
+
const rows = Array.isArray(res) ? res : isPlainObject(res) ? [res] : [];
|
|
390
|
+
const newIds = rows
|
|
391
|
+
.filter((r) => isPlainObject(r) && r.id !== null && r.id !== undefined)
|
|
392
|
+
.map((r) => r.id);
|
|
393
|
+
if (newIds.length === 0) {
|
|
394
|
+
return jsonResponse({ error: "Insert returned no block ids; nothing to position.", raw: res });
|
|
395
|
+
}
|
|
396
|
+
// Reorder — API reverses multi-block order, so we always reverse
|
|
397
|
+
const reorderBody = {
|
|
398
|
+
instanceIds: [...newIds].reverse(),
|
|
399
|
+
contextFieldName: "page",
|
|
400
|
+
contextValue: pageId,
|
|
401
|
+
updates: {},
|
|
402
|
+
};
|
|
403
|
+
if (afterId) {
|
|
404
|
+
reorderBody.beforeInstanceId = afterId;
|
|
405
|
+
}
|
|
406
|
+
else if (beforeId) {
|
|
407
|
+
reorderBody.afterInstanceId = beforeId;
|
|
408
|
+
}
|
|
409
|
+
// The blocks already exist at this point. If only the positioning fails,
|
|
410
|
+
// report partial success with the ids — raising would hide the insert and
|
|
411
|
+
// bait the agent into retrying, which duplicates the blocks.
|
|
412
|
+
try {
|
|
413
|
+
await makeRequest("POST", customOrderUrl(ENTITY_PAGE_BLOCKS), reorderBody);
|
|
414
|
+
}
|
|
415
|
+
catch (e) {
|
|
416
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
417
|
+
return jsonResponse({
|
|
418
|
+
inserted_ids: newIds,
|
|
419
|
+
warning: `Blocks were created (at the end of the page) but positioning failed: ${msg}. ` +
|
|
420
|
+
"Do NOT re-insert — use suppa_reorder_blocks with these ids instead.",
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
return jsonResponse({
|
|
424
|
+
inserted_ids: newIds,
|
|
425
|
+
position: afterId ? "after" : "before",
|
|
426
|
+
anchor: afterId || beforeId,
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
/** Update a block's content. Provide title (HTML) or full properties_json. */
|
|
430
|
+
export async function updateBlock(blockId, title = null, propertiesJson = null) {
|
|
431
|
+
const fields = {};
|
|
432
|
+
if (propertiesJson) {
|
|
433
|
+
let props;
|
|
434
|
+
try {
|
|
435
|
+
props = parseJsonParam(propertiesJson, "properties_json", "object");
|
|
436
|
+
}
|
|
437
|
+
catch (e) {
|
|
438
|
+
return jsonResponse({ error: e instanceof Error ? e.message : String(e) });
|
|
439
|
+
}
|
|
440
|
+
// Heal \uXXXX-escaped title text on this path too (same rule as
|
|
441
|
+
// _blocksFromJson): prose blocks are healed, code blocks left verbatim.
|
|
442
|
+
if (typeof props.title === "string") {
|
|
443
|
+
const existing = await selectEntity(ENTITY_PAGE_BLOCKS, [makeFilter("id", blockId)], {
|
|
444
|
+
id: true,
|
|
445
|
+
type: true,
|
|
446
|
+
});
|
|
447
|
+
if (!(isPlainObject(existing) && existing.type === "code")) {
|
|
448
|
+
props.title = unescapeUnicode(props.title);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
fields.properties = props;
|
|
452
|
+
}
|
|
453
|
+
else if (title !== null && title !== undefined) {
|
|
454
|
+
// The title path heals \uXXXX escapes — but not for code blocks, where the
|
|
455
|
+
// escapes may be genuine source. updateBlock has no type arg, so look it up.
|
|
456
|
+
const existing = await selectEntity(ENTITY_PAGE_BLOCKS, [makeFilter("id", blockId)], {
|
|
457
|
+
id: true,
|
|
458
|
+
type: true,
|
|
459
|
+
});
|
|
460
|
+
const isCode = isPlainObject(existing) && existing.type === "code";
|
|
461
|
+
fields.properties = { title: wrapHtml(title, !isCode) };
|
|
462
|
+
}
|
|
463
|
+
else {
|
|
464
|
+
return jsonResponse({ error: "Provide title or properties_json" });
|
|
465
|
+
}
|
|
466
|
+
const result = await updateEntity(ENTITY_PAGE_BLOCKS, blockId, fields);
|
|
467
|
+
return jsonResponse(result);
|
|
468
|
+
}
|
|
469
|
+
/** Delete a single block. */
|
|
470
|
+
export async function deleteBlock(blockId) {
|
|
471
|
+
const result = await removeEntity(ENTITY_PAGE_BLOCKS, [makeFilter("id", blockId)]);
|
|
472
|
+
return jsonResponse(result);
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Reorder blocks within a page. block_ids: comma-separated IDs to move.
|
|
476
|
+
* Provide after_id to place them after that block, or before_id to place before it.
|
|
477
|
+
*/
|
|
478
|
+
export async function reorderBlocks(pageId, blockIds, afterId = null, beforeId = null) {
|
|
479
|
+
if (!afterId && !beforeId) {
|
|
480
|
+
return jsonResponse({ error: "Provide after_id or before_id" });
|
|
481
|
+
}
|
|
482
|
+
let ids;
|
|
483
|
+
try {
|
|
484
|
+
ids = parseCommaInts(blockIds);
|
|
485
|
+
}
|
|
486
|
+
catch {
|
|
487
|
+
return jsonResponse({
|
|
488
|
+
error: `block_ids must be comma-separated integers, got '${blockIds}'.`,
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
if (ids.length === 0) {
|
|
492
|
+
return jsonResponse({ error: "block_ids is empty — pass at least one block id." });
|
|
493
|
+
}
|
|
494
|
+
// API reverses multi-block order, so we always reverse
|
|
495
|
+
const body = {
|
|
496
|
+
instanceIds: [...ids].reverse(),
|
|
497
|
+
contextFieldName: "page",
|
|
498
|
+
contextValue: pageId,
|
|
499
|
+
updates: {},
|
|
500
|
+
};
|
|
501
|
+
if (afterId) {
|
|
502
|
+
body.beforeInstanceId = afterId;
|
|
503
|
+
}
|
|
504
|
+
else if (beforeId) {
|
|
505
|
+
body.afterInstanceId = beforeId;
|
|
506
|
+
}
|
|
507
|
+
await makeRequest("POST", customOrderUrl(ENTITY_PAGE_BLOCKS), body);
|
|
508
|
+
return jsonResponse({
|
|
509
|
+
reordered: ids,
|
|
510
|
+
position: afterId ? "after" : "before",
|
|
511
|
+
anchor: afterId || beforeId,
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Parse "1, 2 ,3" -> [1,2,3], mirroring Python
|
|
516
|
+
* `[int(x.strip()) for x in block_ids.split(",") if x.strip()]`.
|
|
517
|
+
* Throws on any non-integer token (matching Python's int() ValueError).
|
|
518
|
+
*/
|
|
519
|
+
function parseCommaInts(blockIds) {
|
|
520
|
+
const out = [];
|
|
521
|
+
for (const raw of blockIds.split(",")) {
|
|
522
|
+
const x = raw.trim();
|
|
523
|
+
if (!x) {
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
// Python int() accepts optional sign + digits (with surrounding whitespace,
|
|
527
|
+
// already stripped). Reject anything else.
|
|
528
|
+
if (!/^[+-]?\d+$/.test(x)) {
|
|
529
|
+
throw new Error(`invalid int: ${x}`);
|
|
530
|
+
}
|
|
531
|
+
out.push(parseInt(x, 10));
|
|
532
|
+
}
|
|
533
|
+
return out;
|
|
534
|
+
}
|