1
- const core = require ( '@actions/core' ) ;
2
- const github = require ( '@actions/github' ) ;
3
-
4
- try {
5
- // `who-to-greet` input defined in action metadata file
6
- const nameToGreet = core . getInput ( 'who-to-greet' ) ;
7
- console . log ( `Hello ${ nameToGreet } !` ) ;
8
- const time = ( new Date ( ) ) . toTimeString ( ) ;
9
- core . setOutput ( "time" , time ) ;
10
- // Get the JSON webhook payload for the event that triggered the workflow
11
- const payload = JSON . stringify ( github . context . payload , undefined , 2 )
12
- console . log ( `The event payload: ${ payload } ` ) ;
13
- } catch ( error ) {
14
- core . setFailed ( error . message ) ;
1
+ const fs = require ( "fs" ) ;
2
+ const { globSync } = require ( "glob" ) ;
3
+ const { markdownToBlocks } = require ( '@tryfabric/martian' ) ;
4
+ const { Client } = require ( "@notionhq/client" ) ;
5
+ const { nextTick } = require ( "process" ) ;
6
+ const { root } = require ( "@tryfabric/martian/build/src/markdown" ) ;
7
+
8
+ // TODO: add sleep interval for all requests
9
+ // TODO: fix tables width
10
+ // TODO: NEXT: add content-only update (no pages re-creation)
11
+
12
+ [ 'FOLDER' , 'NOTION_TOKEN' , 'NOTION_ROOT_ID' ] . forEach ( ( varName ) => {
13
+ if ( ! process . env [ varName ] ) {
14
+ console . log ( `${ varName } not provided` )
15
+ process . exit ( 1 )
16
+ }
17
+ } )
18
+
19
+ const notionUrlMatch = process . env . NOTION_ROOT_ID . match ( / [ ^ - ] * $ / )
20
+ if ( notionUrlMatch == null ) {
21
+ throw new SyntaxError ( 'Provided page was not in a valid format, url must end with "-<page-id>"' ) ;
15
22
}
23
+ const notionPageId = notionUrlMatch [ 0 ]
24
+
25
+ const notion = new Client ( {
26
+ auth : process . env . NOTION_TOKEN
27
+ } )
28
+
29
+ notion . pages . retrieve ( { page_id : notionPageId } ) . then ( ( rootPage ) => {
30
+ // console.log("Root page-> ", rootPage)
31
+
32
+ var files = globSync ( `${ process . env . FOLDER } /**/*.md` , { ignore : 'node_modules/**' } )
33
+ // console.log("Files to sync ->", files)
34
+
35
+ notion . blocks . children . list ( { block_id : notionPageId } ) . then ( ( blocksResponse ) => {
36
+ const blockIdsToRemove = blocksResponse . results . map ( ( e ) => e . id )
37
+
38
+ // sequencially delete all page blocks
39
+ var doDelete = function ( idToDelete ) {
40
+ if ( ! idToDelete ) return ;
41
+
42
+ notion . blocks . delete ( {
43
+ block_id : idToDelete
44
+ } ) . then ( function ( ) {
45
+ console . log ( "Block deleted:" , idToDelete )
46
+ if ( idToDelete != blockIdsToRemove [ blockIdsToRemove . length - 1 ] ) {
47
+ nextDeleteId = blockIdsToRemove [ blockIdsToRemove . indexOf ( idToDelete ) + 1 ]
48
+ doDelete ( nextDeleteId )
49
+ }
50
+ } )
51
+ }
52
+
53
+ doDelete ( blockIdsToRemove [ 0 ] )
54
+ console . log ( 'Block deletion complete' )
55
+
56
+ var doCreate = ( filePath ) => {
57
+ const mdContent = fs . readFileSync ( filePath , 'utf8' )
58
+ const newBlocks = markdownToBlocks ( mdContent )
59
+
60
+ var title
61
+
62
+ try {
63
+ title = newBlocks [ 0 ] [ newBlocks [ 0 ] . type ] . rich_text [ 0 ] . text . content
64
+ } catch ( error ) {
65
+ console . log ( "Cannot extract page title from" , newBlocks [ 0 ] )
66
+ process . exit ( 1 )
67
+ }
68
+
69
+ notion . pages . create ( {
70
+ parent : {
71
+ type : "page_id" ,
72
+ page_id : rootPage . id
73
+ } ,
74
+ properties : {
75
+ "title" : {
76
+ "title" : [ { "text" : { "content" : title } } ] , type : "title"
77
+ }
78
+ } ,
79
+ } ) . then ( ( pageResponse ) => {
80
+ console . log ( 'Page created' , pageResponse )
81
+
82
+ notion . blocks . children . append ( { block_id : pageResponse . id , children : newBlocks } ) . then ( ( ) => {
83
+ // process next page
84
+ if ( filePath != files [ files . length - 1 ] ) {
85
+ doCreate ( files [ files . indexOf ( filePath ) + 1 ] )
86
+ }
87
+ } )
88
+
89
+ } ) . catch ( ( error ) => {
90
+ console . log ( "Page creation failed" , error )
91
+ process . exit ( 1 )
92
+ } )
93
+ }
94
+
95
+ doCreate ( files [ 0 ] )
96
+ } )
97
+
98
+ } ) . catch ( ( error ) => {
99
+ console . log ( "Root page not found" , error . body )
100
+ process . exit ( 1 )
101
+ } )
0 commit comments