@@ -6,8 +6,6 @@ import { suggestUnDupTitle } from "./suggestUnDupTitle.ts";
66import type { Result } from "option-t/plain_result" ;
77import type { Socket } from "socket.io-client" ;
88
9- export type PatchOptions = PushOptions ;
10-
119export interface PatchMetadata extends Page {
1210 /** Number of retry attempts for page modification
1311 *
@@ -17,6 +15,30 @@ export interface PatchMetadata extends Page {
1715 attempts : number ;
1816}
1917
18+ /**
19+ * Function used in {@linkcode patch} to generate a patch from the current page state
20+ *
21+ * This function is used to generate a patch from the current page state.
22+ * It receives the current page lines and metadata and returns the new page content.
23+ * The function can be synchronous or asynchronous.
24+ *
25+ * @param lines - Current page lines
26+ * @param metadata - Current page metadata
27+ * @returns one of the following or a {@linkcode Promise} resolving to one:
28+ * - `(string | { text: string; })[]`: New page content
29+ * - `[]`: Delete the page
30+ * - `undefined`: Abort modification
31+ */
32+ export type MakePatchFn = (
33+ lines : BaseLine [ ] ,
34+ metadata : PatchMetadata ,
35+ ) =>
36+ | ( string | { text : string } ) [ ]
37+ | undefined
38+ | Promise < ( string | { text : string } ) [ ] | undefined > ;
39+
40+ export type PatchOptions = PushOptions ;
41+
2042/** Modify an entire Scrapbox page by computing and sending only the differences
2143 *
2244 * This function handles the entire page modification process:
@@ -26,29 +48,20 @@ export interface PatchMetadata extends Page {
2648 * 4. Handles errors (e.g., duplicate titles)
2749 * 5. Retries on conflicts
2850 *
29- * @param project - Project ID containing the target page
30- * @param title - Title of the page to modify
31- * @param update - Function to generate new content:
32- * - Input: Current page lines and metadata
33- * - Return values:
34- * - `string[]`: New page content
35- * - `undefined`: Abort modification
36- * - `[]`: Delete the page
37- * Can be async (returns `Promise`)
38- * @param options - Optional WebSocket configuration
51+ * @param project Project ID containing the target page
52+ * @param title Title of the page to modify
53+ * @param update Function to generate new content
54+ * @param options Optional WebSocket configuration
3955 *
4056 * Special cases:
41- * - If update returns undefined: Operation is cancelled
42- * - If update returns [] : Page is deleted
57+ * - If ` update` returns ` undefined` : Operation is cancelled
58+ * - If ` update` returns `[]` : Page is deleted
4359 * - On duplicate title: Automatically suggests non-conflicting title
4460 */
4561export const patch = (
4662 project : string ,
4763 title : string ,
48- update : (
49- lines : BaseLine [ ] ,
50- metadata : PatchMetadata ,
51- ) => string [ ] | undefined | Promise < string [ ] | undefined > ,
64+ update : MakePatchFn ,
5265 options ?: PatchOptions ,
5366) : Promise < Result < string , PushError | Socket . DisconnectReason > > =>
5467 push (
0 commit comments