Skip to content

Commit

Permalink
simplify imageset
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoMoretti committed Aug 25, 2024
1 parent bac4c12 commit 9497cdc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
8 changes: 5 additions & 3 deletions packages/download-notion/src/MakeImagePersistencePlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,18 @@ export function makeImagePersistencePlan(
imageSet: ImageSet,
imageBlockId: string,
imageOutputRootPath: string,
imagePrefix: string
imagePrefix: string,
directoryConrtainingMardown: string,
pageSlug: string
): OutputPaths {
const outputFileName = getOutputImageFileName(
options,
imageSet,
imageBlockId,
imageSet.pageInfo?.slug
pageSlug
)
const { filePathToUseInMarkdown, primaryFileOutputPath } = getImagePaths(
imageSet.pageInfo!.directoryContainingMarkdown,
directoryConrtainingMardown,
outputFileName,
imageOutputRootPath,
imagePrefix
Expand Down
12 changes: 6 additions & 6 deletions packages/download-notion/src/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export type OutputPaths = {
export type ImageSet = {
primaryUrl: string
caption?: string
pageInfo?: IDocuNotionContextPageInfo
fileData?: FileData // New property to hold ext, mime, and buffer data
}

Expand Down Expand Up @@ -128,7 +127,6 @@ async function processImageBlock(
const minimalImageSet = parseImageBlock(imageBlockimage)
const imageSet: ImageSet = {
...minimalImageSet,
pageInfo: context.pageInfo,
}
// enhance: it would much better if we could split the changes to markdown separately from actual reading/writing,
// so that this wasn't part of the markdown-creation loop. It's already almost there; we just need to
Expand All @@ -146,7 +144,9 @@ async function processImageBlock(
imageSet,
block.id,
context.imageHandler.imageOutputPath,
context.imageHandler.imagePrefix
context.imageHandler.imagePrefix,
context.pageInfo.directoryContainingMarkdown,
context.pageInfo.slug
)
await saveImage(
outputPaths?.primaryFileOutputPath!,
Expand Down Expand Up @@ -273,15 +273,15 @@ export async function processCoverImage(
},
}

imageSet.pageInfo = context.pageInfo

// TODO: Include here the NamingStrategy
const outputPaths = makeImagePersistencePlan(
context.options,
imageSet,
page.id,
context.imageHandler.imageOutputPath,
context.imageHandler.imagePrefix
context.imageHandler.imagePrefix,
context.pageInfo.directoryContainingMarkdown,
context.pageInfo.slug
)
await saveImage(
outputPaths?.primaryFileOutputPath!,
Expand Down
52 changes: 40 additions & 12 deletions packages/download-notion/src/makeImagePersistencePlan.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ const testImageSet: ImageSet = {
}

test("primary file with explicit file output path and prefix", () => {
const directoryConrtainingMardown = "/pathToParentSomewhere/"
const pageSlug = "my-page"
const outputPaths = makeImagePersistencePlan(
optionsUsingDefaultNaming,
testImageSet,
"ABC-123",
"./static/notion_imgs",
"/notion_imgs"
"/notion_imgs",
directoryConrtainingMardown,
pageSlug
)
const expectedFileName = "my-page.ABC-123.png"
expect(outputPaths?.outputFileName).toBe(`${expectedFileName}`)
Expand All @@ -62,12 +66,16 @@ test("primary file with explicit file output path and prefix", () => {
)
})
test("primary file with defaults for image output path and prefix", () => {
const directoryConrtainingMardown = "/pathToParentSomewhere/"
const pageSlug = "my-page"
const outputPaths = makeImagePersistencePlan(
optionsUsingDefaultNaming,
testImageSet,
"ABC-123",
"",
""
"",
directoryConrtainingMardown,
pageSlug
)
const expectedFileName = "my-page.ABC-123.png"
expect(outputPaths?.outputFileName).toBe(`${expectedFileName}`)
Expand All @@ -79,12 +87,16 @@ test("primary file with defaults for image output path and prefix", () => {
expect(outputPaths?.filePathToUseInMarkdown).toBe(`./${expectedFileName}`)
})
test("falls back to getting file extension from url if not in fileType", () => {
const directoryConrtainingMardown = "/pathToParentSomewhere/"
const pageSlug = "my-page"
const outputPaths = makeImagePersistencePlan(
optionsUsingDefaultNaming,
testImageSet,
"ABC-123",
"",
""
"",
directoryConrtainingMardown,
pageSlug
)
expect(outputPaths?.outputFileName).toBe("my-page.ABC-123.png")
})
Expand All @@ -95,17 +107,17 @@ test("falls back to getting file extension from url if not in fileType", () => {
test("handles encoded characters", () => {
const imageSet: ImageSet = {
...testImageSet,
pageInfo: {
directoryContainingMarkdown: "/pathToParentSomewhere/",
slug: "my-page%281%29",
},
}
const directoryConrtainingMardown = "/pathToParentSomewhere/"
const pageSlug = "my-page%281%29"
const outputPaths = makeImagePersistencePlan(
optionsUsingDefaultNaming,
imageSet,
"ABC-123",
"",
""
"",
directoryConrtainingMardown,
pageSlug
)
expect(outputPaths.primaryFileOutputPath).toBe(
`/pathToParentSomewhere/my-page(1).ABC-123.png`
Expand All @@ -120,12 +132,16 @@ const optionsUsingHashNaming: NotionPullOptions = {
imageFileNameFormat: "content-hash",
}
test("hash naming", () => {
const directoryConrtainingMardown = "/pathToParentSomewhere/"
const pageSlug = "my-page%281%29"
const outputPaths = makeImagePersistencePlan(
optionsUsingHashNaming,
testImageSet,
"ABC-123",
"",
""
"",
directoryConrtainingMardown,
pageSlug
)
const expectedFileName = "fe3f26fd515b3cf299ac.png"
expect(outputPaths?.outputFileName).toBe(`${expectedFileName}`)
Expand All @@ -136,19 +152,25 @@ const optionsUsingLegacyNaming: NotionPullOptions = {
imageFileNameFormat: "legacy",
}
test("Legacy naming", () => {
const directoryConrtainingMardown = "/pathToParentSomewhere/"
const pageSlug = "my-page%281%29"
const outputPaths = makeImagePersistencePlan(
optionsUsingLegacyNaming,
testImageSet,
"ABC-123",
"./static/notion_imgs",
"/notion_imgs"
"/notion_imgs",
directoryConrtainingMardown,
pageSlug
)
const expectedHash = hashOfString(
"https://s3.us-west-2.amazonaws.com/primaryImage"
)
expect(outputPaths.outputFileName).toBe(`${expectedHash}.png`)
})
test("Legacy naming - properly extract UUID from old-style notion image url", () => {
const directoryConrtainingMardown = "/pathToParentSomewhere/"
const pageSlug = "my-page%281%29"
const imageSet: ImageSet = {
...testImageSet,
primaryUrl:
Expand All @@ -159,7 +181,9 @@ test("Legacy naming - properly extract UUID from old-style notion image url", ()
imageSet,
"ABC-123",
"./static/notion_imgs",
"/notion_imgs"
"/notion_imgs",
directoryConrtainingMardown,
pageSlug
)
const expectedHash = hashOfString("e1058f46-4d2f-4292-8388-4ad393383439")
expect(outputPaths?.outputFileName).toBe(`${expectedHash}.png`)
Expand All @@ -170,12 +194,16 @@ test("Legacy naming - properly extract UUID from new-style (Sept 2023) notion im
primaryUrl:
"https://prod-files-secure.s3.us-west-2.amazonaws.com/d9a2b712-cf69-4bd6-9d65-87a4ceeacca2/d1bcdc8c-b065-4e40-9a11-392aabeb220e/Untitled.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=AKIAT73L2G45EIPT3X45%2F20230915%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20230915T161258Z&X-Amz-Expires=3600&X-Amz-Signature=28fca48e65fba86d539c3c4b7676fce1fa0857aa194f7b33dd4a468ecca6ab24&X-Amz-SignedHeaders=host&x-id=GetObject",
}
const directoryConrtainingMardown = "/pathToParentSomewhere/"
const pageSlug = "my-page%281%29"
const outputPaths = makeImagePersistencePlan(
optionsUsingLegacyNaming,
imageSet,
"ABC-123",
"./static/notion_imgs",
"/notion_imgs"
"/notion_imgs",
directoryConrtainingMardown,
pageSlug
)
const expectedHash = hashOfString("d1bcdc8c-b065-4e40-9a11-392aabeb220e")
expect(outputPaths?.outputFileName).toBe(`${expectedHash}.png`)
Expand Down

0 comments on commit 9497cdc

Please sign in to comment.