@kamilio/wire 0.1.76 → 0.1.78

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.
@@ -1 +1 @@
1
- {"version":3,"file":"notion-sync.d.ts","sourceRoot":"","sources":["../src/notion-sync.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,WAAW,EAAE,kBAAkB,EAAE,cAAc,EAAE,eAAe,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAEzK,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,WAAW,CAAC;AAErD,MAAM,MAAM,cAAc,GAAG,SAAS,EAAE,EAAE,CAAC;AAC3C,MAAM,MAAM,WAAW,GAAG,QAAQ,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,UAAU,CAAC;IAAC,SAAS,EAAE,cAAc,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AACzI,MAAM,MAAM,UAAU,GAAG,QAAQ,CAAC;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,UAAU,CAAC;IAAC,QAAQ,EAAE,SAAS,UAAU,EAAE,CAAA;CAAE,CAAC,CAAC;AACtG,MAAM,MAAM,eAAe,GAAG,QAAQ,CAAC;IAAE,OAAO,EAAE,UAAU,CAAC;IAAC,IAAI,EAAE,SAAS,SAAS,EAAE,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,SAAS,CAAA;CAAE,CAAC,CAAC;AAC9H,MAAM,MAAM,kBAAkB,GAAG,QAAQ,CAAC;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,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eds: embeds }), page_id: pageId, markdown, blocks: sidecarBlocksFromNotionTree(tree), ...(mentions === undefined ? {} : { user_mentions: mentions }), ...(Object.keys(emailMentions).length === 0 ? {} : { email_mentions: emailMentions }) };
272
272
  }
273
- function lineIndent(line) { return Math.floor(line.match(/^ */)[0].length / 2); }
274
- function stripIndent(line) { return line.replace(/^ +/, ""); }
273
+ function lineIndent(line) {
274
+ let columns = 0;
275
+ for (const character of line.match(/^[ \t]*/)[0])
276
+ columns += character === "\t" ? 4 - columns % 4 : 1;
277
+ return Math.floor(columns / 2);
278
+ }
279
+ function stripIndent(line) { return line.replace(/^[ \t]+/, ""); }
275
280
  function tableCellText(value) {
276
281
  return value.trim().replace(/(^|[^\\])<br>/g, "$1\n").replace(/\\<br>/g, "<br>");
277
282
  }
@@ -323,7 +328,7 @@ function colonFenceBody(lines, index) {
323
328
  }
324
329
  function listContinuation(lines, index, baseIndent, indent, firstLine) {
325
330
  const content = [firstLine];
326
- while (index < lines.length && lines[index].trim() !== "" && baseIndent + lineIndent(lines[index]) === indent + 1 && !markdownBlockStart(stripIndent(lines[index]))) {
331
+ while (index < lines.length && lines[index].trim() !== "" && baseIndent + lineIndent(lines[index]) > indent && !markdownBlockStart(stripIndent(lines[index]))) {
327
332
  content.push(stripIndent(lines[index]));
328
333
  index += 1;
329
334
  }
@@ -417,12 +422,21 @@ function parseLines(lines, baseIndent = 0) {
417
422
  const codeFence = /^(`{3,})(.*)$/.exec(stripped);
418
423
  if (codeFence !== null) {
419
424
  const fence = codeFence[1];
420
- const fenceIndent = line.match(/^ */)[0].length;
425
+ let fenceIndent = 0;
426
+ for (const character of line.match(/^[ \t]*/)[0])
427
+ fenceIndent += character === "\t" ? 4 - fenceIndent % 4 : 1;
421
428
  const language = codeFence[2].trim();
422
429
  const body = [];
423
430
  index += 1;
424
431
  while (index < lines.length && !stripIndent(lines[index]).startsWith(fence)) {
425
- body.push(lines[index].slice(fenceIndent));
432
+ const bodyLine = lines[index];
433
+ let offset = 0;
434
+ let columns = 0;
435
+ while (columns < fenceIndent && (bodyLine[offset] === " " || bodyLine[offset] === "\t")) {
436
+ columns += bodyLine[offset] === "\t" ? 4 - columns % 4 : 1;
437
+ offset += 1;
438
+ }
439
+ body.push(" ".repeat(Math.max(0, columns - fenceIndent)) + bodyLine.slice(offset));
426
440
  index += 1;
427
441
  }
428
442
  index += 1;
@@ -612,20 +626,24 @@ export function buildNotionCreateOperations(blocks, pageId, spaceId, userId, cur
612
626
  const blockId = formatBlockId(createId().replaceAll("-", ""));
613
627
  let parentId = pageId;
614
628
  if (block.type === "table_row")
615
- parentId = lastTableId;
629
+ parentId = lastTableId ?? pageId;
616
630
  else if (block.indent > 0) {
617
- const parent = lastAtIndent.get(block.indent - 1);
631
+ const parentIndent = Math.max(...[...lastAtIndent.keys()].filter((indent) => indent < block.indent));
632
+ const parent = lastAtIndent.get(parentIndent);
618
633
  if (parent === undefined)
619
634
  throw new Error(`Indented Notion Markdown block has no parent at indent ${block.indent - 1}`);
620
635
  parentId = parent;
621
636
  }
622
- if (block.type !== "table_row" && block.indent === 0)
637
+ if (parentId === pageId)
623
638
  topLevelIds.push(blockId);
624
639
  const parentTable = "block";
625
640
  operations.push({ pointer: pointer(blockId, spaceId), path: [], command: "set", args: parserBlockToNotionBlock(block, blockId, parentId, parentTable, spaceId, userId, currentTime, block.type === "table_row" ? tableColumnOrder : undefined) });
626
641
  const after = lastChildByParent.get(parentId);
627
642
  operations.push({ pointer: pointer(parentId, spaceId), path: ["content"], command: after === undefined ? "listBefore" : "listAfter", args: after === undefined ? { id: blockId } : { id: blockId, after } });
628
643
  lastChildByParent.set(parentId, blockId);
644
+ for (const indent of lastAtIndent.keys())
645
+ if (indent >= block.indent)
646
+ lastAtIndent.delete(indent);
629
647
  if (["bulleted_list", "numbered_list", "to_do", "quote", "callout", "toggle", "header", "sub_header", "sub_sub_header", "table", "page", "column_list", "column", "transclusion_container"].includes(block.type))
630
648
  lastAtIndent.set(block.indent, blockId);
631
649
  if (block.type === "table") {
@@ -786,71 +804,78 @@ function deleteRemoteSubtreeOperations(node, parentId, spaceId) {
786
804
  { pointer: pointer(parentId, spaceId), path: ["content"], command: "listRemove", args: { id: node.id } },
787
805
  ];
788
806
  }
789
- function diffNotionChildLists(remoteParent, localChildren, ambient, summary, columnOrder, localParent) {
790
- const operations = [];
791
- const remoteChildren = [...remoteParent.children];
792
- const nextColumnOrder = localParent?.type === "table" ? tableColumnOrder(remoteParent.block, localParent) : columnOrder;
793
- const remoteHash = (index) => notionBlockContentHash(remoteChildren[index].block, nextColumnOrder);
794
- const localHash = (index) => notionBlockContentHash(localChildren[index].block, nextColumnOrder);
807
+ function commonSubsequence(remote, local) {
808
+ const lengths = Array.from({ length: remote.length + 1 }, () => new Uint32Array(local.length + 1));
809
+ for (let remoteIndex = remote.length - 1; remoteIndex >= 0; remoteIndex--) {
810
+ for (let localIndex = local.length - 1; localIndex >= 0; localIndex--)
811
+ lengths[remoteIndex][localIndex] = remote[remoteIndex] === local[localIndex] ? lengths[remoteIndex + 1][localIndex + 1] + 1 : Math.max(lengths[remoteIndex + 1][localIndex], lengths[remoteIndex][localIndex + 1]);
812
+ }
813
+ const pairs = [];
795
814
  let remoteIndex = 0;
796
815
  let localIndex = 0;
797
- let previousId;
798
- while (remoteIndex < remoteChildren.length && localIndex < localChildren.length) {
799
- const remoteNode = remoteChildren[remoteIndex];
800
- const localNode = localChildren[localIndex];
801
- if (remoteHash(remoteIndex) === localHash(localIndex)) {
802
- operations.push(...diffNotionChildLists(remoteNode, localNode.children, ambient, summary, nextColumnOrder, localNode.block));
803
- previousId = remoteNode.id;
816
+ while (remoteIndex < remote.length && localIndex < local.length) {
817
+ if (remote[remoteIndex] === local[localIndex])
818
+ pairs.push([remoteIndex++, localIndex++]);
819
+ else if (lengths[remoteIndex + 1][localIndex] > lengths[remoteIndex][localIndex + 1])
804
820
  remoteIndex += 1;
821
+ else
805
822
  localIndex += 1;
806
- continue;
823
+ }
824
+ return pairs;
825
+ }
826
+ function diffNotionChildLists(remoteParent, localChildren, ambient, summary, columnOrder, localParent) {
827
+ const operations = [];
828
+ const remoteChildren = remoteParent.children;
829
+ const nextColumnOrder = localParent?.type === "table" ? tableColumnOrder(remoteParent.block, localParent) : columnOrder;
830
+ // Alignment keys, strictest first: same content, same type, then any two blocks Notion can convert in place.
831
+ const remoteKeys = remoteChildren.map((child) => [notionBlockContentHash(child.block, nextColumnOrder), child.block["type"], child.block["type"] === "table" ? "remote table" : ""]);
832
+ const localKeys = localChildren.map((child) => [notionBlockContentHash(child.block, nextColumnOrder), child.block.type === "toggle" ? "text" : child.block.type, child.block.type === "table" ? "local table" : ""]);
833
+ const remoteByLocal = new Map();
834
+ const align = (remotes, locals, level) => {
835
+ if (level === 3)
836
+ return;
837
+ const pairs = commonSubsequence(remotes.map((index) => remoteKeys[index][level]), locals.map((index) => localKeys[index][level]));
838
+ let remoteStart = 0;
839
+ let localStart = 0;
840
+ for (const [remoteEnd, localEnd] of [...pairs, [remotes.length, locals.length]]) {
841
+ align(remotes.slice(remoteStart, remoteEnd), locals.slice(localStart, localEnd), level + 1);
842
+ if (remoteEnd < remotes.length)
843
+ remoteByLocal.set(locals[localEnd], remotes[remoteEnd]);
844
+ remoteStart = remoteEnd + 1;
845
+ localStart = localEnd + 1;
807
846
  }
808
- const remoteNodeAppearsLaterLocally = localChildren.some((child, index) => index > localIndex && notionBlockContentHash(child.block, nextColumnOrder) === remoteHash(remoteIndex));
809
- const movedRemoteIndex = remoteNodeAppearsLaterLocally ? remoteChildren.findIndex((child, index) => index > remoteIndex && notionBlockContentHash(child.block, nextColumnOrder) === localHash(localIndex)) : -1;
810
- if (movedRemoteIndex !== -1) {
811
- const moved = remoteChildren[movedRemoteIndex];
847
+ };
848
+ align(remoteChildren.map((_, index) => index), localChildren.map((_, index) => index), 0);
849
+ const kept = new Set(remoteByLocal.values());
850
+ let previousId;
851
+ for (const [localIndex, localNode] of localChildren.entries()) {
852
+ let remoteIndex = remoteByLocal.get(localIndex);
853
+ if (remoteIndex === undefined) {
854
+ remoteIndex = remoteKeys.findIndex((keys, index) => !kept.has(index) && keys[0] === localKeys[localIndex][0]);
855
+ if (remoteIndex === -1) {
856
+ const built = buildNotionCreateSubtreeOperations(localNode, remoteParent.id, ambient, nextColumnOrder);
857
+ operations.push(...positionRootCreateOperations(built.operations, remoteParent.id, ambient.spaceId, previousId));
858
+ summary.inserted += localSubtreeSize(localNode);
859
+ previousId = built.topLevelIds[0];
860
+ continue;
861
+ }
862
+ kept.add(remoteIndex);
863
+ const moved = remoteChildren[remoteIndex];
812
864
  operations.push({ pointer: pointer(remoteParent.id, ambient.spaceId), path: ["content"], command: previousId === undefined ? "listBefore" : "listAfter", args: previousId === undefined ? { id: moved.id } : { id: moved.id, after: previousId } });
813
- remoteChildren.splice(movedRemoteIndex, 1);
814
- remoteChildren.splice(remoteIndex, 0, moved);
815
865
  summary.moved += 1;
816
- continue;
817
866
  }
818
- if (localIndex + 1 < localChildren.length && remoteHash(remoteIndex) === localHash(localIndex + 1)) {
819
- const built = buildNotionCreateSubtreeOperations(localNode, remoteParent.id, ambient, nextColumnOrder);
820
- operations.push(...positionRootCreateOperations(built.operations, remoteParent.id, ambient.spaceId, previousId));
821
- summary.inserted += localSubtreeSize(localNode);
822
- previousId = built.topLevelIds[0];
823
- localIndex += 1;
824
- continue;
825
- }
826
- if (remoteIndex + 1 < remoteChildren.length && remoteHash(remoteIndex + 1) === localHash(localIndex)) {
827
- operations.push(...deleteRemoteSubtreeOperations(remoteNode, remoteParent.id, ambient.spaceId));
828
- summary.deleted += remoteSubtreeSize(remoteNode);
829
- remoteIndex += 1;
830
- continue;
831
- }
832
- const before = operations.length;
833
- operations.push(...emitUpdates(remoteNode.block, localNode.block, remoteNode.id, ambient.spaceId, nextColumnOrder));
834
- operations.push(...diffNotionChildLists(remoteNode, localNode.children, ambient, summary, nextColumnOrder, localNode.block));
835
- if (operations.length > before)
867
+ const remoteNode = remoteChildren[remoteIndex];
868
+ const updates = remoteKeys[remoteIndex][0] === localKeys[localIndex][0] ? [] : emitUpdates(remoteNode.block, localNode.block, remoteNode.id, ambient.spaceId, nextColumnOrder);
869
+ if (updates.length > 0)
836
870
  summary.updated += 1;
871
+ operations.push(...updates, ...diffNotionChildLists(remoteNode, localNode.children, ambient, summary, nextColumnOrder, localNode.block));
837
872
  previousId = remoteNode.id;
838
- remoteIndex += 1;
839
- localIndex += 1;
840
- }
841
- while (localIndex < localChildren.length) {
842
- const localNode = localChildren[localIndex];
843
- const built = buildNotionCreateSubtreeOperations(localNode, remoteParent.id, ambient, nextColumnOrder);
844
- operations.push(...positionRootCreateOperations(built.operations, remoteParent.id, ambient.spaceId, previousId ?? remoteChildren[remoteChildren.length - 1]?.id));
845
- summary.inserted += localSubtreeSize(localNode);
846
- previousId = built.topLevelIds[0];
847
- localIndex += 1;
848
873
  }
849
- while (remoteIndex < remoteChildren.length) {
850
- const child = remoteChildren[remoteIndex];
874
+ for (const [remoteIndex, child] of remoteChildren.entries()) {
875
+ if (kept.has(remoteIndex))
876
+ continue;
851
877
  operations.push(...deleteRemoteSubtreeOperations(child, remoteParent.id, ambient.spaceId));
852
878
  summary.deleted += remoteSubtreeSize(child);
853
- remoteIndex += 1;
854
879
  }
855
880
  return operations;
856
881
  }