@@ -56,9 +56,26 @@ function composeSignature(pageId: string): string {
56
56
return `<!-- ${ pageId } -->` ;
57
57
}
58
58
59
- function extractPageTitle ( page : Page ) : string {
60
- return ( ( page as any ) . properties ?. title || ( page as any ) . properties ?. Title )
61
- ?. title [ 0 ] . plain_text ;
59
+ function extractPageTitle ( page : Page ) : string | null {
60
+ const pageTitleProps =
61
+ ( page as any ) . properties ?. title || ( page as any ) . properties ?. Title ;
62
+
63
+ if ( ! pageTitleProps ) {
64
+ return null ;
65
+ }
66
+
67
+ try {
68
+ const titleRecord = pageTitleProps . title ?. [ 0 ] ;
69
+ if ( ! titleRecord ) {
70
+ return null ;
71
+ }
72
+
73
+ return titleRecord . plain_text || null ;
74
+ } catch ( e ) {
75
+ console . error ( "failed on pageTitleProps" , page ) ;
76
+
77
+ return null ;
78
+ }
62
79
}
63
80
64
81
type DiscussionsSearchResult = NonNullable <
@@ -287,95 +304,103 @@ async function buildUpdatePlan(
287
304
delete : [ ] ,
288
305
} ;
289
306
290
- for ( const page of pages ) {
291
- const pageTitle = extractPageTitle ( page ) ;
292
- console . info ( `Building plan for page: ` , pageTitle , page ) ;
293
- const mdBlocks = await n2m . pageToMarkdown ( page . id , 2 ) ;
294
- const pageAttributes = distinguishPage ( mdBlocks [ 0 ] ) ;
295
- const notionPageSignature = composeSignature ( page . id ) ;
296
- const existingDiscussion = discussions . find ( ( v ) =>
297
- v . body . startsWith ( notionPageSignature )
298
- ) ;
299
- const existingIssue = issues . find ( ( v ) =>
300
- v . body . startsWith ( notionPageSignature )
301
- ) ;
302
-
303
- if ( pageAttributes === null ) {
304
- if ( existingDiscussion ) {
305
- outputDiscussions . delete . push ( {
306
- repoId : existingDiscussion . repository . id ,
307
- discussion : existingDiscussion ,
308
- } ) ;
309
- }
307
+ const modified = await Promise . all (
308
+ pages . map ( async ( page ) => {
309
+ const pageTitle = extractPageTitle ( page ) ;
310
310
311
- if ( existingIssue ) {
312
- outputIssues . delete . push ( {
313
- repoId : existingIssue . repository . id ,
314
- issue : existingIssue ,
315
- } ) ;
311
+ if ( ! pageTitle ) {
312
+ console . warn ( `Skipped page due to missing title info:` , page ) ;
313
+ return ;
316
314
}
317
- } else if ( pageAttributes . type === "discussion" ) {
318
- const [ owner , name ] = pageAttributes . repo . split ( "/" ) ;
319
- const repoInfo = await getRepoInfo ( octokit , name , owner ) ;
320
- const category = ( repoInfo . discussionCategories || [ ] ) . find (
321
- ( d : any ) =>
322
- d . name . toLowerCase ( ) === pageAttributes . categoryName . toLowerCase ( )
315
+
316
+ console . info ( `Building plan for page: ` , pageTitle , page ) ;
317
+ const mdBlocks = await n2m . pageToMarkdown ( page . id , 2 ) ;
318
+ const pageAttributes = distinguishPage ( mdBlocks [ 0 ] ) ;
319
+ const notionPageSignature = composeSignature ( page . id ) ;
320
+ const existingDiscussion = discussions . find ( ( v ) =>
321
+ v . body . startsWith ( notionPageSignature )
322
+ ) ;
323
+ const existingIssue = issues . find ( ( v ) =>
324
+ v . body . startsWith ( notionPageSignature )
323
325
) ;
324
326
325
- if ( ! category ) {
326
- throw new Error (
327
- `Category ${ pageAttributes . categoryName } not found in repo ${ pageAttributes . repo } `
328
- ) ;
329
- }
327
+ if ( pageAttributes === null ) {
328
+ if ( existingDiscussion ) {
329
+ outputDiscussions . delete . push ( {
330
+ repoId : existingDiscussion . repository . id ,
331
+ discussion : existingDiscussion ,
332
+ } ) ;
333
+ }
330
334
331
- if ( existingDiscussion ) {
332
- outputDiscussions . update . push ( {
333
- page,
334
- body : `${ notionPageSignature } \n${ HEADER_NOTE } \n${ composeLink (
335
- page
336
- ) } \n${ n2m . toMarkdownString ( mdBlocks . slice ( 1 ) ) } `,
337
- title : pageTitle ,
338
- repoId : existingDiscussion . repository . id ,
339
- discussion : existingDiscussion ,
340
- categoryId : category . id ,
341
- } ) ;
342
- } else {
343
- outputDiscussions . create . push ( {
344
- page,
345
- body : `${ notionPageSignature } \n${ HEADER_NOTE } \n${ composeLink (
346
- page
347
- ) } \n${ n2m . toMarkdownString ( mdBlocks . slice ( 1 ) ) } `,
348
- title : pageTitle ,
349
- categoryId : category . id ,
350
- repoId : repoInfo . id ,
351
- } ) ;
352
- }
353
- } else if ( pageAttributes . type === "issue" ) {
354
- if ( existingIssue ) {
355
- outputIssues . update . push ( {
356
- page,
357
- body : `${ notionPageSignature } \n${ HEADER_NOTE } \n${ composeLink (
358
- page
359
- ) } \n${ n2m . toMarkdownString ( mdBlocks . slice ( 1 ) ) } `,
360
- title : pageTitle ,
361
- repoId : existingIssue . repository . id ,
362
- issue : existingIssue ,
363
- } ) ;
364
- } else {
335
+ if ( existingIssue ) {
336
+ outputIssues . delete . push ( {
337
+ repoId : existingIssue . repository . id ,
338
+ issue : existingIssue ,
339
+ } ) ;
340
+ }
341
+ } else if ( pageAttributes . type === "discussion" ) {
365
342
const [ owner , name ] = pageAttributes . repo . split ( "/" ) ;
366
343
const repoInfo = await getRepoInfo ( octokit , name , owner ) ;
344
+ const category = ( repoInfo . discussionCategories || [ ] ) . find (
345
+ ( d : any ) =>
346
+ d . name . toLowerCase ( ) === pageAttributes . categoryName . toLowerCase ( )
347
+ ) ;
348
+
349
+ if ( ! category ) {
350
+ throw new Error (
351
+ `Category ${ pageAttributes . categoryName } not found in repo ${ pageAttributes . repo } `
352
+ ) ;
353
+ }
367
354
368
- outputIssues . create . push ( {
369
- page,
370
- body : `${ notionPageSignature } \n${ HEADER_NOTE } \n${ composeLink (
371
- page
372
- ) } \n${ n2m . toMarkdownString ( mdBlocks . slice ( 1 ) ) } `,
373
- title : pageTitle ,
374
- repoId : repoInfo . id ,
375
- } ) ;
355
+ if ( existingDiscussion ) {
356
+ outputDiscussions . update . push ( {
357
+ page,
358
+ body : `${ notionPageSignature } \n${ HEADER_NOTE } \n${ composeLink (
359
+ page
360
+ ) } \n${ n2m . toMarkdownString ( mdBlocks . slice ( 1 ) ) } `,
361
+ title : pageTitle ,
362
+ repoId : existingDiscussion . repository . id ,
363
+ discussion : existingDiscussion ,
364
+ categoryId : category . id ,
365
+ } ) ;
366
+ } else {
367
+ outputDiscussions . create . push ( {
368
+ page,
369
+ body : `${ notionPageSignature } \n${ HEADER_NOTE } \n${ composeLink (
370
+ page
371
+ ) } \n${ n2m . toMarkdownString ( mdBlocks . slice ( 1 ) ) } `,
372
+ title : pageTitle ,
373
+ categoryId : category . id ,
374
+ repoId : repoInfo . id ,
375
+ } ) ;
376
+ }
377
+ } else if ( pageAttributes . type === "issue" ) {
378
+ if ( existingIssue ) {
379
+ outputIssues . update . push ( {
380
+ page,
381
+ body : `${ notionPageSignature } \n${ HEADER_NOTE } \n${ composeLink (
382
+ page
383
+ ) } \n${ n2m . toMarkdownString ( mdBlocks . slice ( 1 ) ) } `,
384
+ title : pageTitle ,
385
+ repoId : existingIssue . repository . id ,
386
+ issue : existingIssue ,
387
+ } ) ;
388
+ } else {
389
+ const [ owner , name ] = pageAttributes . repo . split ( "/" ) ;
390
+ const repoInfo = await getRepoInfo ( octokit , name , owner ) ;
391
+
392
+ outputIssues . create . push ( {
393
+ page,
394
+ body : `${ notionPageSignature } \n${ HEADER_NOTE } \n${ composeLink (
395
+ page
396
+ ) } \n${ n2m . toMarkdownString ( mdBlocks . slice ( 1 ) ) } `,
397
+ title : pageTitle ,
398
+ repoId : repoInfo . id ,
399
+ } ) ;
400
+ }
376
401
}
377
- }
378
- }
402
+ } )
403
+ ) ;
379
404
380
405
return {
381
406
issues : outputIssues ,
@@ -647,7 +672,7 @@ export default {
647
672
) : Promise < Response > {
648
673
try {
649
674
const plan = await run ( env ) ;
650
-
675
+ console . info ( `Sync result:` , plan ) ;
651
676
return new Response ( JSON . stringify ( { plan } ) , { status : 200 } ) ;
652
677
} catch ( e ) {
653
678
console . error ( e ) ;
@@ -666,6 +691,6 @@ export default {
666
691
env : Env ,
667
692
ctx : ExecutionContext
668
693
) : Promise < void > {
669
- await run ( env ) ;
694
+ console . info ( `Scheduled sync result: ` , await run ( env ) ) ;
670
695
} ,
671
696
} ;
0 commit comments