@wirecat/notion-cli 0.1.0 → 0.3.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/CHANGELOG.md +69 -0
- package/README.md +14 -4
- package/contracts/wirecat.json +698 -1
- package/dist/bin/notion.js +2 -2
- package/dist/bundle/{all-profiles-OETMCOLN.js → all-profiles-4IONYZB6.js} +10 -10
- package/dist/bundle/{anthropic-5Y3O2TYM.js → anthropic-OXF74J4P.js} +6 -6
- package/dist/bundle/{app-server-IVWBIVHC.js → app-server-M2VF6ABM.js} +61 -52
- package/dist/bundle/{bun-EO636SAD.js → bun-DLA6QRLL.js} +2 -2
- package/dist/bundle/{bun-sqlite-C2T7QJ2M.js → bun-sqlite-F4BYLW3F.js} +7 -7
- package/dist/bundle/{chunk-QBC23NHD.js → chunk-2MHMKNWD.js} +1 -1
- package/dist/bundle/{chunk-TSCIIXN3.js → chunk-4KUJTSHG.js} +2 -2
- package/dist/bundle/{chunk-24FAYAML.js → chunk-4TQVZXOV.js} +3 -3
- package/dist/bundle/{chunk-FF4DDBCB.js → chunk-5LBJNTEJ.js} +6 -6
- package/dist/bundle/chunk-5UA2TYMI.js +29 -0
- package/dist/bundle/{chunk-DIM5YWBU.js → chunk-5XP4KQ2F.js} +1 -1
- package/dist/bundle/{chunk-WM7663C2.js → chunk-6GN7KU7X.js} +9 -9
- package/dist/bundle/chunk-6JBLIQQN.js +16 -0
- package/dist/bundle/{chunk-CLG2ISQH.js → chunk-7NF3V5L2.js} +4 -4
- package/dist/bundle/{chunk-TR7R5DL4.js → chunk-7NGFGFE5.js} +12 -63
- package/dist/bundle/{chunk-YA4LSSDZ.js → chunk-7NVPM55S.js} +2913 -2371
- package/dist/bundle/{chunk-XC5CKHX2.js → chunk-7YZPBAQX.js} +1 -1
- package/dist/bundle/{chunk-RSIS7BR7.js → chunk-AYU5TFDU.js} +21 -21
- package/dist/bundle/{chunk-64D5VUZ4.js → chunk-BEPFGITI.js} +68 -64
- package/dist/bundle/{chunk-HRTIRRXR.js → chunk-CBGBFAYJ.js} +41 -6
- package/dist/bundle/{chunk-QC3KOJT4.js → chunk-CDGZZNGY.js} +6 -6
- package/dist/bundle/{chunk-JK44XO2S.js → chunk-CIVXTBJY.js} +56 -56
- package/dist/bundle/{chunk-OWJDC4CB.js → chunk-COH7OEER.js} +14 -13
- package/dist/bundle/{chunk-XL2HXHP3.js → chunk-D6RCRBDH.js} +2 -2
- package/dist/bundle/{chunk-47R43UVJ.js → chunk-EQPAZ33O.js} +19 -19
- package/dist/bundle/{chunk-AE2KQ37N.js → chunk-EY6S7FV3.js} +6 -6
- package/dist/bundle/chunk-FEMHUSGO.js +88 -0
- package/dist/bundle/{chunk-NLDYZBTC.js → chunk-FSJHASRQ.js} +1146 -2555
- package/dist/bundle/chunk-GBFEEHQC.js +385 -0
- package/dist/bundle/{chunk-IQWEJEB5.js → chunk-GCCPFKSU.js} +26264 -21086
- package/dist/bundle/{chunk-RDZ7SVBZ.js → chunk-GK4GLPG2.js} +3 -3
- package/dist/bundle/{chunk-B2XKEEFX.js → chunk-GSNE7OB4.js} +4 -4
- package/dist/bundle/{chunk-UWW24JQF.js → chunk-HTKSCSJU.js} +4 -4
- package/dist/bundle/{chunk-7FII4DCE.js → chunk-IJMFG7HB.js} +7 -7
- package/dist/bundle/{chunk-WVZFXI36.js → chunk-JIRRF6BM.js} +4 -4
- package/dist/bundle/{chunk-ST2UC7FS.js → chunk-JODE2XED.js} +31 -27
- package/dist/bundle/{chunk-CWVAYQXI.js → chunk-K5I64ZVI.js} +9 -9
- package/dist/bundle/chunk-KFVDL2QS.js +146 -0
- package/dist/bundle/{chunk-JKIN5TOU.js → chunk-KL3P5TQK.js} +4 -4
- package/dist/bundle/chunk-L62XTC7N.js +726 -0
- package/dist/bundle/chunk-L6H7PY4X.js +170 -0
- package/dist/bundle/{chunk-F6MLJ3V5.js → chunk-LIQMUDIZ.js} +2 -2
- package/dist/bundle/{chunk-HWRKURQH.js → chunk-M2EEIYBU.js} +2 -2
- package/dist/bundle/chunk-M6DGZ2O6.js +823 -0
- package/dist/bundle/chunk-MGUEZYFA.js +5546 -0
- package/dist/bundle/{chunk-UWIA2G6Q.js → chunk-MRA62W5K.js} +404 -537
- package/dist/bundle/{chunk-XYIT2XB3.js → chunk-MVA5HVKL.js} +20 -0
- package/dist/bundle/{chunk-GA3URWIF.js → chunk-NO2LIHWA.js} +1 -1
- package/dist/bundle/{chunk-ESZT4WW3.js → chunk-OCDJ4FFA.js} +4 -4
- package/dist/bundle/{chunk-B4B65XIO.js → chunk-OI5DWZJG.js} +3 -3
- package/dist/bundle/chunk-P3P6DA6K.js +259 -0
- package/dist/bundle/chunk-QX3TPFEX.js +1977 -0
- package/dist/bundle/{chunk-EU5WCOMQ.js → chunk-R6SDRSZK.js} +76 -35
- package/dist/bundle/{chunk-K5TWMQSY.js → chunk-RMJMID5D.js} +8 -8
- package/dist/bundle/{chunk-RLSFOR2D.js → chunk-SGBS5QCU.js} +4 -4
- package/dist/bundle/{chunk-3D3PFXBP.js → chunk-T5UNSFES.js} +10 -15
- package/dist/bundle/{chunk-RAZKIHGH.js → chunk-TFNRCCGR.js} +4 -4
- package/dist/bundle/{chunk-WK3OSDNV.js → chunk-TJCPTRYD.js} +7 -7
- package/dist/bundle/{chunk-BWICUW7W.js → chunk-TXONV3HW.js} +2 -2
- package/dist/bundle/{chunk-AQZO3ITW.js → chunk-TZ2QJ53V.js} +2 -2
- package/dist/bundle/{chunk-35K5T6WV.js → chunk-UKE3CGHK.js} +1 -1
- package/dist/bundle/{chunk-7N5QB6NL.js → chunk-UROEWCOG.js} +5 -3
- package/dist/bundle/chunk-WHJWURZB.js +335 -0
- package/dist/bundle/{chunk-R6ZM45NF.js → chunk-WU3XHGOB.js} +185 -57
- package/dist/bundle/{chunk-2YR3RYT4.js → chunk-WZEEEQGC.js} +3 -3
- package/dist/bundle/{chunk-B2WFKFYL.js → chunk-XCTOCVMW.js} +20 -116
- package/dist/bundle/chunk-XFLCVHSQ.js +50 -0
- package/dist/bundle/{chunk-EGTBL3MJ.js → chunk-Y5LAJWR5.js} +1 -1
- package/dist/bundle/{chunk-66PL7P4C.js → chunk-YAPDU3SV.js} +1 -1
- package/dist/bundle/{chunk-WWMB4UHF.js → chunk-YFWLHTNO.js} +1 -1
- package/dist/bundle/{chunk-KHOVVPLC.js → chunk-YGCI3DHS.js} +1 -1
- package/dist/bundle/{chunk-4GC5DBHW.js → chunk-Z53LYX4M.js} +3 -3
- package/dist/bundle/{chunk-EPAZ7DJB.js → chunk-ZC6ZQQ55.js} +48 -45
- package/dist/bundle/command-contract-UBXS6NHI.js +38 -0
- package/dist/bundle/{command-server-entry-A66DJ3MH.js → command-server-entry-2SORMVRM.js} +27 -24
- package/dist/bundle/decode-5O7IEFPN.js +297 -0
- package/dist/bundle/{dispatch-FEKU5UXC.js → dispatch-YHQX6FEJ.js} +8 -8
- package/dist/bundle/{dist-YFKABFFF.js → dist-ILNOLGYN.js} +1 -1
- package/dist/bundle/{echarts-XD2JSWU4.js → echarts-ARLMVQC7.js} +2 -2
- package/dist/bundle/{embed-F3HYGAF7.js → embed-FUDUKQ3K.js} +7 -7
- package/dist/bundle/{embedder-GSEDDUH7.js → embedder-HDRV7GX3.js} +10 -10
- package/dist/bundle/{failure-HDJLR2YJ.js → failure-JHXICWIL.js} +3 -3
- package/dist/bundle/first-run-BGEFTXOE.js +29 -0
- package/dist/bundle/first-screen-F5F3VS6Y.js +38 -0
- package/dist/bundle/{instructions-NZGJ4FW5.js → instructions-MEFIKCDI.js} +1 -1
- package/dist/bundle/{mcp-VFIOZDVY.js → mcp-VMS6PBCU.js} +2 -2
- package/dist/bundle/mdast-util-from-markdown-R66F4HZA.js +1958 -0
- package/dist/bundle/mdast-util-gfm-LN2ELINH.js +2699 -0
- package/dist/bundle/meeting-combined-HNSS3FQY.js +24 -0
- package/dist/bundle/meeting-model-REZVYGQW.js +26 -0
- package/dist/bundle/meeting-semantic-R4CSLVRG.js +22 -0
- package/dist/bundle/micromark-extension-gfm-5X7YT43T.js +1838 -0
- package/dist/bundle/{node-RPKPDYQS.js → node-MAQALPLA.js} +2 -2
- package/dist/bundle/{node-sqlite-KH2UUYVO.js → node-sqlite-J7IJYNBO.js} +8 -8
- package/dist/bundle/notion.js +14 -14
- package/dist/bundle/{oauth-NKAI7TQD.js → oauth-AMV4YGJD.js} +8 -8
- package/dist/bundle/{open-WSAEAA2V.js → open-AB27MTGR.js} +6 -6
- package/dist/bundle/{openai-JPVDGHG5.js → openai-D77MU5XV.js} +31 -29
- package/dist/bundle/{process-5ZFVKOQX.js → process-SIOWTRJ6.js} +1 -1
- package/dist/bundle/{program-M3D56D6T.js → program-QC5MX4ZJ.js} +7533 -1997
- package/dist/bundle/{prompt-FROYQ5LV.js → prompt-W77YDHA6.js} +6 -6
- package/dist/bundle/{recognize-NYCHQMCX.js → recognize-3JAXQQ36.js} +5 -5
- package/dist/bundle/{remote-4OERF3Z4.js → remote-PSOJXCXO.js} +11 -11
- package/dist/bundle/{rerank-TEGXFUWY.js → rerank-EE44ZTGL.js} +8 -8
- package/dist/bundle/{schemas-MZNNOEYN.js → schemas-LBYUMBC6.js} +2 -2
- package/dist/bundle/{serve-HLYYBAXY.js → serve-XJMGAMFW.js} +10 -10
- package/dist/bundle/{server-D5OYNEUW.js → server-ASU6CP5R.js} +53 -44
- package/dist/bundle/server-WSPQ5EOA.js +72 -0
- package/dist/bundle/services-PT4PA4J6.js +588 -0
- package/dist/bundle/{sqlite-runtime-H2S5RR2F.js → sqlite-runtime-YHXRTVXE.js} +7 -7
- package/dist/bundle/{state-OPOXCDZL.js → state-T6ZKFWGD.js} +7 -7
- package/dist/bundle/{store-NHMXEJPD.js → store-3MUCZEI3.js} +20 -14
- package/dist/bundle/{store-Y3KRLCJ7.js → store-E5EE25FY.js} +30 -21
- package/dist/bundle/store-maintenance-command-ZZ55BBBG.js +63 -0
- package/dist/commands/api.d.ts.map +1 -1
- package/dist/commands/api.js +13 -4
- package/dist/commands/api.js.map +1 -1
- package/dist/commands/documents.d.ts +10 -1
- package/dist/commands/documents.d.ts.map +1 -1
- package/dist/commands/documents.js +175 -5
- package/dist/commands/documents.js.map +1 -1
- package/dist/commands/library.d.ts +10 -0
- package/dist/commands/library.d.ts.map +1 -0
- package/dist/commands/library.js +470 -0
- package/dist/commands/library.js.map +1 -0
- package/dist/commands/session.d.ts.map +1 -1
- package/dist/commands/session.js +4 -3
- package/dist/commands/session.js.map +1 -1
- package/dist/config.d.ts +19 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +73 -5
- package/dist/config.js.map +1 -1
- package/dist/context.d.ts +8 -0
- package/dist/context.d.ts.map +1 -1
- package/dist/context.js +15 -3
- package/dist/context.js.map +1 -1
- package/dist/generated/types.d.ts +485 -375
- package/dist/generated/types.d.ts.map +1 -1
- package/dist/identity.d.ts.map +1 -1
- package/dist/identity.js +1 -0
- package/dist/identity.js.map +1 -1
- package/dist/knowledge.d.ts +27 -0
- package/dist/knowledge.d.ts.map +1 -0
- package/dist/knowledge.js +87 -0
- package/dist/knowledge.js.map +1 -0
- package/dist/library.d.ts +44 -0
- package/dist/library.d.ts.map +1 -0
- package/dist/library.js +149 -0
- package/dist/library.js.map +1 -0
- package/dist/notion/client.d.ts +4 -1
- package/dist/notion/client.d.ts.map +1 -1
- package/dist/notion/client.js +5 -2
- package/dist/notion/client.js.map +1 -1
- package/dist/notion/errors.d.ts +5 -0
- package/dist/notion/errors.d.ts.map +1 -1
- package/dist/notion/errors.js +33 -1
- package/dist/notion/errors.js.map +1 -1
- package/dist/notion/reads.d.ts +4 -0
- package/dist/notion/reads.d.ts.map +1 -1
- package/dist/notion/writes.d.ts +40 -0
- package/dist/notion/writes.d.ts.map +1 -0
- package/dist/notion/writes.js +112 -0
- package/dist/notion/writes.js.map +1 -0
- package/dist/program.js +2 -2
- package/dist/program.js.map +1 -1
- package/dist/sync/cursor.d.ts +93 -0
- package/dist/sync/cursor.d.ts.map +1 -1
- package/dist/sync/cursor.js +63 -2
- package/dist/sync/cursor.js.map +1 -1
- package/dist/sync/files.d.ts +87 -0
- package/dist/sync/files.d.ts.map +1 -0
- package/dist/sync/files.js +31 -0
- package/dist/sync/files.js.map +1 -0
- package/dist/sync/markdown.d.ts +4 -1
- package/dist/sync/markdown.d.ts.map +1 -1
- package/dist/sync/markdown.js +8 -6
- package/dist/sync/markdown.js.map +1 -1
- package/dist/sync/source.d.ts +6 -59
- package/dist/sync/source.d.ts.map +1 -1
- package/dist/sync/source.js +650 -271
- package/dist/sync/source.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -3
- package/skills/notion-cli/SKILL.md +18 -1
- package/dist/bundle/command-contract-6G5DC2FN.js +0 -35
- package/dist/bundle/decode-7XOFRAAT.js +0 -163
- package/dist/bundle/first-run-TXNTIUSP.js +0 -28
- package/dist/bundle/first-screen-JZPJND7J.js +0 -33
- package/dist/bundle/server-LWZGVMNW.js +0 -62
- package/dist/bundle/store-maintenance-command-D4W7WSLQ.js +0 -53
- package/dist/commands/comments.d.ts +0 -3
- package/dist/commands/comments.d.ts.map +0 -1
- package/dist/commands/comments.js +0 -114
- package/dist/commands/comments.js.map +0 -1
- package/dist/sample/comments.d.ts +0 -61
- package/dist/sample/comments.d.ts.map +0 -1
- package/dist/sample/comments.js +0 -39
- package/dist/sample/comments.js.map +0 -1
- package/dist/sample/records.d.ts +0 -16
- package/dist/sample/records.d.ts.map +0 -1
- package/dist/sample/records.js +0 -22
- package/dist/sample/records.js.map +0 -1
- package/dist/sample/transport.d.ts +0 -42
- package/dist/sample/transport.d.ts.map +0 -1
- package/dist/sample/transport.js +0 -153
- package/dist/sample/transport.js.map +0 -1
- /package/dist/bundle/{chunk-ZI3YTXSF.js → chunk-SHTHNBHG.js} +0 -0
package/dist/sync/source.js
CHANGED
|
@@ -1,41 +1,24 @@
|
|
|
1
1
|
import { CliError, isCliError } from "@wirecat/cli-base";
|
|
2
|
+
import { deadlineMargin } from "@wirecat/cli-core/background";
|
|
3
|
+
import { isItemError, isNotFound, isRefused } from "../notion/errors.js";
|
|
2
4
|
import { withoutSignedLinks } from "../notion/links.js";
|
|
3
|
-
import { decodeCursor, encodeCursor } from "./cursor.js";
|
|
5
|
+
import { decodeCursor, decodeListPosition, encodeCursor, encodeListPosition, } from "./cursor.js";
|
|
6
|
+
import { parentId, sourceIds, UNTITLED, WORKSPACE_ROOT, } from "./files.js";
|
|
4
7
|
import { frontMatter, frontMatterLines, titleOf } from "./front-matter.js";
|
|
5
8
|
import { pageText } from "./markdown.js";
|
|
6
|
-
|
|
7
|
-
export const WORKSPACE_ROOT = "notion-workspace";
|
|
9
|
+
export { isNotionDocument, isNotionFolder, UNTITLED, WORKSPACE_ROOT, } from "./files.js";
|
|
8
10
|
const MINUTE = 60_000;
|
|
9
11
|
/** Notion rounds `last_edited_time` to the minute; a read this soon after an edit may miss another in that minute. */
|
|
10
12
|
export const SETTLE_MS = 3 * MINUTE;
|
|
11
13
|
export const MIN_OVERLAP_MS = 2 * MINUTE;
|
|
12
14
|
const DEFAULTS = { overlapMs: 10 * MINUTE, walkEveryMs: 24 * 60 * MINUTE, maxRequests: 2000 };
|
|
13
15
|
const PAGE_SIZE = 100;
|
|
14
|
-
/**
|
|
15
|
-
* Until cli-core plans a page with children as both a folder and a document (C2), only the workspace and databases
|
|
16
|
-
* are folders: the engine syncs top-level pages and database rows, not pages under pages.
|
|
17
|
-
*/
|
|
18
|
-
export const isNotionFolder = (file) => file.providerMetadata.kind === "workspace" || file.providerMetadata.kind === "database";
|
|
19
|
-
const parentId = (parent) => {
|
|
20
|
-
switch (parent.type) {
|
|
21
|
-
case "workspace":
|
|
22
|
-
return WORKSPACE_ROOT;
|
|
23
|
-
case "page_id":
|
|
24
|
-
return parent.page_id;
|
|
25
|
-
case "data_source_id":
|
|
26
|
-
return parent.data_source_id;
|
|
27
|
-
case "database_id":
|
|
28
|
-
return parent.database_id;
|
|
29
|
-
case "block_id":
|
|
30
|
-
return parent.block_id;
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
16
|
const minus = (iso, ms) => new Date(Date.parse(iso) - ms).toISOString();
|
|
34
17
|
const later = (a, b) => (!a ? b : !b ? a : Date.parse(a) >= Date.parse(b) ? a : b);
|
|
35
|
-
const isNotFound = (error) => isCliError(error) && error.code === "not_found";
|
|
36
|
-
/** Answers about one item, not the sync: a linked database view answers 404 by design, a restricted page 403. */
|
|
37
|
-
const isItemError = (error) => isCliError(error) && (error.code === "not_found" || error.code === "permission_error");
|
|
38
18
|
const text = (parts) => parts.map((part) => part.plain_text).join("");
|
|
19
|
+
const OUT_OF_TIME = new Error("the run's deadline is near");
|
|
20
|
+
/** Whether a Notion pass is still under way: the next `changes()` goes on with its tasks. */
|
|
21
|
+
export const passUnderWay = (token) => token !== undefined && decodeCursor(token).pass !== undefined;
|
|
39
22
|
export const notionSource = (notion, options = {}) => {
|
|
40
23
|
const overlapMs = options.overlapMs ?? DEFAULTS.overlapMs;
|
|
41
24
|
if (overlapMs < MIN_OVERLAP_MS)
|
|
@@ -45,10 +28,24 @@ export const notionSource = (notion, options = {}) => {
|
|
|
45
28
|
const roots = options.roots?.length ? options.roots : [WORKSPACE_ROOT];
|
|
46
29
|
const now = options.now ?? Date.now;
|
|
47
30
|
const warn = options.warn ?? (() => undefined);
|
|
31
|
+
const clock = options.clock ?? Date.now;
|
|
32
|
+
const deadline = options.deadline;
|
|
33
|
+
const margin = deadline ? deadlineMargin(deadline) : 0;
|
|
48
34
|
let requests = 0;
|
|
49
|
-
|
|
35
|
+
/** Set while `changes()` runs: a request that could end past the deadline is not sent. */
|
|
36
|
+
let timed = false;
|
|
37
|
+
let slowest = 0;
|
|
38
|
+
const counted = (call) => async (...args) => {
|
|
39
|
+
const started = clock();
|
|
40
|
+
if (timed && deadline && started + slowest + margin >= deadline.endsAt)
|
|
41
|
+
throw OUT_OF_TIME;
|
|
50
42
|
requests++;
|
|
51
|
-
|
|
43
|
+
try {
|
|
44
|
+
return await call.apply(notion, args);
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
slowest = Math.max(slowest, clock() - started);
|
|
48
|
+
}
|
|
52
49
|
};
|
|
53
50
|
const reads = {
|
|
54
51
|
self: counted(notion.self),
|
|
@@ -60,14 +57,18 @@ export const notionSource = (notion, options = {}) => {
|
|
|
60
57
|
blockChildren: counted(notion.blockChildren),
|
|
61
58
|
pageMarkdown: counted(notion.pageMarkdown),
|
|
62
59
|
};
|
|
63
|
-
//
|
|
60
|
+
// The state the engine holds does not change while the source lists, and parsing it costs as much as a request.
|
|
61
|
+
/** Folders listed again in this run after Notion stopped taking their saved position. */
|
|
62
|
+
const restarted = new Set();
|
|
63
|
+
let held;
|
|
64
64
|
let known = new Map();
|
|
65
65
|
// Containers resolved within one call; a page's parent is re-read on the next one, as it may have moved.
|
|
66
66
|
let containers = new Map();
|
|
67
67
|
let accessible = new Set();
|
|
68
68
|
let newestSeen = Number.NEGATIVE_INFINITY;
|
|
69
69
|
const fresh = () => {
|
|
70
|
-
|
|
70
|
+
held ??= new Map([...(options.known?.() ?? [])].map((file) => [file.id, file]));
|
|
71
|
+
known = held;
|
|
71
72
|
containers = new Map();
|
|
72
73
|
accessible = new Set(known.keys());
|
|
73
74
|
};
|
|
@@ -110,7 +111,7 @@ export const notionSource = (notion, options = {}) => {
|
|
|
110
111
|
const row = page.parent.type === "data_source_id" || page.parent.type === "database_id";
|
|
111
112
|
return {
|
|
112
113
|
id: page.id,
|
|
113
|
-
name: titleOf(page.properties),
|
|
114
|
+
name: titleOf(page.properties).trim() || UNTITLED,
|
|
114
115
|
mime: "application/vnd.notion.page",
|
|
115
116
|
parents: [await containerOf(page.parent)],
|
|
116
117
|
version: versionOf(page.last_edited_time),
|
|
@@ -127,11 +128,38 @@ export const notionSource = (notion, options = {}) => {
|
|
|
127
128
|
},
|
|
128
129
|
};
|
|
129
130
|
};
|
|
131
|
+
/**
|
|
132
|
+
* A page as its parent's listing gives it, with no read of its own. A page under a page has only its title for
|
|
133
|
+
* properties, and the block names it, its times and its authors (https://developers.notion.com/reference/block).
|
|
134
|
+
*/
|
|
135
|
+
const childPageFile = (block, container) => {
|
|
136
|
+
accessible.add(block.id);
|
|
137
|
+
return {
|
|
138
|
+
id: block.id,
|
|
139
|
+
name: block.child_page?.title.trim() || UNTITLED,
|
|
140
|
+
mime: "application/vnd.notion.page",
|
|
141
|
+
parents: [container],
|
|
142
|
+
version: versionOf(block.last_edited_time),
|
|
143
|
+
modifiedAt: block.last_edited_time,
|
|
144
|
+
createdAt: block.created_time,
|
|
145
|
+
// The block has no link. Notion's own form is https://app.notion.com/p/{page-id}, and the page's link replaces
|
|
146
|
+
// it when its text is read (https://developers.notion.com/reference/versioning).
|
|
147
|
+
url: known.get(block.id)?.url || `https://app.notion.com/p/${block.id.replaceAll("-", "")}`,
|
|
148
|
+
trashed: block.in_trash ?? false,
|
|
149
|
+
canDownload: true,
|
|
150
|
+
providerMetadata: {
|
|
151
|
+
kind: "page",
|
|
152
|
+
parent: { type: block.parent.type, id: parentId(block.parent) },
|
|
153
|
+
...(block.created_by ? { createdBy: block.created_by.id } : {}),
|
|
154
|
+
...(block.last_edited_by ? { lastEditedBy: block.last_edited_by.id } : {}),
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
};
|
|
130
158
|
const databaseFile = async (database) => {
|
|
131
159
|
accessible.add(database.id);
|
|
132
160
|
return {
|
|
133
161
|
id: database.id,
|
|
134
|
-
name: text(database.title),
|
|
162
|
+
name: text(database.title).trim() || UNTITLED,
|
|
135
163
|
mime: "application/vnd.notion.database",
|
|
136
164
|
parents: [await containerOf(database.parent)],
|
|
137
165
|
version: database.last_edited_time,
|
|
@@ -171,53 +199,109 @@ export const notionSource = (notion, options = {}) => {
|
|
|
171
199
|
return databaseFile(await reads.retrieveDatabase(id));
|
|
172
200
|
}
|
|
173
201
|
};
|
|
174
|
-
/** One
|
|
175
|
-
const
|
|
202
|
+
/** One page of a data source's rows, oldest first: created from `after` on, and edited since `since`. */
|
|
203
|
+
const rowPage = (source, after, since, start) => {
|
|
176
204
|
const filters = [
|
|
177
205
|
...(since ? [{ timestamp: "last_edited_time", last_edited_time: { on_or_after: since } }] : []),
|
|
178
206
|
...(after ? [{ timestamp: "created_time", created_time: { on_or_after: after } }] : []),
|
|
179
207
|
];
|
|
180
208
|
const filter = filters.length > 1 ? { and: filters } : filters[0];
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
209
|
+
return reads.queryDataSource(source, {
|
|
210
|
+
...(filter ? { filter } : {}),
|
|
211
|
+
sorts: [{ timestamp: "created_time", direction: "ascending" }],
|
|
212
|
+
page_size: PAGE_SIZE,
|
|
213
|
+
...(start ? { start_cursor: start } : {}),
|
|
214
|
+
});
|
|
215
|
+
};
|
|
216
|
+
/**
|
|
217
|
+
* A page of rows and where the next starts: from its last row's minute on, as Notion rounds `created_time` to the
|
|
218
|
+
* minute, or by the query's own position while a whole page shares one minute. `missing`: the known rows created
|
|
219
|
+
* within the span the page covered that it did not return, to read directly before any counts as gone.
|
|
220
|
+
*/
|
|
221
|
+
const rowsPage = async (database, source, at, since) => {
|
|
222
|
+
const page = await rowPage(source, at.after, since, at.start);
|
|
223
|
+
const last = page.results.at(-1)?.created_time;
|
|
224
|
+
const more = page.has_more && page.next_cursor && last ? { cursor: page.next_cursor, last } : undefined;
|
|
225
|
+
const dense = more && at.after !== undefined && Date.parse(more.last) <= Date.parse(at.after);
|
|
226
|
+
const seen = [...(at.seen ?? []), ...page.results.map((row) => row.id)];
|
|
227
|
+
if (dense && isCut(page))
|
|
228
|
+
throw new CliError("validation_error", `database ${database} has more than 10000 rows created in one minute`, {
|
|
229
|
+
reason: "notion.window-too-dense",
|
|
230
|
+
database,
|
|
190
231
|
});
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
232
|
+
const from = at.after ? Date.parse(at.after) : Number.NEGATIVE_INFINITY;
|
|
233
|
+
const to = more ? Date.parse(more.last) : Number.POSITIVE_INFINITY;
|
|
234
|
+
const shown = new Set(seen);
|
|
235
|
+
const missing = [...known.values()].filter((file) => {
|
|
236
|
+
const created = Date.parse(file.createdAt ?? "");
|
|
237
|
+
return (file.providerMetadata.kind === "row" &&
|
|
238
|
+
file.providerMetadata.parent?.id === source &&
|
|
239
|
+
!shown.has(file.id) &&
|
|
240
|
+
created >= from &&
|
|
241
|
+
created < to);
|
|
242
|
+
});
|
|
243
|
+
const next = dense
|
|
244
|
+
? { ...(at.after ? { after: at.after } : {}), start: more.cursor, seen }
|
|
245
|
+
: more
|
|
246
|
+
? { after: more.last }
|
|
247
|
+
: undefined;
|
|
248
|
+
return { rows: page.results, missing, next };
|
|
249
|
+
};
|
|
250
|
+
/**
|
|
251
|
+
* One page of a page's blocks: the child pages and databases it names, and the blocks still to list, the page first,
|
|
252
|
+
* then the blocks inside it that hold more (a child page can sit in a column or a toggle).
|
|
253
|
+
*/
|
|
254
|
+
const blocksPage = async (at) => {
|
|
255
|
+
const blocks = [...at.blocks];
|
|
256
|
+
const seen = [...at.seen];
|
|
257
|
+
const pages = [];
|
|
258
|
+
const databases = [];
|
|
259
|
+
const head = blocks[0];
|
|
260
|
+
try {
|
|
261
|
+
const page = await reads.blockChildren(head.id, head.start);
|
|
262
|
+
for (const block of page.results) {
|
|
263
|
+
if (block.in_trash)
|
|
264
|
+
continue;
|
|
265
|
+
if (block.type === "child_page" || block.type === "child_database")
|
|
266
|
+
seen.push(block.id);
|
|
267
|
+
if (block.type === "child_page")
|
|
268
|
+
pages.push(block);
|
|
269
|
+
else if (block.type === "child_database")
|
|
270
|
+
databases.push(block.id);
|
|
271
|
+
else if (block.has_children)
|
|
272
|
+
blocks.push({ id: block.id });
|
|
273
|
+
}
|
|
274
|
+
if (page.has_more && page.next_cursor)
|
|
275
|
+
blocks[0] = { id: head.id, start: page.next_cursor };
|
|
276
|
+
else
|
|
277
|
+
blocks.shift();
|
|
278
|
+
}
|
|
279
|
+
catch (error) {
|
|
280
|
+
if (!isItemError(error))
|
|
281
|
+
throw error;
|
|
282
|
+
// The known children it hides are read one by one before any counts as gone.
|
|
283
|
+
warn({
|
|
284
|
+
id: head.id,
|
|
285
|
+
code: error.code,
|
|
286
|
+
reason: "notion.left-out",
|
|
287
|
+
message: "the blocks inside were left out of this sync: Notion would not list them",
|
|
288
|
+
});
|
|
289
|
+
blocks.shift();
|
|
205
290
|
}
|
|
206
|
-
return {
|
|
291
|
+
return { pages, databases, blocks, seen };
|
|
207
292
|
};
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
293
|
+
/** A database a page's listing names; a linked view of one elsewhere answers 404, and is left out with a warning. */
|
|
294
|
+
const childDatabase = async (id) => {
|
|
295
|
+
try {
|
|
296
|
+
return await databaseFile(await reads.retrieveDatabase(id));
|
|
297
|
+
}
|
|
298
|
+
catch (error) {
|
|
299
|
+
if (!isItemError(error))
|
|
300
|
+
throw error;
|
|
301
|
+
const code = error.code;
|
|
302
|
+
warn({ id, code, reason: "notion.child-unreadable", message: "a child Notion would not give was left out" });
|
|
303
|
+
return undefined;
|
|
219
304
|
}
|
|
220
|
-
return [...files.values()];
|
|
221
305
|
};
|
|
222
306
|
const searchPage = (kind, start) => reads.search({
|
|
223
307
|
filter: { property: "object", value: kind },
|
|
@@ -241,12 +325,16 @@ export const notionSource = (notion, options = {}) => {
|
|
|
241
325
|
if (isCut(page))
|
|
242
326
|
warn({
|
|
243
327
|
code: "provider_error",
|
|
328
|
+
reason: "notion.search-capped",
|
|
244
329
|
message: `Notion's search gave only the ${results.length} most recently edited ${kind}s; a top-level one it left out is found once it is edited`,
|
|
245
330
|
});
|
|
246
331
|
} while (start);
|
|
247
332
|
return results;
|
|
248
333
|
};
|
|
249
|
-
/**
|
|
334
|
+
/**
|
|
335
|
+
* A database, or why Notion will not give it: `gone` on a 404 (search entries can be stale, a linked view always
|
|
336
|
+
* answers so), `unreadable` on a 403, which says nothing about whether it is still there.
|
|
337
|
+
*/
|
|
250
338
|
const readDatabase = async (id) => {
|
|
251
339
|
try {
|
|
252
340
|
return await reads.retrieveDatabase(id);
|
|
@@ -254,28 +342,21 @@ export const notionSource = (notion, options = {}) => {
|
|
|
254
342
|
catch (error) {
|
|
255
343
|
if (!isItemError(error))
|
|
256
344
|
throw error;
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
};
|
|
261
|
-
const childPages = async (blockId, found) => {
|
|
262
|
-
let start;
|
|
263
|
-
do {
|
|
264
|
-
const page = await reads.blockChildren(blockId, start);
|
|
265
|
-
for (const block of page.results) {
|
|
266
|
-
if (block.in_trash)
|
|
267
|
-
continue;
|
|
268
|
-
if (block.type === "child_page")
|
|
269
|
-
found.pages.push(block.id);
|
|
270
|
-
else if (block.type === "child_database")
|
|
271
|
-
found.databases.push(block.id);
|
|
272
|
-
else if (block.has_children)
|
|
273
|
-
await childPages(block.id, found);
|
|
345
|
+
if (isNotFound(error)) {
|
|
346
|
+
warn({ id, code: "not_found", reason: "notion.database-unreadable", message: "a database could not be read" });
|
|
347
|
+
return "gone";
|
|
274
348
|
}
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
349
|
+
warn({
|
|
350
|
+
id,
|
|
351
|
+
code: error.code,
|
|
352
|
+
reason: "notion.kept-unconfirmed",
|
|
353
|
+
message: "kept as it was: Notion would not show the database",
|
|
354
|
+
});
|
|
355
|
+
return "unreadable";
|
|
356
|
+
}
|
|
278
357
|
};
|
|
358
|
+
const isGone = (database) => database === "gone" || (database !== "unreadable" && database.in_trash);
|
|
359
|
+
/** The top of the workspace, from search: Notion lists no children of a workspace. */
|
|
279
360
|
const listed = async (id) => {
|
|
280
361
|
if (id === WORKSPACE_ROOT) {
|
|
281
362
|
const pages = (await searchAll("page"));
|
|
@@ -289,7 +370,7 @@ export const notionSource = (notion, options = {}) => {
|
|
|
289
370
|
if (databases.has(id) || source.in_trash)
|
|
290
371
|
continue;
|
|
291
372
|
const database = await readDatabase(id);
|
|
292
|
-
if (database)
|
|
373
|
+
if (typeof database === "object")
|
|
293
374
|
databases.set(id, database);
|
|
294
375
|
}
|
|
295
376
|
const files = [];
|
|
@@ -301,30 +382,7 @@ export const notionSource = (notion, options = {}) => {
|
|
|
301
382
|
files.push(await databaseFile(database));
|
|
302
383
|
return files.filter((file) => file.parents[0] === WORKSPACE_ROOT);
|
|
303
384
|
}
|
|
304
|
-
|
|
305
|
-
if (kind === "database")
|
|
306
|
-
return allRows(await reads.retrieveDatabase(id));
|
|
307
|
-
if (kind === "row")
|
|
308
|
-
return [];
|
|
309
|
-
const found = await childPages(id, { pages: [], databases: [] });
|
|
310
|
-
const files = [];
|
|
311
|
-
const child = async (id, read) => {
|
|
312
|
-
try {
|
|
313
|
-
const file = await read();
|
|
314
|
-
if (!file.trashed)
|
|
315
|
-
files.push(file);
|
|
316
|
-
}
|
|
317
|
-
catch (error) {
|
|
318
|
-
if (!isItemError(error))
|
|
319
|
-
throw error;
|
|
320
|
-
warn({ id, code: error.code, message: "a child Notion would not give was left out" });
|
|
321
|
-
}
|
|
322
|
-
};
|
|
323
|
-
for (const page of found.pages)
|
|
324
|
-
await child(page, async () => pageFile(await reads.retrievePage(page)));
|
|
325
|
-
for (const database of found.databases)
|
|
326
|
-
await child(database, async () => databaseFile(await reads.retrieveDatabase(database)));
|
|
327
|
-
return files;
|
|
385
|
+
return [];
|
|
328
386
|
};
|
|
329
387
|
/** Known items missing from a listing, each read directly: gone only on 404 or the trash, kept on any doubt. */
|
|
330
388
|
const confirm = async (missing, container, listing) => {
|
|
@@ -343,13 +401,113 @@ export const notionSource = (notion, options = {}) => {
|
|
|
343
401
|
listing.gone.push(file.id);
|
|
344
402
|
else if (isItemError(error)) {
|
|
345
403
|
listing.files.push(file);
|
|
346
|
-
warn({
|
|
404
|
+
warn({
|
|
405
|
+
id: file.id,
|
|
406
|
+
code: error.code,
|
|
407
|
+
reason: "notion.kept-unconfirmed",
|
|
408
|
+
message: "kept as it was: Notion would not say",
|
|
409
|
+
});
|
|
347
410
|
}
|
|
348
411
|
else
|
|
349
412
|
throw error;
|
|
350
413
|
}
|
|
351
414
|
}
|
|
352
415
|
};
|
|
416
|
+
/**
|
|
417
|
+
* One page of a folder's children for the engine's walk, with the position of the next in `cursor`. A database goes a
|
|
418
|
+
* page of rows at a time, a page a page of blocks at a time. Known children a page did not show are read directly
|
|
419
|
+
* before any counts as gone: for rows, those created within the span the page covers; for a page's children, those
|
|
420
|
+
* missing once its last block is listed. The workspace is one listing, as search has no order to resume a span by.
|
|
421
|
+
*/
|
|
422
|
+
const childrenPage = async (id, token) => {
|
|
423
|
+
if (id === WORKSPACE_ROOT)
|
|
424
|
+
return { files: (await listChildren(id)).files };
|
|
425
|
+
let at;
|
|
426
|
+
if (token === undefined) {
|
|
427
|
+
const kind = known.get(id)?.providerMetadata.kind ?? (await retrieve(id)).providerMetadata.kind;
|
|
428
|
+
if (kind === "row")
|
|
429
|
+
return { files: [] };
|
|
430
|
+
if (kind === "database") {
|
|
431
|
+
const database = await readDatabase(id);
|
|
432
|
+
if (typeof database !== "object" || !database.data_sources.length)
|
|
433
|
+
return { files: [] };
|
|
434
|
+
at = { k: "rows", sources: database.data_sources.map((source) => source.id) };
|
|
435
|
+
}
|
|
436
|
+
else
|
|
437
|
+
at = { k: "blocks", blocks: [{ id }], seen: [] };
|
|
438
|
+
}
|
|
439
|
+
else
|
|
440
|
+
at = decodeListPosition(token);
|
|
441
|
+
const files = [];
|
|
442
|
+
const listing = { files: [], gone: [], moved: [] };
|
|
443
|
+
// Notion answers 400 validation_error to a start_cursor it no longer takes. On a later page that is the saved
|
|
444
|
+
// position going stale: as not_found the engine lists the folder again from its first page and drops nothing.
|
|
445
|
+
// Only the request sent with the saved start_cursor: a 400 on any other (a block Notion cannot list) is the
|
|
446
|
+
// answer. A folder already listed again in this run that goes stale again fails, so it cannot loop.
|
|
447
|
+
const stale = (error, start) => {
|
|
448
|
+
if (token === undefined || start === undefined || !isRefused(error) || restarted.has(id))
|
|
449
|
+
return error;
|
|
450
|
+
restarted.add(id);
|
|
451
|
+
return new CliError("not_found", `Notion no longer takes the saved position in ${id}; it is listed again`, {
|
|
452
|
+
reason: "notion.listing-stale",
|
|
453
|
+
});
|
|
454
|
+
};
|
|
455
|
+
if (at.k === "rows") {
|
|
456
|
+
const source = at.sources[0];
|
|
457
|
+
let page;
|
|
458
|
+
try {
|
|
459
|
+
page = await rowsPage(id, source, at, null);
|
|
460
|
+
}
|
|
461
|
+
catch (error) {
|
|
462
|
+
if (!isItemError(error))
|
|
463
|
+
throw stale(error, at.start);
|
|
464
|
+
warn({
|
|
465
|
+
id: source,
|
|
466
|
+
code: error.code,
|
|
467
|
+
reason: "notion.left-out",
|
|
468
|
+
message: `a data source of database ${id} was left out: Notion would not query it`,
|
|
469
|
+
});
|
|
470
|
+
// Its rows stay as the sync holds them: a refusal says nothing of them, and the walk drops what it does not list.
|
|
471
|
+
for (const file of known.values())
|
|
472
|
+
if (file.providerMetadata.kind === "row" && file.providerMetadata.parent?.id === source)
|
|
473
|
+
files.push(file);
|
|
474
|
+
const rest = at.sources.slice(1);
|
|
475
|
+
return rest.length ? { files, cursor: encodeListPosition({ k: "rows", sources: rest }) } : { files };
|
|
476
|
+
}
|
|
477
|
+
for (const row of page.rows)
|
|
478
|
+
if (!row.in_trash)
|
|
479
|
+
files.push(await pageFile(row));
|
|
480
|
+
await confirm(page.missing, id, listing);
|
|
481
|
+
for (const file of listing.files)
|
|
482
|
+
files.push(file);
|
|
483
|
+
const next = page.next
|
|
484
|
+
? { k: "rows", sources: at.sources, ...page.next }
|
|
485
|
+
: at.sources.length > 1
|
|
486
|
+
? { k: "rows", sources: at.sources.slice(1) }
|
|
487
|
+
: undefined;
|
|
488
|
+
return next ? { files, cursor: encodeListPosition(next) } : { files };
|
|
489
|
+
}
|
|
490
|
+
let page;
|
|
491
|
+
try {
|
|
492
|
+
page = await blocksPage(at);
|
|
493
|
+
}
|
|
494
|
+
catch (error) {
|
|
495
|
+
throw stale(error, at.blocks[0]?.start);
|
|
496
|
+
}
|
|
497
|
+
for (const block of page.pages)
|
|
498
|
+
files.push(childPageFile(block, id));
|
|
499
|
+
for (const database of page.databases) {
|
|
500
|
+
const file = await childDatabase(database);
|
|
501
|
+
if (file && !file.trashed)
|
|
502
|
+
files.push(file);
|
|
503
|
+
}
|
|
504
|
+
const { blocks, seen } = page;
|
|
505
|
+
if (blocks.length)
|
|
506
|
+
return { files, cursor: encodeListPosition({ k: "blocks", blocks, seen }) };
|
|
507
|
+
const shown = new Set(seen);
|
|
508
|
+
await confirm([...known.values()].filter((file) => file.parents[0] === id && !shown.has(file.id)), id, listing);
|
|
509
|
+
return { files: [...files, ...listing.files] };
|
|
510
|
+
};
|
|
353
511
|
/** A container's children, with every known child that did not come back checked directly before it counts as gone. */
|
|
354
512
|
const listChildren = async (id) => {
|
|
355
513
|
const listing = { files: await listed(id), gone: [], moved: [] };
|
|
@@ -366,6 +524,17 @@ export const notionSource = (notion, options = {}) => {
|
|
|
366
524
|
};
|
|
367
525
|
const startPass = (cursor) => {
|
|
368
526
|
const walk = now() - cursor.walkedAt >= walkEveryMs;
|
|
527
|
+
if (walk && roots.includes(WORKSPACE_ROOT)) {
|
|
528
|
+
// The workspace is listed by search a page at a time, so that a walk too long for one run goes on in the next.
|
|
529
|
+
const top = [...known.values()].filter((file) => file.parents[0] === WORKSPACE_ROOT).map((file) => file.id);
|
|
530
|
+
const tasks = [
|
|
531
|
+
{ t: "search", kind: "page", full: true },
|
|
532
|
+
{ t: "search", kind: "data_source", full: true },
|
|
533
|
+
...roots.filter((id) => id !== WORKSPACE_ROOT).map((id) => ({ t: "walk", id })),
|
|
534
|
+
{ t: "confirm", container: WORKSPACE_ROOT, ids: top },
|
|
535
|
+
];
|
|
536
|
+
return { next: cursor.mark, startedAt: now(), walk, tasks };
|
|
537
|
+
}
|
|
369
538
|
const tasks = [
|
|
370
539
|
{ t: "search", kind: "page" },
|
|
371
540
|
{ t: "search", kind: "data_source" },
|
|
@@ -380,6 +549,8 @@ export const notionSource = (notion, options = {}) => {
|
|
|
380
549
|
fresh();
|
|
381
550
|
requests = 0;
|
|
382
551
|
const pass = cursor.pass ?? startPass(cursor);
|
|
552
|
+
containers = new Map(Object.entries(pass.parents ?? {}));
|
|
553
|
+
const resolved = containers.size;
|
|
383
554
|
const since = cursor.mark ? minus(cursor.mark, overlapMs) : null;
|
|
384
555
|
const items = new Map();
|
|
385
556
|
const emit = (file) => items.set(file.id, { id: file.id, removed: false, file });
|
|
@@ -387,165 +558,351 @@ export const notionSource = (notion, options = {}) => {
|
|
|
387
558
|
// The engine walks every folder it is sent, all its rows, so a database it already holds is not sent again.
|
|
388
559
|
const unheld = (file) => {
|
|
389
560
|
const before = known.get(file.id);
|
|
390
|
-
return !before ||
|
|
561
|
+
return (!before ||
|
|
562
|
+
before.name !== file.name ||
|
|
563
|
+
before.parents[0] !== file.parents[0] ||
|
|
564
|
+
sourceIds(before) !== sourceIds(file));
|
|
565
|
+
};
|
|
566
|
+
// Search still finds the rows of a data source taken out of its database; they stay out with it.
|
|
567
|
+
const unlistedRow = (page) => {
|
|
568
|
+
if (page.parent.type !== "data_source_id")
|
|
569
|
+
return false;
|
|
570
|
+
const { database_id, data_source_id } = page.parent;
|
|
571
|
+
const sources = known.get(database_id)?.providerMetadata.dataSources;
|
|
572
|
+
return sources !== undefined && !sources.some((item) => item.id === data_source_id);
|
|
573
|
+
};
|
|
574
|
+
// Rows of a data source the database no longer lists go with it. A database Notion will not show (403) lists
|
|
575
|
+
// nothing, so its rows stay.
|
|
576
|
+
const unlisted = (database) => {
|
|
577
|
+
const listed = new Set(database.data_sources.map((source) => source.id));
|
|
578
|
+
cursor.sources = cursor.sources.filter((item) => item.database !== database.id || listed.has(item.source));
|
|
579
|
+
for (const file of known.values())
|
|
580
|
+
if (file.providerMetadata.kind === "row" &&
|
|
581
|
+
file.parents[0] === database.id &&
|
|
582
|
+
!listed.has(file.providerMetadata.parent?.id ?? ""))
|
|
583
|
+
gone(file.id);
|
|
584
|
+
};
|
|
585
|
+
/** Work this run keeps even when the deadline cuts its step: a direct read or a resolved kind. */
|
|
586
|
+
let kept = 0;
|
|
587
|
+
// A saved position Notion no longer takes (it answers 400) starts that listing again, so it cannot fail every run.
|
|
588
|
+
const restart = (task, what) => {
|
|
589
|
+
warn({
|
|
590
|
+
code: "validation_error",
|
|
591
|
+
reason: "notion.listing-restarted",
|
|
592
|
+
message: `${what} started again from its top`,
|
|
593
|
+
});
|
|
594
|
+
pass.tasks.splice(1, 0, task);
|
|
595
|
+
};
|
|
596
|
+
// A confirm task stays last: it reads directly only what the rest of the pass did not show.
|
|
597
|
+
const queue = (task) => {
|
|
598
|
+
const last = pass.tasks.at(-1);
|
|
599
|
+
if (last?.t === "confirm")
|
|
600
|
+
pass.tasks.splice(-1, 0, task);
|
|
601
|
+
else
|
|
602
|
+
pass.tasks.push(task);
|
|
603
|
+
};
|
|
604
|
+
const shown = (id) => {
|
|
605
|
+
for (const task of pass.tasks)
|
|
606
|
+
if (task.t === "confirm")
|
|
607
|
+
task.ids = task.ids.filter((item) => item !== id);
|
|
391
608
|
};
|
|
392
609
|
const addSource = (database, source) => {
|
|
393
610
|
if (!cursor.sources.some((item) => item.source === source))
|
|
394
611
|
cursor.sources.push({ database, source });
|
|
395
612
|
};
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
613
|
+
// One page of the answer per task, so that a pass cut short by the deadline goes on from the page it reached.
|
|
614
|
+
const search = async (task) => {
|
|
615
|
+
const { kind } = task;
|
|
616
|
+
let page;
|
|
617
|
+
try {
|
|
618
|
+
page = await searchPage(kind, task.start);
|
|
619
|
+
}
|
|
620
|
+
catch (error) {
|
|
621
|
+
if (!task.start || !isRefused(error))
|
|
622
|
+
throw error;
|
|
623
|
+
return restart({ ...task, start: undefined }, `the search for ${kind}s`);
|
|
624
|
+
}
|
|
625
|
+
let start = page.has_more && page.next_cursor ? page.next_cursor : undefined;
|
|
626
|
+
if (task.full) {
|
|
627
|
+
await searchTop(page);
|
|
628
|
+
if (isCut(page))
|
|
629
|
+
warn({
|
|
630
|
+
code: "provider_error",
|
|
631
|
+
reason: "notion.search-capped",
|
|
632
|
+
message: `Notion's search gave only the most recently edited ${kind}s; a top-level one it left out is found once it is edited`,
|
|
633
|
+
});
|
|
634
|
+
if (start)
|
|
635
|
+
pass.tasks.splice(1, 0, { ...task, start });
|
|
636
|
+
return;
|
|
637
|
+
}
|
|
638
|
+
let reachedMark = false;
|
|
639
|
+
for (const result of page.results) {
|
|
640
|
+
if (since && Date.parse(result.last_edited_time) < Date.parse(since)) {
|
|
641
|
+
start = undefined;
|
|
642
|
+
reachedMark = true;
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
// Only what this pass saw before it began counts towards the next mark; later edits must stay inside it.
|
|
646
|
+
if (kind === "page")
|
|
647
|
+
pass.next = later(pass.next, result.last_edited_time);
|
|
648
|
+
if (result.object === "page") {
|
|
649
|
+
if (!unlistedRow(result))
|
|
412
650
|
emit(await pageFile(result));
|
|
413
|
-
|
|
414
|
-
|
|
651
|
+
else if (known.has(result.id))
|
|
652
|
+
gone(result.id);
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
const id = result.parent.database_id;
|
|
656
|
+
// A database outside the chosen folders is not read; one inside them is held already, or comes by a walk.
|
|
657
|
+
if (!roots.includes(WORKSPACE_ROOT) && !known.has(id))
|
|
658
|
+
continue;
|
|
659
|
+
if (!pass.tasks.some((item) => item.t === "database" && item.id === id))
|
|
660
|
+
queue({ t: "database", id });
|
|
661
|
+
}
|
|
662
|
+
// Search has no time filter to split a cut answer by, so edits past the cut fail the sync instead of passing.
|
|
663
|
+
if (isCut(page) && !reachedMark)
|
|
664
|
+
throw new CliError("provider_error", `Notion cut the search for edited ${kind}s short`, {
|
|
665
|
+
reason: "notion.search-incomplete",
|
|
666
|
+
...(page.request_status?.incomplete_reason
|
|
667
|
+
? { incomplete_reason: page.request_status.incomplete_reason }
|
|
668
|
+
: {}),
|
|
669
|
+
});
|
|
670
|
+
if (start)
|
|
671
|
+
pass.tasks.splice(1, 0, { t: "search", kind, start });
|
|
672
|
+
};
|
|
673
|
+
const searchTop = async (page) => {
|
|
674
|
+
for (const result of page.results) {
|
|
675
|
+
if (result.in_trash)
|
|
676
|
+
continue;
|
|
677
|
+
if (result.object === "data_source") {
|
|
415
678
|
const id = result.parent.database_id;
|
|
416
|
-
|
|
417
|
-
if (
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
679
|
+
const queued = pass.tasks.find((item) => item.t === "database" && item.id === id);
|
|
680
|
+
if (queued?.t === "database")
|
|
681
|
+
queued.walk = true;
|
|
682
|
+
else
|
|
683
|
+
queue({ t: "database", id, walk: true });
|
|
684
|
+
continue;
|
|
685
|
+
}
|
|
686
|
+
pass.next = later(pass.next, result.last_edited_time);
|
|
687
|
+
if (result.parent.type !== "workspace")
|
|
688
|
+
continue;
|
|
689
|
+
const file = await pageFile(result);
|
|
690
|
+
shown(file.id);
|
|
691
|
+
if (changedPage(file))
|
|
692
|
+
emit(file);
|
|
693
|
+
queue({ t: "walk", id: file.id });
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
const database = async (task) => {
|
|
697
|
+
const found = await readDatabase(task.id);
|
|
698
|
+
if (typeof found !== "object")
|
|
699
|
+
return;
|
|
700
|
+
const file = await databaseFile(found);
|
|
701
|
+
if (file.trashed)
|
|
702
|
+
gone(task.id);
|
|
703
|
+
else {
|
|
704
|
+
unlisted(found);
|
|
705
|
+
if (unheld(file))
|
|
706
|
+
emit(file);
|
|
707
|
+
}
|
|
708
|
+
if (task.walk && !file.trashed && file.parents[0] === WORKSPACE_ROOT) {
|
|
709
|
+
shown(file.id);
|
|
710
|
+
queue({ t: "walk", id: file.id });
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
const confirmEach = async (task) => {
|
|
714
|
+
while (task.ids.length) {
|
|
715
|
+
const file = known.get(task.ids[0]);
|
|
716
|
+
if (file) {
|
|
717
|
+
const listing = { files: [], gone: [], moved: [] };
|
|
718
|
+
await confirm([file], task.container, listing);
|
|
719
|
+
for (const found of [...listing.files, ...listing.moved])
|
|
720
|
+
if (changedPage(found))
|
|
721
|
+
emit(found);
|
|
722
|
+
for (const id of listing.gone)
|
|
424
723
|
gone(id);
|
|
425
|
-
else if (unheld(file))
|
|
426
|
-
emit(file);
|
|
427
724
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
reason: "notion.search-incomplete",
|
|
432
|
-
...(page.request_status?.incomplete_reason
|
|
433
|
-
? { incomplete_reason: page.request_status.incomplete_reason }
|
|
434
|
-
: {}),
|
|
435
|
-
});
|
|
436
|
-
} while (start);
|
|
725
|
+
task.ids.shift();
|
|
726
|
+
kept++;
|
|
727
|
+
}
|
|
437
728
|
};
|
|
729
|
+
// One page of rows per step. The next step reads from the last row's minute on, as Notion rounds created_time
|
|
730
|
+
// to the minute; only when a whole page shares one minute does it go on by the query's own position instead.
|
|
438
731
|
const rows = async (task) => {
|
|
439
|
-
let
|
|
732
|
+
let page;
|
|
440
733
|
try {
|
|
441
|
-
|
|
734
|
+
page = await rowsPage(task.database, task.source, task, task.full ? null : since);
|
|
442
735
|
}
|
|
443
736
|
catch (error) {
|
|
737
|
+
if (task.start && isRefused(error))
|
|
738
|
+
return restart({ ...task, start: undefined, seen: undefined }, `the rows of database ${task.database}`);
|
|
444
739
|
if (!isNotFound(error))
|
|
445
740
|
throw error;
|
|
446
741
|
cursor.sources = cursor.sources.filter((item) => item.source !== task.source);
|
|
447
742
|
const database = await readDatabase(task.database);
|
|
448
|
-
if (
|
|
743
|
+
if (isGone(database))
|
|
449
744
|
gone(task.database);
|
|
745
|
+
else if (typeof database === "object") {
|
|
746
|
+
unlisted(database);
|
|
747
|
+
const file = await databaseFile(database);
|
|
748
|
+
if (unheld(file))
|
|
749
|
+
emit(file);
|
|
750
|
+
}
|
|
450
751
|
return;
|
|
451
752
|
}
|
|
452
|
-
const
|
|
453
|
-
for (const row of window.rows) {
|
|
454
|
-
seen.add(row.id);
|
|
753
|
+
for (const row of page.rows)
|
|
455
754
|
emit(await pageFile(row));
|
|
456
|
-
|
|
457
|
-
if (task.full)
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
const listing = { files: [], gone: [], moved: [] };
|
|
471
|
-
await confirm(missing, task.database, listing);
|
|
472
|
-
for (const file of [...listing.files, ...listing.moved])
|
|
473
|
-
emit(file);
|
|
474
|
-
for (const id of listing.gone)
|
|
475
|
-
gone(id);
|
|
476
|
-
}
|
|
477
|
-
if (window.next)
|
|
478
|
-
pass.tasks.push({ ...task, after: window.next });
|
|
755
|
+
const next = [];
|
|
756
|
+
if (task.full && page.missing.length)
|
|
757
|
+
next.push({ t: "confirm", container: task.database, ids: page.missing.map((file) => file.id) });
|
|
758
|
+
if (page.next)
|
|
759
|
+
next.push({ ...task, after: page.next.after, start: page.next.start, seen: page.next.seen });
|
|
760
|
+
pass.tasks.splice(1, 0, ...next);
|
|
761
|
+
};
|
|
762
|
+
const child = async (task) => {
|
|
763
|
+
const file = await childDatabase(task.id);
|
|
764
|
+
if (!file || file.trashed)
|
|
765
|
+
return;
|
|
766
|
+
if (unheld(file))
|
|
767
|
+
emit(file);
|
|
768
|
+
queue({ t: "walk", id: file.id, kind: "database" });
|
|
479
769
|
};
|
|
480
770
|
const walkDatabase = async (id) => {
|
|
481
771
|
const database = await readDatabase(id);
|
|
482
|
-
if (
|
|
772
|
+
if (database === "unreadable")
|
|
773
|
+
return;
|
|
774
|
+
if (database === "gone" || database.in_trash) {
|
|
483
775
|
if (!roots.includes(id))
|
|
484
776
|
gone(id);
|
|
485
777
|
return;
|
|
486
778
|
}
|
|
779
|
+
unlisted(database);
|
|
487
780
|
for (const source of database.data_sources) {
|
|
488
781
|
addSource(id, source.id);
|
|
489
|
-
|
|
782
|
+
queue({ t: "rows", database: id, source: source.id, full: true });
|
|
490
783
|
}
|
|
491
784
|
};
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
785
|
+
// The engine walks again every folder it is sent, so a held page is sent only when Notion changed it.
|
|
786
|
+
const changedPage = (file) => unheld(file) || known.get(file.id)?.version !== file.version;
|
|
787
|
+
// A page's children a page of blocks per step, from where the last step stopped.
|
|
788
|
+
const walkPage = async (task) => {
|
|
789
|
+
let page;
|
|
790
|
+
try {
|
|
791
|
+
page = await blocksPage({ blocks: task.blocks ?? [{ id: task.id }], seen: task.seen ?? [] });
|
|
792
|
+
}
|
|
793
|
+
catch (error) {
|
|
794
|
+
if (task.blocks?.[0]?.start && isRefused(error))
|
|
795
|
+
return restart({ t: "walk", id: task.id, kind: "page" }, `the listing of page ${task.id}`);
|
|
796
|
+
throw error;
|
|
797
|
+
}
|
|
798
|
+
const { blocks, seen } = page;
|
|
799
|
+
for (const block of page.pages) {
|
|
800
|
+
const file = childPageFile(block, task.id);
|
|
801
|
+
if (changedPage(file))
|
|
802
|
+
emit(file);
|
|
803
|
+
const holds = [...known.values()].some((item) => item.parents[0] === block.id);
|
|
804
|
+
if (block.has_children || holds)
|
|
805
|
+
queue({ t: "walk", id: block.id, kind: "page" });
|
|
806
|
+
}
|
|
807
|
+
for (const id of page.databases)
|
|
808
|
+
queue({ t: "child", id, container: task.id });
|
|
809
|
+
if (blocks.length)
|
|
810
|
+
return void pass.tasks.splice(1, 0, { ...task, blocks, seen });
|
|
811
|
+
const listed = new Set(seen);
|
|
812
|
+
const missing = [...known.values()].filter((file) => file.parents[0] === task.id && !listed.has(file.id));
|
|
813
|
+
if (missing.length)
|
|
814
|
+
pass.tasks.splice(1, 0, { t: "confirm", container: task.id, ids: missing.map((file) => file.id) });
|
|
815
|
+
};
|
|
816
|
+
const walk = async (task) => {
|
|
817
|
+
if (task.id === WORKSPACE_ROOT)
|
|
818
|
+
return walkWorkspace();
|
|
819
|
+
if (!task.kind) {
|
|
820
|
+
task.kind = known.get(task.id)?.providerMetadata.kind ?? (await retrieve(task.id)).providerMetadata.kind;
|
|
821
|
+
kept++;
|
|
822
|
+
}
|
|
823
|
+
if (task.kind === "database")
|
|
824
|
+
return walkDatabase(task.id);
|
|
825
|
+
if (task.kind === "page")
|
|
826
|
+
return walkPage(task);
|
|
827
|
+
};
|
|
828
|
+
// Only a pass a 0.1.0 sync saved still holds the workspace as one step.
|
|
829
|
+
const walkWorkspace = async () => {
|
|
830
|
+
const listing = await listChildren(WORKSPACE_ROOT);
|
|
500
831
|
for (const file of listing.moved)
|
|
501
832
|
emit(file);
|
|
502
833
|
for (const file of listing.files) {
|
|
503
|
-
|
|
834
|
+
const page = file.providerMetadata.kind === "page";
|
|
835
|
+
if (page ? changedPage(file) : unheld(file))
|
|
504
836
|
emit(file);
|
|
505
|
-
if (file.providerMetadata.kind === "database")
|
|
506
|
-
|
|
837
|
+
if (page || file.providerMetadata.kind === "database")
|
|
838
|
+
queue({ t: "walk", id: file.id });
|
|
507
839
|
}
|
|
508
840
|
for (const gid of listing.gone)
|
|
509
841
|
gone(gid);
|
|
510
842
|
};
|
|
511
843
|
let done = 0;
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
continue;
|
|
844
|
+
timed = true;
|
|
845
|
+
try {
|
|
846
|
+
while (pass.tasks.length && requests < maxRequests) {
|
|
847
|
+
const task = pass.tasks[0];
|
|
848
|
+
try {
|
|
849
|
+
if (task.t === "search")
|
|
850
|
+
await search(task);
|
|
851
|
+
else if (task.t === "database")
|
|
852
|
+
await database(task);
|
|
853
|
+
else if (task.t === "confirm")
|
|
854
|
+
await confirmEach(task);
|
|
855
|
+
else if (task.t === "rows")
|
|
856
|
+
await rows(task);
|
|
857
|
+
else if (task.t === "child")
|
|
858
|
+
await child(task);
|
|
859
|
+
else
|
|
860
|
+
await walk(task);
|
|
530
861
|
}
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
862
|
+
catch (error) {
|
|
863
|
+
// The task is done again from its start next time: what it emitted is sent twice, which the engine allows.
|
|
864
|
+
if (error === OUT_OF_TIME) {
|
|
865
|
+
// cli-core ends the command at the deadline, so a run that saves nothing would only be asked again.
|
|
866
|
+
if (!done && !kept && containers.size === resolved)
|
|
867
|
+
throw new CliError("timeout", "the time left was too short for one step of the Notion sync; run it again with a longer --timeout", { reason: "notion.timeout-too-short" });
|
|
868
|
+
break;
|
|
869
|
+
}
|
|
870
|
+
const failure = isCliError(error) ? error : new CliError("provider_error", String(error));
|
|
871
|
+
const id = task.t === "rows" ? task.database : task.t === "search" || task.t === "confirm" ? undefined : task.id;
|
|
872
|
+
if (isItemError(failure)) {
|
|
873
|
+
warn({
|
|
874
|
+
...(id ? { id } : {}),
|
|
875
|
+
code: failure.code,
|
|
876
|
+
reason: "notion.left-out",
|
|
877
|
+
message: `left out of this sync: ${failure.message}`,
|
|
878
|
+
});
|
|
879
|
+
pass.tasks.shift();
|
|
880
|
+
done++;
|
|
881
|
+
continue;
|
|
882
|
+
}
|
|
883
|
+
// With nothing done yet the error is the answer; otherwise the work so far is kept and the next sync goes on.
|
|
884
|
+
if (!done)
|
|
885
|
+
throw failure;
|
|
886
|
+
warn({
|
|
887
|
+
...(id ? { id } : {}),
|
|
888
|
+
code: failure.code,
|
|
889
|
+
reason: "notion.stopped-early",
|
|
890
|
+
message: `the sync stopped here and goes on next time: ${failure.message}`,
|
|
891
|
+
...(typeof failure.details.retry_after_ms === "number"
|
|
892
|
+
? { retry_after_ms: failure.details.retry_after_ms }
|
|
893
|
+
: {}),
|
|
894
|
+
});
|
|
895
|
+
break;
|
|
896
|
+
}
|
|
897
|
+
pass.tasks.shift();
|
|
898
|
+
done++;
|
|
543
899
|
}
|
|
544
|
-
|
|
545
|
-
|
|
900
|
+
}
|
|
901
|
+
finally {
|
|
902
|
+
timed = false;
|
|
546
903
|
}
|
|
547
904
|
if (pass.tasks.length)
|
|
548
|
-
cursor.pass = pass;
|
|
905
|
+
cursor.pass = { ...pass, parents: Object.fromEntries(containers) };
|
|
549
906
|
else {
|
|
550
907
|
cursor.mark = later(cursor.mark, pass.next);
|
|
551
908
|
if (pass.walk)
|
|
@@ -568,14 +925,53 @@ export const notionSource = (notion, options = {}) => {
|
|
|
568
925
|
add(user, `property:${name}`);
|
|
569
926
|
return [...found.values()];
|
|
570
927
|
};
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
928
|
+
const content = async (id, maxChars) => {
|
|
929
|
+
containers = new Map();
|
|
930
|
+
const page = await reads.retrievePage(id);
|
|
931
|
+
if (page.in_trash)
|
|
932
|
+
throw new CliError("not_found", "the page is in Notion's trash");
|
|
933
|
+
const file = await pageFile(page);
|
|
934
|
+
const body = await pageText(reads, id, (block, code) => {
|
|
935
|
+
if (code === "permission_error")
|
|
936
|
+
warn({
|
|
937
|
+
id: block,
|
|
938
|
+
code,
|
|
939
|
+
reason: "notion.left-out",
|
|
940
|
+
message: `a block of page ${id} was left out of its text: the token may not read it`,
|
|
577
941
|
});
|
|
578
|
-
|
|
942
|
+
});
|
|
943
|
+
const fields = frontMatter(page.properties);
|
|
944
|
+
const metadata = {
|
|
945
|
+
...file,
|
|
946
|
+
providerMetadata: {
|
|
947
|
+
...file.providerMetadata,
|
|
948
|
+
frontMatter: fields,
|
|
949
|
+
people: people(page),
|
|
950
|
+
...(body.missing ? { missingBlocks: body.missing } : {}),
|
|
951
|
+
},
|
|
952
|
+
};
|
|
953
|
+
const searchable = [file.name, ...frontMatterLines(fields), body.markdown].filter(Boolean).join("\n\n");
|
|
954
|
+
if (maxChars !== undefined && searchable.length > maxChars)
|
|
955
|
+
throw new CliError("validation_error", "the page is longer than the text limit", {
|
|
956
|
+
reason: "source.text-limit",
|
|
957
|
+
text_limit: maxChars,
|
|
958
|
+
});
|
|
959
|
+
return { metadata, markdown: body.markdown, text: withoutSignedLinks(searchable).text, tabs: [] };
|
|
960
|
+
};
|
|
961
|
+
let account;
|
|
962
|
+
const identity = async () => {
|
|
963
|
+
const self = await reads.self();
|
|
964
|
+
if (!self.bot?.workspace_id)
|
|
965
|
+
throw new CliError("invalid_response", "Notion did not name the token's workspace", {
|
|
966
|
+
reason: "notion.no-workspace",
|
|
967
|
+
});
|
|
968
|
+
return `${self.bot.workspace_id}:${self.id}`;
|
|
969
|
+
};
|
|
970
|
+
return {
|
|
971
|
+
// One sync run asks once: the CLI checks the account before it takes the lock, and the engine asks again.
|
|
972
|
+
identity() {
|
|
973
|
+
account ??= identity();
|
|
974
|
+
return account;
|
|
579
975
|
},
|
|
580
976
|
async metadata(id) {
|
|
581
977
|
fresh();
|
|
@@ -583,7 +979,21 @@ export const notionSource = (notion, options = {}) => {
|
|
|
583
979
|
},
|
|
584
980
|
async children(id) {
|
|
585
981
|
fresh();
|
|
586
|
-
|
|
982
|
+
if (id === WORKSPACE_ROOT)
|
|
983
|
+
return (await listChildren(id)).files;
|
|
984
|
+
const files = [];
|
|
985
|
+
let cursor;
|
|
986
|
+
do {
|
|
987
|
+
const page = await childrenPage(id, cursor);
|
|
988
|
+
for (const file of page.files)
|
|
989
|
+
files.push(file);
|
|
990
|
+
cursor = page.cursor;
|
|
991
|
+
} while (cursor);
|
|
992
|
+
return files;
|
|
993
|
+
},
|
|
994
|
+
async childrenPage(id, cursor) {
|
|
995
|
+
fresh();
|
|
996
|
+
return childrenPage(id, cursor);
|
|
587
997
|
},
|
|
588
998
|
async startToken() {
|
|
589
999
|
const top = await reads.search({
|
|
@@ -595,40 +1005,9 @@ export const notionSource = (notion, options = {}) => {
|
|
|
595
1005
|
return encodeCursor(cursor);
|
|
596
1006
|
},
|
|
597
1007
|
changes,
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
if (page.in_trash)
|
|
602
|
-
throw new CliError("not_found", "the page is in Notion's trash");
|
|
603
|
-
const file = await pageFile(page);
|
|
604
|
-
const body = await pageText(reads, id);
|
|
605
|
-
const fields = frontMatter(page.properties);
|
|
606
|
-
const metadata = {
|
|
607
|
-
...file,
|
|
608
|
-
providerMetadata: {
|
|
609
|
-
...file.providerMetadata,
|
|
610
|
-
frontMatter: fields,
|
|
611
|
-
people: people(page),
|
|
612
|
-
...(body.missing ? { missingBlocks: body.missing } : {}),
|
|
613
|
-
},
|
|
614
|
-
};
|
|
615
|
-
const searchable = [file.name, ...frontMatterLines(fields), body.markdown].filter(Boolean).join("\n\n");
|
|
616
|
-
if (maxChars !== undefined && searchable.length > maxChars)
|
|
617
|
-
throw new CliError("validation_error", "the page is longer than the text limit", {
|
|
618
|
-
reason: "source.text-limit",
|
|
619
|
-
text_limit: maxChars,
|
|
620
|
-
});
|
|
621
|
-
const content = {
|
|
622
|
-
metadata,
|
|
623
|
-
markdown: body.markdown,
|
|
624
|
-
text: withoutSignedLinks(searchable).text,
|
|
625
|
-
tabs: [],
|
|
626
|
-
};
|
|
627
|
-
return content;
|
|
628
|
-
},
|
|
629
|
-
async bytes() {
|
|
630
|
-
throw new CliError("validation_error", "a Notion page has no file to download");
|
|
631
|
-
},
|
|
1008
|
+
content,
|
|
1009
|
+
// A page has no file of its own; its Markdown is the nearest thing to one.
|
|
1010
|
+
bytes: async (id) => new TextEncoder().encode((await content(id)).markdown),
|
|
632
1011
|
};
|
|
633
1012
|
};
|
|
634
1013
|
//# sourceMappingURL=source.js.map
|