@@ -4,18 +4,11 @@ import {
44 GetPageResponse ,
55 ListBlockChildrenResponse ,
66} from "@notionhq/client/build/src/api-endpoints" ;
7- import { RateLimiter } from "limiter" ;
87import { Client } from "@notionhq/client" ;
98import { logDebug } from "./log" ;
10- import { parseLinkId } from "./links " ;
9+ import { parseLinkId } from "./plugins/internalLinks " ;
1110import { info } from "console" ;
12-
13- const notionLimiter = new RateLimiter ( {
14- tokensPerInterval : 3 ,
15- interval : "second" ,
16- } ) ;
17-
18- let notionClient : Client ;
11+ import { ListBlockChildrenResponseResults } from "notion-to-md/build/types" ;
1912
2013// Notion has 2 kinds of pages: a normal one which is just content, and what I'm calling a "database page", which has whatever properties you put on it.
2114// docu-notion supports the later via links from outline pages. That is, you put the database pages in a database, then separately, in the outline, you
@@ -25,55 +18,50 @@ export enum PageType {
2518 DatabasePage ,
2619 Simple ,
2720}
28- export function initNotionClient ( notionToken : string ) : Client {
29- notionClient = new Client ( {
30- auth : notionToken ,
31- } ) ;
32- return notionClient ;
33- }
3421
3522export class NotionPage {
36- private metadata : GetPageResponse ;
37- public readonly pageId : string ;
38- public readonly order : number ;
39- public context : string ; // where we found it in the hierarchy of the outline
23+ public metadata : GetPageResponse ;
24+ public pageId : string ;
25+ public order : number ;
26+ public layoutContext : string ; // where we found it in the hierarchy of the outline
4027 public foundDirectlyInOutline : boolean ; // the page was found as a descendent of /outline instead of being linked to
4128
42- private constructor (
43- context : string ,
44- pageId : string ,
45- order : number ,
46- metadata : GetPageResponse ,
47- foundDirectlyInOutline : boolean
48- ) {
49- this . context = context ;
50- this . pageId = pageId ;
51- this . order = order ;
52- this . metadata = metadata ;
53- this . foundDirectlyInOutline = foundDirectlyInOutline ;
29+ // public constructor(
30+ // layoutContext: string,
31+ // pageId: string,
32+ // order: number,
33+ // metadata: GetPageResponse,
34+ // foundDirectlyInOutline: boolean
35+ // ) {
36+ // this.layoutContext = layoutContext;
37+ // this.pageId = pageId;
38+ // this.order = order;
39+ // this.metadata = metadata;
40+ // this.foundDirectlyInOutline = foundDirectlyInOutline;
41+
42+ // // review: this is expensive to learn as it takes another api call... I
43+ // // think? We can tell if it's a database because it has a "Name" instead of a
44+ // // "tile" and "parent": "type": "database_id". But do we need to differentiate
45+ // //this.type = PageType.Unknown;
46+ // }
47+ public constructor ( args : {
48+ layoutContext : string ;
49+ pageId : string ;
50+ order : number ;
51+ metadata : GetPageResponse ;
52+ foundDirectlyInOutline : boolean ;
53+ } ) {
54+ this . layoutContext = args . layoutContext ;
55+ this . pageId = args . pageId ;
56+ this . order = args . order ;
57+ this . metadata = args . metadata ;
58+ this . foundDirectlyInOutline = args . foundDirectlyInOutline ;
5459
5560 // review: this is expensive to learn as it takes another api call... I
5661 // think? We can tell if it's a database because it has a "Name" instead of a
5762 // "tile" and "parent": "type": "database_id". But do we need to differentiate
5863 //this.type = PageType.Unknown;
5964 }
60- public static async fromPageId (
61- context : string ,
62- pageId : string ,
63- order : number ,
64- foundDirectlyInOutline : boolean
65- ) : Promise < NotionPage > {
66- const metadata = await getPageMetadata ( pageId ) ;
67-
68- //logDebug("notion metadata", JSON.stringify(metadata));
69- return new NotionPage (
70- context ,
71- pageId ,
72- order ,
73- metadata ,
74- foundDirectlyInOutline
75- ) ;
76- }
7765
7866 public matchesLinkId ( id : string ) : boolean {
7967 const { baseLinkId } = parseLinkId ( id ) ;
@@ -172,14 +160,6 @@ export class NotionPage {
172160 return this . getSelectProperty ( "Status" ) ;
173161 }
174162
175- private async getChildren ( ) : Promise < ListBlockChildrenResponse > {
176- const children = await notionClient . blocks . children . list ( {
177- block_id : this . pageId ,
178- page_size : 100 , // max hundred links in a page
179- } ) ;
180- return children ;
181- }
182-
183163 public getPlainTextProperty (
184164 property : string ,
185165 defaultIfEmpty : string
@@ -244,68 +224,28 @@ export class NotionPage {
244224 return p . select ?. name || undefined ;
245225 }
246226
247- public async getBlockChildren ( ) : Promise < ListBlockChildrenResponse > {
248- // we can only get so many responses per call, so we set this to
249- // the first response we get, then keep adding to its array of blocks
250- // with each subsequent response
251- let overallResult : ListBlockChildrenResponse | undefined = undefined ;
252- let start_cursor = undefined ;
253-
254- do {
255- await rateLimit ( ) ;
256-
257- const response : ListBlockChildrenResponse =
258- await notionClient . blocks . children . list ( {
259- start_cursor : start_cursor ,
260- block_id : this . pageId ,
261- } ) ;
262- if ( ! overallResult ) {
263- overallResult = response ;
264- } else {
265- overallResult . results . push ( ...response . results ) ;
266- }
267-
268- start_cursor = response ?. next_cursor ;
269- } while ( start_cursor != null ) ;
270- return overallResult ;
271- }
272-
273- public async getContentInfo ( ) : Promise < {
227+ public async getContentInfo (
228+ children : ListBlockChildrenResponseResults
229+ ) : Promise < {
274230 childPageIdsAndOrder : { id : string ; order : number } [ ] ;
275231 linksPageIdsAndOrder : { id : string ; order : number } [ ] ;
276232 hasParagraphs : boolean ;
277233 } > {
278- const children = await this . getChildren ( ) ;
279- for ( let i = 0 ; i < children . results . length ; i ++ ) {
280- ( children . results [ i ] as any ) . order = i ;
234+ for ( let i = 0 ; i < children . length ; i ++ ) {
235+ ( children [ i ] as any ) . order = i ;
281236 }
282237 return {
283- childPageIdsAndOrder : children . results
238+ childPageIdsAndOrder : children
284239 . filter ( ( b : any ) => b . type === "child_page" )
285240 . map ( ( b : any ) => ( { id : b . id , order : b . order } ) ) ,
286- linksPageIdsAndOrder : children . results
241+ linksPageIdsAndOrder : children
287242 . filter ( ( b : any ) => b . type === "link_to_page" )
288243 . map ( ( b : any ) => ( { id : b . link_to_page . page_id , order : b . order } ) ) ,
289- hasParagraphs : children . results . some (
244+ hasParagraphs : children . some (
290245 b =>
291246 ( b as any ) . type === "paragraph" &&
292247 ( b as any ) . paragraph . rich_text . length > 0
293248 ) ,
294249 } ;
295250 }
296251}
297-
298- async function getPageMetadata ( id : string ) : Promise < GetPageResponse > {
299- await rateLimit ( ) ;
300-
301- return await notionClient . pages . retrieve ( {
302- page_id : id ,
303- } ) ;
304- }
305-
306- async function rateLimit ( ) {
307- if ( notionLimiter . getTokensRemaining ( ) < 1 ) {
308- logDebug ( "" , "*** delaying for rate limit" ) ;
309- }
310- await notionLimiter . removeTokens ( 1 ) ;
311- }
0 commit comments