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