Skip to content

Commit

Permalink
chore: bump Notion-To-Markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
HEIGE-PCloud committed May 2, 2024
1 parent 68d40aa commit 6d09245
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 91 deletions.
2 changes: 1 addition & 1 deletion notion-hugo.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ const userConfig: UserConfig = {
}
}

module.exports = userConfig
export default userConfig;
66 changes: 7 additions & 59 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@
"author": "HEIGE-PCloud",
"dependencies": {
"@notionhq/client": "^2.2.15",
"@pclouddev/notion-to-markdown": "^2.7.14",
"@pclouddev/notion-to-markdown": "^2.7.15",
"dotenv": "^16.4.5",
"front-matter": "^4.0.2",
"fs-extra": "^11.2.0",
"katex": "^0.16.10",
"yaml": "^2.4.2"
},
"devDependencies": {
"@types/fs-extra": "^11.0.4",
"@types/katex": "^0.16.7",
"@types/node": "^20.12.7",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
Expand Down
58 changes: 30 additions & 28 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Client, isFullBlock, iteratePaginatedAPI } from "@notionhq/client";

const userDefinedConfig = require('../notion-hugo.config')
import userDefinedConfig from "../notion-hugo.config";

export type PageMount = {
page_id: string;
Expand All @@ -22,44 +22,46 @@ export type Config = {
};

export async function loadConfig(): Promise<Config> {
const userConfig = userDefinedConfig as UserConfig
const userConfig = userDefinedConfig as UserConfig;
const config: Config = {
mount: {
databases: [],
pages: []
}
}
pages: [],
},
};
// configure mount settings
if (userConfig.mount.manual) {
if (userConfig.mount.databases) config.mount.databases = userConfig.mount.databases
if (userConfig.mount.pages) config.mount.pages = userConfig.mount.pages
if (userConfig.mount.databases)
config.mount.databases = userConfig.mount.databases;
if (userConfig.mount.pages) config.mount.pages = userConfig.mount.pages;
} else {
if (userConfig.mount.page_url === undefined)
throw Error(`[Error] When mount.manual is false, a page_url must be set.`)
const url = new URL(userConfig.mount.page_url)
const len = url.pathname.length
if (len < 32)
throw Error(`[Error] The page_url ${url.href} is invalid`)
const pageId = url.pathname.slice(len - 32, len)
throw Error(
`[Error] When mount.manual is false, a page_url must be set.`,
);
const url = new URL(userConfig.mount.page_url);
const len = url.pathname.length;
if (len < 32) throw Error(`[Error] The page_url ${url.href} is invalid`);
const pageId = url.pathname.slice(len - 32, len);
const notion = new Client({
auth: process.env.NOTION_TOKEN,
});

for await (const block of iteratePaginatedAPI(notion.blocks.children.list, {
block_id: pageId
block_id: pageId,
})) {
if (!isFullBlock(block)) continue;
if (block.type === 'child_database') {
if (block.type === "child_database") {
config.mount.databases.push({
database_id: block.id,
target_folder: block.child_database.title
})
target_folder: block.child_database.title,
});
}
if (block.type === 'child_page') {
if (block.type === "child_page") {
config.mount.pages.push({
page_id: block.id,
target_folder: '.'
})
target_folder: ".",
});
}
}
}
Expand All @@ -68,16 +70,16 @@ export async function loadConfig(): Promise<Config> {
}

export type UserMount = {
manual: boolean
page_url?: string
manual: boolean;
page_url?: string;
databases?: DatabaseMount[];
pages?: PageMount[];
}
};

export type UserConfig = {
mount: UserMount
}
mount: UserMount;
};

export function defineConfig(config: UserConfig) {
return config
}
return config;
}

0 comments on commit 6d09245

Please sign in to comment.