Skip to content

Commit

Permalink
big cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoMoretti committed Aug 29, 2024
1 parent bf0c0d9 commit 5bc3e30
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 100 deletions.
78 changes: 43 additions & 35 deletions packages/download-notion/src/FilesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
FilesMap,
FilesMapData,
ObjectPrefixDict,
recordMapWithPathPrefix,
recordMapWithPrefix,
recordWithPrefix,
} from "./FilesMap"
import { NotionObject } from "./NotionObject"

Expand Down Expand Up @@ -63,32 +64,38 @@ export class FilesManager {
return this.filesMap.exists(type, id)
}

public get(relativeTo: PathType, type: FileType, id: string): FileRecord {
public get(pathType: PathType, type: FileType, id: string): FileRecord {
const recordFromDirectory = this.filesMap.get(type, id)

if (relativeTo === "output") {
return this.filesMap.recordToRootRelativePath(
recordFromDirectory,
this.outputDirectories[type]
)
} else {
if (pathType === "output") {
return recordWithPrefix(recordFromDirectory, this.outputDirectories[type])
} else if (pathType === "markdown") {
return recordWithPrefix(recordFromDirectory, this.markdownPrefixes[type])
} else if (pathType === "base") {
return recordFromDirectory
} else {
// error out
throw new Error(`Invalid path type: ${pathType}`)
}
}

public set(
relativeTo: PathType,
pathType: PathType,
type: FileType,
id: string,
record: FileRecord
): void {
const recordToSet =
relativeTo === "output"
? this.filesMap.recordToDirectoriesRelativePath(
record,
this.outputDirectories[type]
)
: record
let recordToSet: FileRecord
if (pathType === "output") {
recordToSet = recordWithPrefix(record, this.outputDirectories[type])
} else if (pathType === "markdown") {
recordToSet = recordWithPrefix(record, this.markdownPrefixes[type])
} else if (pathType === "base") {
recordToSet = record
} else {
// error out
throw new Error(`Invalid path type: ${pathType}`)
}
this.filesMap.set(type, id, recordToSet)
}

Expand All @@ -97,37 +104,38 @@ export class FilesManager {
}

public getAllOfType(
relativeTo: PathType,
pathType: PathType,
type: FileType
): Record<string, FileRecord> {
const records = this.filesMap.getAllOfType(type)
if (relativeTo === "output") {
return recordMapWithPathPrefix(records, this.outputDirectories[type])
} else {
if (pathType === "output") {
return recordMapWithPrefix(records, this.outputDirectories[type])
} else if (pathType === "markdown") {
return recordMapWithPrefix(records, this.markdownPrefixes[type])
} else if (pathType === "base") {
return records
} else {
// error out
throw new Error(`Invalid path type: ${pathType}`)
}
}

public getAll(relativeTo: PathType): FilesMapData {
public getAll(pathType: PathType): FilesMapData {
const filesMapData = this.filesMap.getAll()

if (relativeTo === "output") {
if (pathType === "output" || pathType === "markdown") {
const prefixes =
pathType === "output" ? this.outputDirectories : this.markdownPrefixes
return {
page: recordMapWithPathPrefix(
filesMapData.page,
this.outputDirectories.page
),
database: recordMapWithPathPrefix(
filesMapData.database,
this.outputDirectories.database
),
image: recordMapWithPathPrefix(
filesMapData.image,
this.outputDirectories.image
),
page: recordMapWithPrefix(filesMapData.page, prefixes.page),
database: recordMapWithPrefix(filesMapData.database, prefixes.database),
image: recordMapWithPrefix(filesMapData.image, prefixes.image),
}
} else {
} else if (pathType === "base") {
return filesMapData
} else {
// error out
throw new Error(`Invalid path type: ${pathType}`)
}
}
}
44 changes: 18 additions & 26 deletions packages/download-notion/src/FilesMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,8 @@ export class FilesMap {
return JSON.stringify(this.map)
}

recordToRootRelativePath(fileRecord: FileRecord, prefix: string): FileRecord {
return recordWithPathPrefix(fileRecord, prefix)
}

recordToDirectoriesRelativePath(
fileRecord: FileRecord,
prefix: string
): FileRecord {
return recordWithoutPathPrefix(fileRecord, prefix)
}

// TODO: Move relative to root and to directories to FilesManager
static allToRootRelativePath(
// TODO: Move this functions to utilities outside of this class
static allToPathWithPrefix(
filesMap: FilesMap,
objectsDirectories: ObjectPrefixDict
): FilesMap {
Expand All @@ -82,16 +71,16 @@ export class FilesMap {
const { page, database, image } = filesMap.getAll()

const fromRootFilesMapData: FilesMapData = {
page: recordMapWithPathPrefix(page, pagesDirectory),
database: recordMapWithPathPrefix(database, databasesDirectory),
image: recordMapWithPathPrefix(image, imagesDirectory),
page: recordMapWithPrefix(page, pagesDirectory),
database: recordMapWithPrefix(database, databasesDirectory),
image: recordMapWithPrefix(image, imagesDirectory),
}
const fromRootFilesMap = new FilesMap()
fromRootFilesMap.map = fromRootFilesMapData
return fromRootFilesMap
}

static allToDirectoriesRelativePath(
static allToPathWithoutPrefix(
filesMap: FilesMap,
objectsDirectories: ObjectPrefixDict
): FilesMap {
Expand All @@ -103,45 +92,48 @@ export class FilesMap {
const { page, database, image } = filesMap.getAll()

const toDirectoriesFilesMapData: FilesMapData = {
page: recordMapWithoutPathPrefix(page, pagesDirectory),
database: recordMapWithoutPathPrefix(database, databasesDirectory),
image: recordMapWithoutPathPrefix(image, imagesDirectory),
page: recordMapWithoutPrefix(page, pagesDirectory),
database: recordMapWithoutPrefix(database, databasesDirectory),
image: recordMapWithoutPrefix(image, imagesDirectory),
}
const toDirectoriesFilesMap = new FilesMap()
toDirectoriesFilesMap.map = toDirectoriesFilesMapData
return toDirectoriesFilesMap
}
}

export function recordMapWithPathPrefix(
export function recordMapWithPrefix(
recordMap: Record<string, FileRecord>,
prefix: string
): Record<string, FileRecord> {
return Object.fromEntries(
Object.entries(recordMap).map(
([id, record]: [string, FileRecord]): [string, FileRecord] => [
id,
recordWithPathPrefix(record, prefix),
recordWithPrefix(record, prefix),
]
)
)
}

export function recordMapWithoutPathPrefix(
export function recordMapWithoutPrefix(
recordMap: Record<string, FileRecord>,
prefix: string
): Record<string, FileRecord> {
return Object.fromEntries(
Object.entries(recordMap).map(
([id, record]: [string, FileRecord]): [string, FileRecord] => [
id,
recordWithoutPathPrefix(record, prefix),
recordWithoutPrefix(record, prefix),
]
)
)
}

function recordWithPathPrefix(record: FileRecord, prefix: string): FileRecord {
export function recordWithPrefix(
record: FileRecord,
prefix: string
): FileRecord {
const newPath = addPathPrefix(record.path, prefix)

return {
Expand All @@ -150,7 +142,7 @@ function recordWithPathPrefix(record: FileRecord, prefix: string): FileRecord {
}
}

function recordWithoutPathPrefix(
export function recordWithoutPrefix(
record: FileRecord,
prefix: string
): FileRecord {
Expand Down
11 changes: 8 additions & 3 deletions packages/download-notion/src/notionPull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ export async function notionPull(options: NotionPullOptions): Promise<void> {
image: options.imgOutputPath,
}

const markdownPrefixes: ObjectPrefixDict = {
page: "",
database: "",
image: options.imgPrefixInMarkdown || options.imgOutputPath || ".",
}

const filesMapFilePath =
options.cwd.replace(/\/+$/, "") + "/" + FILES_MAP_FILE_PATH
// TODO: Implement a logic to reset the state if previous save directories vs current directories are different
Expand All @@ -140,9 +146,11 @@ export async function notionPull(options: NotionPullOptions): Promise<void> {
const existingFilesManager = new FilesManager({
initialFilesMap: previousFilesMap,
outputDirectories: objectsDirectories,
markdownPrefixes: markdownPrefixes,
})
const newFilesManager = new FilesManager({
outputDirectories: objectsDirectories,
markdownPrefixes: markdownPrefixes,
})

// TODO: Path strategies should simply be handled by the FilesManager
Expand Down Expand Up @@ -250,9 +258,6 @@ export async function notionPull(options: NotionPullOptions): Promise<void> {
existingFilesManager,
newFilesManager,
imageNamingStrategy,
imageFilePathStrategy,
options,
imageMarkdownPathStrategy,
pages,
databases,
})
Expand Down
44 changes: 10 additions & 34 deletions packages/download-notion/src/processImages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,28 @@ async function processImage({
existingFilesManager,
newFilesManager,
imageNamingStrategy,
imageFilePathStrategy,
imageMarkdownPathStrategy,
options,
}: {
image: NotionImage
existingFilesManager: FilesManager
newFilesManager: FilesManager
imageNamingStrategy: ImageNamingStrategy
// TODO: The ones below should be replaced by filesmanager
imageFilePathStrategy: PathStrategy
imageMarkdownPathStrategy: PathStrategy
options: NotionPullOptions
}) {
if (existingFilesManager.isObjectNew(image)) {
await image.read()
const imageFilename = imageNamingStrategy.getFileName(image)

// TODO: These paths strategies should be handled inside FilesManager getting the root path
const imageFileOutputPath = imageFilePathStrategy.getPath(imageFilename)

await image.save(imageFileOutputPath)

const pathFromImageDirectory = removePathPrefix(
imageFileOutputPath,
options.imgOutputPath
)
newFilesManager.set("base", "image", image.id, {
path: pathFromImageDirectory,
path: imageFilename,
lastEditedTime: image.lastEditedTime,
})
const markdownPath = imageMarkdownPathStrategy.getPath(imageFilename)

const imageFileOutputPath = newFilesManager.get(
"output",
"image",
image.id
).path

await image.save(imageFileOutputPath)
const markdownPath = newFilesManager.get("markdown", "image", image.id).path
updateImageUrlToMarkdownImagePath(image.file, markdownPath)
} else {
const imageRecordFromDirectory = existingFilesManager.get(
Expand All @@ -71,18 +62,12 @@ export async function processImages({
existingFilesManager,
newFilesManager,
imageNamingStrategy,
imageFilePathStrategy,
options,
imageMarkdownPathStrategy,
pages,
databases,
}: {
options: NotionPullOptions
existingFilesManager: FilesManager
newFilesManager: FilesManager
imageNamingStrategy: ImageNamingStrategy
imageFilePathStrategy: PathStrategy
imageMarkdownPathStrategy: PathStrategy
imageBlocks: (BlockObjectResponse & { type: "image" })[]
pages: NotionPage[]
databases: NotionDatabase[]
Expand All @@ -95,9 +80,6 @@ export async function processImages({
existingFilesManager,
newFilesManager,
imageNamingStrategy,
imageFilePathStrategy,
imageMarkdownPathStrategy,
options,
})
}

Expand All @@ -111,9 +93,6 @@ export async function processImages({
existingFilesManager,
newFilesManager,
imageNamingStrategy,
imageFilePathStrategy,
imageMarkdownPathStrategy,
options,
})
}

Expand All @@ -126,9 +105,6 @@ export async function processImages({
existingFilesManager,
newFilesManager,
imageNamingStrategy,
imageFilePathStrategy,
imageMarkdownPathStrategy,
options,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/download-notion/test/FilesMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe("FilesMap", () => {
}
filesMap.set("page", "page-id", pageRecord)

const rootRelativeFilesMap = FilesMap.allToRootRelativePath(
const rootRelativeFilesMap = FilesMap.allToPathWithPrefix(
filesMap,
objectsDirectories
)
Expand All @@ -126,7 +126,7 @@ describe("FilesMap", () => {
}
filesMap.set("page", "page-id", pageRecord)

const dirRelativeFilesMap = FilesMap.allToDirectoriesRelativePath(
const dirRelativeFilesMap = FilesMap.allToPathWithoutPrefix(
filesMap,
objectsDirectories
)
Expand Down

0 comments on commit 5bc3e30

Please sign in to comment.