Skip to content

Commit

Permalink
temporarily define a new image strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoMoretti committed Sep 1, 2024
1 parent f2813fb commit 39623d3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions downloader.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
markdownPrefixes: {
images: "/assets",
},
imageNamingStrategy: "default-flat",
pageLinkHasExtension: false,
slugProperty: "slug",
},
Expand Down
5 changes: 3 additions & 2 deletions packages/download-notion/src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ export const conversionSchema = z.object({
images: z.string().default(""),
}),
layoutStrategy: z
.enum(["HierarchicalNamedLayoutStrategy", "FlatGuidLayoutStrategy"])
.enum(["HierarchicalNamedLayoutStrategy", "FlatLayoutStrategy"])
.default("HierarchicalNamedLayoutStrategy"),
namingStrategy: z
.enum(["github-slug", "notion-slug", "guid", "title"])
.default("github-slug"),
imageNamingStrategy: z
.enum(["default", "content-hash", "legacy"])
// TODO: Make image naming strategies independent of their path building
.enum(["default", "content-hash", "legacy", "default-flat"])
.optional()
.default("default"),
})
Expand Down
3 changes: 2 additions & 1 deletion packages/download-notion/src/getOutputImageFileName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { LegacyImageNamingStrategy } from "./LegacyImageNamingStrategy"
import { NotionImage } from "./NotionImage"

export function getStrategy(
format: "legacy" | "content-hash" | "default",
format: "legacy" | "content-hash" | "default" | "default-flat",
getPageAncestorName: (image: NotionImage) => string
): ImageNamingStrategy {
switch (format) {
Expand All @@ -14,6 +14,7 @@ export function getStrategy(
case "content-hash":
return new ContentHashImageNamingStrategy()
case "default":
case "default-flat":
return new DefaultImageNamingStrategy(getPageAncestorName)
default:
throw new Error(`Unknown image file name format: ${format}`)
Expand Down
21 changes: 17 additions & 4 deletions packages/download-notion/src/notionPull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { processImages } from "./processImages"
import { getMarkdownForPage } from "./transform"
import {
convertToUUID,
getAncestorPageOrDatabaseFilename,
getAncestorPageOrDatabaseFilepath,
sanitizeMarkdownOutputPath,
saveDataToJson,
Expand Down Expand Up @@ -121,7 +122,7 @@ export async function notionPull(options: NotionPullOptions): Promise<void> {
? new GuidNamingStrategy()
: new TitleNamingStrategy()
const layoutStrategy =
options.conversion.layoutStrategy === "FlatGuidLayoutStrategy"
options.conversion.layoutStrategy === "FlatLayoutStrategy"
? new FlatLayoutStrategy(namingStrategy)
: new HierarchicalLayoutStrategy(namingStrategy)

Expand Down Expand Up @@ -229,9 +230,21 @@ export async function notionPull(options: NotionPullOptions): Promise<void> {
options.conversion.imageNamingStrategy || "default",
// TODO: A new strategy could be with ancestor filename `getAncestorPageOrDatabaseFilename`
(image) =>
removePathExtension(
getAncestorPageOrDatabaseFilepath(image, allObjectsMap, newFilesManager)
)
options.conversion.imageNamingStrategy == "default"
? removePathExtension(
getAncestorPageOrDatabaseFilepath(
image,
allObjectsMap,
newFilesManager
)
)
: options.conversion.imageNamingStrategy == "default-flat"
? getAncestorPageOrDatabaseFilename(
image,
allObjectsMap,
newFilesManager
)
: ""
)

// -------- FILES ---------
Expand Down
3 changes: 2 additions & 1 deletion packages/download-notion/src/processImages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async function processImage({
}) {
if (existingFilesManager.isObjectNew(image)) {
await image.read()
// TODO: Write here a layout naming strategy for images. Name is ok, but path is not.
const imageFilename = imageNamingStrategy.getFileName(image)
newFilesManager.set("base", "image", image.id, {
path: imageFilename,
Expand All @@ -46,7 +47,7 @@ async function processImage({
} else {
copyRecord(existingFilesManager, newFilesManager, "image", image.id)
const imageRecordFromDirectory = newFilesManager.get(
"base",
"markdown",
"image",
image.id
)
Expand Down

0 comments on commit 39623d3

Please sign in to comment.