Skip to content

Commit

Permalink
renamed NotionPage2
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoMoretti committed Aug 10, 2024
1 parent 29410fe commit d93d306
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 38 deletions.
6 changes: 3 additions & 3 deletions packages/cli/src/FlatLayoutStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LayoutStrategy } from "./LayoutStrategy"
import { NotionDatabase } from "./NotionDatabase"
import { NotionPage2 } from "./NotionPage2"
import { NotionPage } from "./NotionPage"
import { NamingStrategy } from "./namingStrategy"

// This strategy creates a flat list of files that have notion-id for file names.
Expand All @@ -22,14 +22,14 @@ export class FlatLayoutStrategy extends LayoutStrategy {

public newLevel(
context: string,
pageOrDatabaseName: NotionPage2 | NotionDatabase
pageOrDatabaseName: NotionPage | NotionDatabase
): string {
// In this strategy, we ignore context and don't create any directories to match the levels.
// Just return the following for the benefit of logging.
return context
}

public getPathForPage2(page: NotionPage2, currentPath: string): string {
public getPathForPage2(page: NotionPage, currentPath: string): string {
// In this strategy, we don't care about the location or the title
return "/" + this.namingStrategy.nameForPage(page) + ".md"
}
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/HierarchicalLayoutStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import sanitize from "sanitize-filename"

import { LayoutStrategy } from "./LayoutStrategy"
import { NotionDatabase } from "./NotionDatabase"
import { NotionPage2 } from "./NotionPage2"
import { NotionPage } from "./NotionPage"
import { NamingStrategy } from "./namingStrategy"

// This strategy gives us a file tree that mirrors that of notion.
Expand All @@ -20,19 +20,19 @@ export class HierarchicalLayoutStrategy extends LayoutStrategy {

public newLevel(
context: string,
pageOrDatabaseName: NotionPage2 | NotionDatabase
pageOrDatabaseName: NotionPage | NotionDatabase
): string {
const extendPath =
pageOrDatabaseName.metadata.object === "page"
? this.namingStrategy.nameForPage(pageOrDatabaseName as NotionPage2)
? this.namingStrategy.nameForPage(pageOrDatabaseName as NotionPage)
: this.namingStrategy.nameForDatabase(
pageOrDatabaseName as NotionDatabase
)
const path = ("/" + context + "/" + extendPath).replaceAll("//", "/")
return path
}

public getPathForPage2(page: NotionPage2, currentPath: string): string {
public getPathForPage2(page: NotionPage, currentPath: string): string {
const sanitizedName = this.namingStrategy.nameForPage(page)

const context = ("/" + currentPath + "/").replaceAll("//", "/")
Expand Down
9 changes: 3 additions & 6 deletions packages/cli/src/LayoutStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { NotionDatabase } from "./NotionDatabase"
import { NotionPage2 } from "./NotionPage2"
import { NotionPage } from "./NotionPage"

// Here a fuller name would be File Tree Layout Strategy. That is,
// as we walk the Notion outline and create files, where do we create them, what do we name them, etc.
export abstract class LayoutStrategy {
public abstract newLevel(
// TODO: Fix incosnsistency in naming `context` and `currentPath`
context: string,
pageOrDatabaseName: NotionPage2 | NotionDatabase
pageOrDatabaseName: NotionPage | NotionDatabase
): string

public abstract getPathForPage2(
page: NotionPage2,
currentPath: string
): string
public abstract getPathForPage2(page: NotionPage, currentPath: string): string
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export type NotionPageConfig = {
slugProperty?: string
}

export class NotionPage2 {
export class NotionPage {
public metadata: PageObjectResponse
public config: CustomPropertiesConfig

Expand Down Expand Up @@ -337,7 +337,7 @@ export async function fromPageId(
pageId: string,
client: Client,
config?: NotionPageConfig
): Promise<NotionPage2> {
): Promise<NotionPage> {
const metadata = await client.pages.retrieve({
page_id: pageId,
})
Expand All @@ -347,5 +347,5 @@ export async function fromPageId(
}

//logDebug("notion metadata", JSON.stringify(metadata));
return new NotionPage2(metadata, config)
return new NotionPage(metadata, config)
}
4 changes: 2 additions & 2 deletions packages/cli/src/get-tree-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DatabaseObjectResponse } from "@notionhq/client/build/src/api-endpoints
import { FilesMap } from "./FilesMap"
import { LayoutStrategy } from "./LayoutStrategy"
import { NotionDatabase } from "./NotionDatabase"
import { NotionPage2, fromPageId, getPageContentInfo } from "./NotionPage2"
import { NotionPage, fromPageId, getPageContentInfo } from "./NotionPage"
import { getBlockChildren } from "./getBlockChildren"
import { error, info, warning } from "./log"
import { OutputCounts } from "./notionPull"
Expand All @@ -17,7 +17,7 @@ export async function getTreePages(
currentType: "page" | "database",
rootLevel: boolean,
client: Client,
pages: Array<NotionPage2>,
pages: Array<NotionPage>,
layoutStrategy: LayoutStrategy,
counts: OutputCounts,
filesMap: FilesMap
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/getFileTreeMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Client } from "@notionhq/client"

import { FilesMap } from "./FilesMap"
import { LayoutStrategy } from "./LayoutStrategy"
import { NotionPageConfig, getPageContentInfo } from "./NotionPage2"
import { NotionPageConfig, getPageContentInfo } from "./NotionPage"
import { getNotionDatabase, getNotionPage2 } from "./notionPull"

export async function getFileTreeMap(
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/src/namingStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { slug } from "github-slugger"
import sanitize from "sanitize-filename"

import { NotionDatabase } from "./NotionDatabase"
import { NotionPage2 } from "./NotionPage2"
import { NotionPage } from "./NotionPage"

export abstract class NamingStrategy {
public abstract nameForPage(page: NotionPage2): string
public abstract nameForPage(page: NotionPage): string
public abstract nameForDatabase(page: NotionDatabase): string
}

Expand All @@ -19,7 +19,7 @@ export abstract class SlugNamingStrategy extends NamingStrategy {
this.slugProperty = slugProperty || "Slug"
}

public nameForPage(page: NotionPage2): string {
public nameForPage(page: NotionPage): string {
// TODO This logic needs to be handled either here or in the page.
const explicitSlug = page.getPlainTextProperty(this.slugProperty, "")

Expand Down Expand Up @@ -61,7 +61,7 @@ export class NotionSlugNamingStrategy extends SlugNamingStrategy {
}

export class GuidNamingStrategy extends NamingStrategy {
public nameForPage(page: NotionPage2): string {
public nameForPage(page: NotionPage): string {
return page.id
}
public nameForDatabase(database: NotionDatabase): string {
Expand All @@ -70,7 +70,7 @@ export class GuidNamingStrategy extends NamingStrategy {
}

export class TitleNamingStrategy extends NamingStrategy {
public nameForPage(page: NotionPage2): string {
public nameForPage(page: NotionPage): string {
return page.nameOrTitle
}
public nameForDatabase(database: NotionDatabase): string {
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/src/notionPull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { FilesMap } from "./FilesMap"
import { FlatLayoutStrategy } from "./FlatLayoutStrategy"
import { HierarchicalLayoutStrategy } from "./HierarchicalLayoutStrategy"
import { NotionDatabase } from "./NotionDatabase"
import { NotionPage2, NotionPageConfig, fromPageId } from "./NotionPage2"
import { NotionPage, NotionPageConfig, fromPageId } from "./NotionPage"
import { IDocuNotionConfig, loadConfigAsync } from "./config/configuration"
import { pullOptionsSchema } from "./config/schema"
import { getBlockChildren } from "./getBlockChildren"
Expand Down Expand Up @@ -64,7 +64,7 @@ export async function getNotionPage2(
if (!isFullPage(pageResponse)) {
throw Error("Notion page response is not full for " + currentID)
}
const page = new NotionPage2(pageResponse, pageConfig)
const page = new NotionPage(pageResponse, pageConfig)
return page
}

Expand Down Expand Up @@ -213,7 +213,7 @@ export async function notionPull(options: DocuNotionOptions): Promise<void> {
pageConfig
)

const pagesPromises: Promise<NotionPage2>[] = Object.keys(filesMap.page).map(
const pagesPromises: Promise<NotionPage>[] = Object.keys(filesMap.page).map(
(id) => fromPageId(id, cachedNotionClient, pageConfig)
)

Expand Down Expand Up @@ -250,7 +250,7 @@ export async function notionPull(options: DocuNotionOptions): Promise<void> {
async function outputPages(
options: DocuNotionOptions,
config: IDocuNotionConfig,
pages: Array<NotionPage2>,
pages: Array<NotionPage>,
client: Client,
notionToMarkdown: NotionToMarkdown,
filesMap: FilesMap
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/plugins/internalLinks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "path"

import { NotionPage2 } from "../NotionPage2"
import { NotionPage } from "../NotionPage"
import { error, warning } from "../log"
import { IDocuNotionContext, IPlugin } from "./pluginTypes"

Expand Down Expand Up @@ -79,7 +79,7 @@ function convertInternalLink(
return `[${label}](${url})`
}

function convertLinkLabel(targetPage: NotionPage2, text: string): string {
function convertLinkLabel(targetPage: NotionPage, text: string): string {
// In Notion, if you just add a link to a page without linking it to any text, then in Notion
// you see the name of the page as the text of the link. But when Notion gives us that same
// link, it uses "link_to_page" as the text. So we have to look up the name of the page in
Expand All @@ -89,7 +89,7 @@ function convertLinkLabel(targetPage: NotionPage2, text: string): string {
}
function convertLinkHref(
context: IDocuNotionContext,
targetPage: NotionPage2,
targetPage: NotionPage,
url: string
): string {
let convertedLink = context.filesMap.page[targetPage.id]
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/plugins/pluginTestRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Client } from "@notionhq/client"
import { GetPageResponse } from "@notionhq/client/build/src/api-endpoints"
import { NotionToMarkdown } from "notion-to-md"

import { NotionPage2 } from "../NotionPage2"
import { NotionPage } from "../NotionPage"
import { NotionPageLegacy } from "../NotionPageLegacy"
import { IDocuNotionConfig } from "../config/configuration"
import { numberChildrenIfNumberedList } from "../getBlockChildren"
Expand All @@ -14,7 +14,7 @@ import { IDocuNotionContext } from "./pluginTypes"
export async function blocksToMarkdown(
config: IDocuNotionConfig,
blocks: NotionBlock[],
pages?: NotionPage2[],
pages?: NotionPage[],
// Notes on children:
// - These children will apply to each block in blocks. (could enhance but not needed yet)
// - If you are passing in children, it is probably because your parent block has has_children=true.
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/plugins/pluginTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NotionToMarkdown } from "notion-to-md"
import { ListBlockChildrenResponseResult } from "notion-to-md/build/types"

import { FilesMap } from "../FilesMap"
import { NotionPage2 } from "../NotionPage2"
import { NotionPage } from "../NotionPage"
import { ICounts, NotionBlock } from "../index"
import { DocuNotionOptions } from "../notionPull"

Expand Down Expand Up @@ -73,7 +73,7 @@ export type IDocuNotionContext = {
notionToMarkdown: NotionToMarkdown
pageInfo: IDocuNotionContextPageInfo
convertNotionLinkToLocalDocusaurusLink: (url: string) => string | undefined
pages: NotionPage2[]
pages: NotionPage[]
counts: ICounts
filesMap: FilesMap

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from "chalk"

import { NotionPage2 } from "./NotionPage2"
import { NotionPage } from "./NotionPage"
import { IDocuNotionConfig } from "./config/configuration"
import { error, info, logDebug, logDebugFn, verbose, warning } from "./log"
import {
Expand All @@ -12,7 +12,7 @@ import { NotionBlock } from "./types"
export async function getMarkdownForPage(
config: IDocuNotionConfig,
context: IDocuNotionContext,
page: NotionPage2
page: NotionPage
): Promise<string> {
info(`Reading & converting page ${page.id}/${page.nameOrTitle}`)

Expand Down Expand Up @@ -248,7 +248,7 @@ function registerNotionToMarkdownCustomTransforms(
}

// enhance:make this built-in plugin so that it can be overridden
function getFrontMatter(page: NotionPage2): string {
function getFrontMatter(page: NotionPage): string {
const customProperties = Object.fromEntries(
Object.entries(page.metadata.properties).map(([key, _]) => [
key,
Expand Down

0 comments on commit d93d306

Please sign in to comment.