diff --git a/packages/download-notion/src/notionPull.ts b/packages/download-notion/src/notionPull.ts index 7966011..f403190 100644 --- a/packages/download-notion/src/notionPull.ts +++ b/packages/download-notion/src/notionPull.ts @@ -225,9 +225,6 @@ export async function notionPull(options: NotionPullOptions): Promise { group("Stage 2: Filtering pages...") filterTree(objectsTree, options.conversion.statusTag) - - // TODO: allObjectsMap should not be needed, instead, getting the ancestors should be handled by the objectsTree - const allObjectsMap = objectsToObjectsMap(objectsTree.data) endGroup() @@ -253,16 +250,12 @@ export async function notionPull(options: NotionPullOptions): Promise { ? removePathExtension( getAncestorPageOrDatabaseFilepath( image, - allObjectsMap, + objectsTree, newFilesManager ) ) : options.conversion.imageNamingStrategy == "default-flat" - ? getAncestorPageOrDatabaseFilename( - image, - allObjectsMap, - newFilesManager - ) + ? getAncestorPageOrDatabaseFilename(image, objectsTree, newFilesManager) : "" ) const pages = objectsTree.getPages().map((page) => new NotionPage(page)) diff --git a/packages/download-notion/src/objects_utils.ts b/packages/download-notion/src/objects_utils.ts index 5ed02b5..c087b21 100644 --- a/packages/download-notion/src/objects_utils.ts +++ b/packages/download-notion/src/objects_utils.ts @@ -6,7 +6,9 @@ import { import { NotionCacheClient } from "notion-cache-client" import { IdWithType, + NotionObjectTree, NotionObjectTreeNode, + NotionObjectsData, objectTreeToObjectIds, } from "notion-downloader" @@ -49,11 +51,7 @@ export async function getObjectTypeFromClient( } } -export type NotionObjectsData = { - page: Record - database: Record - block: Record -} +// TODO: Consider a "build Object Tree" method with a generic `Client` interface export async function getAllObjectsInObjectsTree( objectsTree: NotionObjectTreeNode, client: NotionCacheClient @@ -81,23 +79,23 @@ export async function getAllObjectsInObjectsTree( return objects } -type ObjectsMap = Record< - string, - PageObjectResponse | DatabaseObjectResponse | BlockObjectResponse -> - -export function getPageAncestorId(id: string, flatObjectsMap: ObjectsMap) { - const parent = flatObjectsMap[id]?.parent - if (!parent || parent.type === "workspace") { +export function getPageAncestorId(id: string, objectTree: NotionObjectTree) { + const parentId = objectTree.getParentId(id) + if (!parentId) { return null } - if (parent.type === "page_id") { - return parent.page_id + const parent = objectTree.getObject(parentId) + if (!parent) { + return null + } + + if (parent.object === "page") { + return parent.id } - if (parent.type === "database_id") { - return getPageAncestorId(parent.database_id, flatObjectsMap) + if (parent.object === "database") { + return getPageAncestorId(parent.id, objectTree) } - if (parent.type === "block_id") { - return getPageAncestorId(parent.block_id, flatObjectsMap) + if (parent.object === "block") { + return getPageAncestorId(parent.id, objectTree) } } diff --git a/packages/download-notion/src/utils.ts b/packages/download-notion/src/utils.ts index ca83e93..3635ff6 100644 --- a/packages/download-notion/src/utils.ts +++ b/packages/download-notion/src/utils.ts @@ -1,6 +1,7 @@ import crypto from "crypto" import * as Path from "path" import fs from "fs-extra" +import { NotionObjectTree } from "notion-downloader" import { FilesManager } from "./FilesManager" import { NotionImage } from "./NotionImage" @@ -52,7 +53,7 @@ export function filenameFromPath(path: string) { } export function getAncestorPageOrDatabaseFilepath( image: NotionImage, - objectsMap: PlainObjectsMap, + objectsTree: NotionObjectTree, filesManager: FilesManager ): string { if (image.object == "page") { @@ -62,7 +63,7 @@ export function getAncestorPageOrDatabaseFilepath( } // It's a block. Ancestor is page - const ancestorPageId = getPageAncestorId(image.id, objectsMap) + const ancestorPageId = getPageAncestorId(image.id, objectsTree) if (!ancestorPageId) { throw new Error("Ancestor page not found for image " + image.id) } @@ -71,11 +72,11 @@ export function getAncestorPageOrDatabaseFilepath( export function getAncestorPageOrDatabaseFilename( image: NotionImage, - objectsMap: PlainObjectsMap, + objectsTree: NotionObjectTree, filesManager: FilesManager ): string { return filenameFromPath( - getAncestorPageOrDatabaseFilepath(image, objectsMap, filesManager) + getAncestorPageOrDatabaseFilepath(image, objectsTree, filesManager) ) } diff --git a/packages/notion-downloader/src/index.ts b/packages/notion-downloader/src/index.ts index 291d421..af7e2a3 100644 --- a/packages/notion-downloader/src/index.ts +++ b/packages/notion-downloader/src/index.ts @@ -9,6 +9,7 @@ export { BlockObjectTreeNode, NotionObjectTree, NotionObjectResponse, + NotionObjectsData, } from "./notion-object-tree" export { cacheOptionsSchema } from "./schema" diff --git a/packages/notion-downloader/src/notion-object-tree.ts b/packages/notion-downloader/src/notion-object-tree.ts index ccaa5fc..719672d 100644 --- a/packages/notion-downloader/src/notion-object-tree.ts +++ b/packages/notion-downloader/src/notion-object-tree.ts @@ -156,6 +156,12 @@ export class NotionObjectTree { this.idToNodeMap.set(id, newNode) } + getParentId(id: string): string | null { + const node = this.getNodeById(id) + if (!node) return null + return node.parent + } + getObject( id: string ):