diff --git a/src/services/NotionService/BlockHandler/BlockHandler.ts b/src/services/NotionService/BlockHandler/BlockHandler.ts index f27d7851c..e85ab3bf0 100644 --- a/src/services/NotionService/BlockHandler/BlockHandler.ts +++ b/src/services/NotionService/BlockHandler/BlockHandler.ts @@ -278,7 +278,7 @@ class BlockHandler { } async findFlashcardsFromPage(locator: Finder): Promise { - const { topLevelId, rules, parentName, parentType } = locator; + const { topLevelId, rules, parentName } = locator; let { decks } = locator; const page = await this.api.getPage(topLevelId); @@ -297,7 +297,7 @@ class BlockHandler { if (!this.firstPageTitle) { this.firstPageTitle = title; } - if (rules.permitsDeckAsPage() && parentType === 'page' && page) { + if (rules.permitsDeckAsPage() && page) { // Locate the card blocks to be used from the parser rules const cBlocks = blocks.filter((b: GetBlockResponse) => { if (!isFullBlock(b)) { @@ -333,14 +333,22 @@ class BlockHandler { if (this.settings.isAll) { const subDecks = blocks.filter((b) => { - if (!isFullBlock(b)) { - return; + if ('type' in b) { + return rules.SUB_DECKS.includes(b.type); } - return rules.SUB_DECKS.includes(b.type); + return; }); for (const sd of subDecks) { if (isFullBlock(sd)) { + if ( + sd.type === 'child_database' && + rules.SUB_DECKS.includes('child_database') + ) { + decks = await this.handleChildDatabase(sd, rules, decks); + continue; + } + const res = await this.api.getBlocks({ createdAt: sd.created_time, lastEditedAt: sd.last_edited_time, @@ -348,7 +356,7 @@ class BlockHandler { all: rules.UNLIMITED, type: sd.type, }); - const cBlocks = res.results.filter((b: GetBlockResponse) => + let cBlocks = res.results.filter((b: GetBlockResponse) => flashCardTypes.includes((b as BlockObjectResponse).type) ); @@ -391,6 +399,27 @@ class BlockHandler { console.log('have ', decks.length, ' decks so far'); return decks; } + + private async handleChildDatabase( + sd: BlockObjectResponse, + rules: ParserRules, + decks: Deck[] + ): Promise { + const dbResult = await this.api.queryDatabase(sd.id); + const database = await this.api.getDatabase(sd.id); + const dbName = await this.api.getDatabaseTitle(database, this.settings); + + for (const entry of dbResult.results) { + decks = await this.findFlashcardsFromPage({ + parentType: 'database', + topLevelId: entry.id, + rules, + decks, + parentName: dbName, + }); + } + return decks; + } } export default BlockHandler; diff --git a/src/services/NotionService/BlockHandler/helpers/getSubDeckName.ts b/src/services/NotionService/BlockHandler/helpers/getSubDeckName.ts index 647f7c02d..bded16643 100644 --- a/src/services/NotionService/BlockHandler/helpers/getSubDeckName.ts +++ b/src/services/NotionService/BlockHandler/helpers/getSubDeckName.ts @@ -4,6 +4,7 @@ import { getChildPageBlock } from '../../blocks/helpers/getChildPageBlock'; import { getToggleBlock } from '../../blocks/helpers/getToggleBlock'; import { richObjectToString } from '../../blocks/helpers/richObjectToString'; import { getHeading } from '../../blocks/helpers/getHeading'; +import { getChildDatabaseBlock } from '../../blocks/helpers/getChildDatabaseBlock'; const getSubDeckName = ( block: BlockObjectResponse | { title: string } @@ -17,6 +18,8 @@ const getSubDeckName = ( switch (block.type) { case 'child_page': return getChildPageBlock(block).title; + case 'child_database': + return getChildDatabaseBlock(block).title; case 'toggle': return richObjectToString(getToggleBlock(block)); case 'heading_1': diff --git a/src/services/NotionService/blocks/helpers/getChildDatabaseBlock.ts b/src/services/NotionService/blocks/helpers/getChildDatabaseBlock.ts new file mode 100644 index 000000000..35d5b3edc --- /dev/null +++ b/src/services/NotionService/blocks/helpers/getChildDatabaseBlock.ts @@ -0,0 +1,9 @@ +import { + BlockObjectResponse, + ChildDatabaseBlockObjectResponse, +} from '@notionhq/client/build/src/api-endpoints'; + +export const getChildDatabaseBlock = (block: BlockObjectResponse) => { + const page = block as ChildDatabaseBlockObjectResponse; + return page.child_database; +};