From b59a1d6b4392a4d423cb224243644da36df8c85d Mon Sep 17 00:00:00 2001 From: Francisco Moretti Date: Fri, 11 Oct 2024 09:22:43 +0100 Subject: [PATCH] download as mdx --- packages/notion-downloader/src/config/schema.ts | 4 ++++ packages/notion-downloader/src/getFileTreeMap.ts | 6 ++++-- .../src/notionObjects/NotionObjectUtils.ts | 8 ++++++-- .../src/notionObjects/NotionPage.ts | 13 +++++++++---- packages/notion-downloader/src/notionPull.ts | 3 ++- .../notion-downloader/test/utils/get-config.test.ts | 1 + 6 files changed, 26 insertions(+), 9 deletions(-) diff --git a/packages/notion-downloader/src/config/schema.ts b/packages/notion-downloader/src/config/schema.ts index a8cf79f..eea23b5 100644 --- a/packages/notion-downloader/src/config/schema.ts +++ b/packages/notion-downloader/src/config/schema.ts @@ -150,6 +150,9 @@ export type FilterType = z.infer export const pathOptionsSchema = z.union([z.string(), filepathSchema]) +export const MarkdownExtension = z.enum(["md", "mdx"]) +export type MarkdownExtension = z.infer + const filterSchema = z.object({ propertyName: z.string(), propertyValue: z.string(), @@ -161,6 +164,7 @@ export type Filter = z.infer export const conversionSchema = z.object({ skip: z.boolean().default(false), overwrite: z.boolean().default(false), + markdownExtension: MarkdownExtension.default("md"), slugProperty: z.string().optional(), filters: z.array(filterSchema).default([]), diff --git a/packages/notion-downloader/src/getFileTreeMap.ts b/packages/notion-downloader/src/getFileTreeMap.ts index 416b4a0..f669bc4 100644 --- a/packages/notion-downloader/src/getFileTreeMap.ts +++ b/packages/notion-downloader/src/getFileTreeMap.ts @@ -1,6 +1,7 @@ import { ObjectType } from "notion-cache-client" import { NotionObjectResponse, NotionObjectTree } from "notion-tree" +import { MarkdownExtension } from "./config/schema" import { LayoutStrategyGroup } from "./createStrategies" import { FilesManager, copyRecord } from "./files/FilesManager" import { LayoutStrategy } from "./layoutStrategy/LayoutStrategy" @@ -21,7 +22,8 @@ export function getFileTreeMap( existingFilesManager: FilesManager, newFilesManager: FilesManager, - filesInMemory: FileBuffersMemory + filesInMemory: FileBuffersMemory, + markdownExtension: MarkdownExtension ) { const nodeAction = ( objectResponse: NotionObjectResponse, @@ -30,7 +32,7 @@ export function getFileTreeMap( databaseIsRoot: boolean } ) => { - const notionObject = getNotionObject(objectResponse) + const notionObject = getNotionObject(objectResponse, markdownExtension) // New level path is created by objects that can contain files as children const newLevelPath = diff --git a/packages/notion-downloader/src/notionObjects/NotionObjectUtils.ts b/packages/notion-downloader/src/notionObjects/NotionObjectUtils.ts index 5364568..03018b1 100644 --- a/packages/notion-downloader/src/notionObjects/NotionObjectUtils.ts +++ b/packages/notion-downloader/src/notionObjects/NotionObjectUtils.ts @@ -1,14 +1,18 @@ import { ObjectType } from "notion-cache-client" import { NotionObjectResponse } from "notion-tree" +import { MarkdownExtension } from "../config/schema" import { NotionBlock } from "./NotionBlock" import { NotionBlockImage } from "./NotionBlockImage" import { NotionDatabase } from "./NotionDatabase" import { NotionPage } from "./NotionPage" -export function getNotionObject(response: NotionObjectResponse) { +export function getNotionObject( + response: NotionObjectResponse, + markdownExtension: MarkdownExtension +) { if (response.object == ObjectType.enum.page) { - return new NotionPage(response) + return new NotionPage(response, markdownExtension) } else if (response.object == ObjectType.enum.database) { return new NotionDatabase(response) } else if (response.object == ObjectType.enum.block) { diff --git a/packages/notion-downloader/src/notionObjects/NotionPage.ts b/packages/notion-downloader/src/notionObjects/NotionPage.ts index 1929901..c5dc6ef 100644 --- a/packages/notion-downloader/src/notionObjects/NotionPage.ts +++ b/packages/notion-downloader/src/notionObjects/NotionPage.ts @@ -1,8 +1,7 @@ import { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints" import { ObjectType } from "notion-cache-client" -import { AssetType, FileType, TextType } from "../config/schema" -import { error } from "../log" +import { FileType, MarkdownExtension, TextType } from "../config/schema" import { stringifyProperty } from "../properties/toPlainText" import { PageProperty } from "../properties/types" import { NotionObject } from "./NotionObject" @@ -11,10 +10,16 @@ export class NotionPage implements NotionObject { // TODO: Can this, Database and Image Extend the PageObjectResponse instead of using as metadata? public metadata: PageObjectResponse public fileType: FileType = TextType.enum.markdown - public extension: string = "md" + // TODO: This object has too many responsibilities. NotionPage should only be for handy access to PageObjectResponse. + // TODO: File based stuff only matters in some parts of the application, therefore it should be in another class. + public extension: MarkdownExtension - public constructor(metadata: PageObjectResponse) { + public constructor( + metadata: PageObjectResponse, + markdownExtension: MarkdownExtension = MarkdownExtension.enum.md + ) { this.metadata = metadata + this.extension = markdownExtension } public get id(): string { diff --git a/packages/notion-downloader/src/notionPull.ts b/packages/notion-downloader/src/notionPull.ts index 87a55d9..151399b 100644 --- a/packages/notion-downloader/src/notionPull.ts +++ b/packages/notion-downloader/src/notionPull.ts @@ -220,7 +220,8 @@ export async function notionPull(options: NotionPullOptions): Promise { layoutStrategies, existingFilesManager, newFilesManager, - filesInMemory + filesInMemory, + options.conversion.markdownExtension ) await updateAssetFilePathsForMarkdown(objectsTree, newFilesManager) diff --git a/packages/notion-downloader/test/utils/get-config.test.ts b/packages/notion-downloader/test/utils/get-config.test.ts index bb27851..6ba73c0 100644 --- a/packages/notion-downloader/test/utils/get-config.test.ts +++ b/packages/notion-downloader/test/utils/get-config.test.ts @@ -49,6 +49,7 @@ test("get config", async () => { skip: false, overwrite: false, filters: [], + markdownExtension: "md", pageLinkHasExtension: true, outputPaths: "./content", markdownPrefixes: "",