|
| 1 | +import { Client } from "@notionhq/client"; |
| 2 | +import { ListBlockChildrenResponse } from "@notionhq/client/build/src/api-endpoints"; |
| 3 | +import { NotionToMarkdown } from "notion-to-md"; |
| 4 | +import { |
| 5 | + ListBlockChildrenResponseResult, |
| 6 | + ListBlockChildrenResponseResults, |
| 7 | +} from "notion-to-md/build/types"; |
| 8 | + |
| 9 | +export function setupCustomTransformers( |
| 10 | + notionToMarkdown: NotionToMarkdown, |
| 11 | + notionClient: Client |
| 12 | +): void { |
| 13 | + notionToMarkdown.setCustomTransformer( |
| 14 | + "column_list", |
| 15 | + (block: ListBlockChildrenResponseResult) => |
| 16 | + notionColumnListToMarkdown(notionToMarkdown, notionClient, block) |
| 17 | + ); |
| 18 | + |
| 19 | + notionToMarkdown.setCustomTransformer( |
| 20 | + "column", |
| 21 | + (block: ListBlockChildrenResponseResult) => |
| 22 | + notionColumnToMarkdown(notionToMarkdown, notionClient, block) |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +async function notionColumnListToMarkdown( |
| 27 | + notionToMarkdown: NotionToMarkdown, |
| 28 | + notionClient: Client, |
| 29 | + block: ListBlockChildrenResponseResult |
| 30 | +): Promise<string> { |
| 31 | + const { id, has_children } = block as any; // "any" because the notion api type system is complex with a union that don't know how to help TS to cope with |
| 32 | + |
| 33 | + if (!has_children) return ""; |
| 34 | + |
| 35 | + const column_list_children = await getBlockChildren(notionClient, id, 100); |
| 36 | + |
| 37 | + const column_list_promise = column_list_children.map( |
| 38 | + async column => await notionToMarkdown.blockToMarkdown(column) |
| 39 | + ); |
| 40 | + |
| 41 | + const columns: string[] = await Promise.all(column_list_promise); |
| 42 | + |
| 43 | + return `<div class='column_list'>${columns.join("\n\n")}</div>`; |
| 44 | +} |
| 45 | + |
| 46 | +async function notionColumnToMarkdown( |
| 47 | + notionToMarkdown: NotionToMarkdown, |
| 48 | + notionClient: Client, |
| 49 | + block: ListBlockChildrenResponseResult |
| 50 | +): Promise<string> { |
| 51 | + const { id, has_children } = block as any; // "any" because the notion api type system is complex with a union that don't know how to help TS to cope with |
| 52 | + |
| 53 | + if (!has_children) return ""; |
| 54 | + |
| 55 | + const children = await getBlockChildren(notionClient, id, 100); |
| 56 | + |
| 57 | + const childrenPromise = children.map( |
| 58 | + async column => await notionToMarkdown.blockToMarkdown(column) |
| 59 | + ); |
| 60 | + |
| 61 | + const childrenStrings: string[] = await Promise.all(childrenPromise); |
| 62 | + |
| 63 | + return `<div class='column'>${childrenStrings.join("\n\n")}</div>`; |
| 64 | +} |
| 65 | + |
| 66 | +async function getBlockChildren( |
| 67 | + notionClient: Client, |
| 68 | + block_id: string, |
| 69 | + totalPage: number | null |
| 70 | +) { |
| 71 | + try { |
| 72 | + const result: ListBlockChildrenResponseResults = []; |
| 73 | + let pageCount = 0; |
| 74 | + let start_cursor = undefined; |
| 75 | + |
| 76 | + do { |
| 77 | + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion |
| 78 | + const response = (await notionClient.blocks.children.list({ |
| 79 | + start_cursor: start_cursor, |
| 80 | + block_id: block_id, |
| 81 | + })) as ListBlockChildrenResponse; |
| 82 | + result.push(...response.results); |
| 83 | + |
| 84 | + start_cursor = response?.next_cursor; |
| 85 | + pageCount += 1; |
| 86 | + } while ( |
| 87 | + start_cursor != null && |
| 88 | + (totalPage == null || pageCount < totalPage) |
| 89 | + ); |
| 90 | + |
| 91 | + //TODO: copied this in, what is it for? modifyNumberedListObject(result); |
| 92 | + return result; |
| 93 | + } catch (e) { |
| 94 | + console.error(e); |
| 95 | + return []; |
| 96 | + } |
| 97 | +} |
0 commit comments