Skip to content

Commit

Permalink
fix: allow usage of child_databases
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Nov 16, 2024
1 parent 6e1ac04 commit 6983b66
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/services/NotionService/BlockHandler/BlockHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class BlockHandler {
}

async findFlashcardsFromPage(locator: Finder): Promise<Deck[]> {
const { topLevelId, rules, parentName, parentType } = locator;
const { topLevelId, rules, parentName } = locator;
let { decks } = locator;

const page = await this.api.getPage(topLevelId);
Expand All @@ -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)) {
Expand Down Expand Up @@ -333,22 +333,27 @@ 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') {
decks = await this.handleChildDatabase(sd, rules, decks);
continue;
}

const res = await this.api.getBlocks({
createdAt: sd.created_time,
lastEditedAt: sd.last_edited_time,
id: sd.id,
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)
);

Expand Down Expand Up @@ -391,6 +396,27 @@ class BlockHandler {
console.log('have ', decks.length, ' decks so far');
return decks;
}

private async handleChildDatabase(
sd: BlockObjectResponse,
rules: ParserRules,
decks: Deck[]
): Promise<Deck[]> {
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;
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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':
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 6983b66

Please sign in to comment.