Skip to content

Commit

Permalink
propagate the NotionObjecTree interface
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoMoretti committed Sep 4, 2024
1 parent c988a66 commit 46b2ace
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 32 deletions.
11 changes: 2 additions & 9 deletions packages/download-notion/src/notionPull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,6 @@ export async function notionPull(options: NotionPullOptions): Promise<void> {
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()

Expand All @@ -253,16 +250,12 @@ export async function notionPull(options: NotionPullOptions): Promise<void> {
? 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))
Expand Down
36 changes: 17 additions & 19 deletions packages/download-notion/src/objects_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
import { NotionCacheClient } from "notion-cache-client"
import {
IdWithType,
NotionObjectTree,
NotionObjectTreeNode,
NotionObjectsData,
objectTreeToObjectIds,
} from "notion-downloader"

Expand Down Expand Up @@ -49,11 +51,7 @@ export async function getObjectTypeFromClient(
}
}

export type NotionObjectsData = {
page: Record<string, PageObjectResponse>
database: Record<string, DatabaseObjectResponse>
block: Record<string, BlockObjectResponse>
}
// TODO: Consider a "build Object Tree" method with a generic `Client` interface
export async function getAllObjectsInObjectsTree(
objectsTree: NotionObjectTreeNode,
client: NotionCacheClient
Expand Down Expand Up @@ -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)
}
}
9 changes: 5 additions & 4 deletions packages/download-notion/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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") {
Expand All @@ -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)
}
Expand All @@ -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)
)
}

Expand Down
1 change: 1 addition & 0 deletions packages/notion-downloader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
BlockObjectTreeNode,
NotionObjectTree,
NotionObjectResponse,
NotionObjectsData,
} from "./notion-object-tree"

export { cacheOptionsSchema } from "./schema"
Expand Down
6 changes: 6 additions & 0 deletions packages/notion-downloader/src/notion-object-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand Down

0 comments on commit 46b2ace

Please sign in to comment.