Skip to content

Commit

Permalink
download as mdx
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoMoretti committed Oct 11, 2024
1 parent cebf5d1 commit b59a1d6
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions packages/notion-downloader/src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ export type FilterType = z.infer<typeof filterTypeSchema>

export const pathOptionsSchema = z.union([z.string(), filepathSchema])

export const MarkdownExtension = z.enum(["md", "mdx"])
export type MarkdownExtension = z.infer<typeof MarkdownExtension>

const filterSchema = z.object({
propertyName: z.string(),
propertyValue: z.string(),
Expand All @@ -161,6 +164,7 @@ export type Filter = z.infer<typeof filterSchema>
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([]),

Expand Down
6 changes: 4 additions & 2 deletions packages/notion-downloader/src/getFileTreeMap.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -21,7 +22,8 @@ export function getFileTreeMap(
existingFilesManager: FilesManager,

newFilesManager: FilesManager,
filesInMemory: FileBuffersMemory
filesInMemory: FileBuffersMemory,
markdownExtension: MarkdownExtension
) {
const nodeAction = (
objectResponse: NotionObjectResponse,
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
13 changes: 9 additions & 4 deletions packages/notion-downloader/src/notionObjects/NotionPage.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion packages/notion-downloader/src/notionPull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ export async function notionPull(options: NotionPullOptions): Promise<void> {
layoutStrategies,
existingFilesManager,
newFilesManager,
filesInMemory
filesInMemory,
options.conversion.markdownExtension
)

await updateAssetFilePathsForMarkdown(objectsTree, newFilesManager)
Expand Down
1 change: 1 addition & 0 deletions packages/notion-downloader/test/utils/get-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ test("get config", async () => {
skip: false,
overwrite: false,
filters: [],
markdownExtension: "md",
pageLinkHasExtension: true,
outputPaths: "./content",
markdownPrefixes: "",
Expand Down

0 comments on commit b59a1d6

Please sign in to comment.