@@ -13,12 +13,16 @@ async function importNotes() {
13
13
const files = readdirSync ( notesDir , { recursive : true } )
14
14
. filter ( it => lstatSync ( `${ notesDir } /${ it } ` ) . isFile ( ) )
15
15
16
+ let fileIndex = 0
16
17
for ( const relFilePath of files ) {
17
- createParentNotes ( relFilePath )
18
+ const progress = Math . round ( ++ fileIndex / files . length * 100 )
19
+ console . log ( `\n[${ progress } %][${ fileIndex } /${ files . length } ] ${ relFilePath } ` )
20
+ console . log ( '-' . repeat ( 30 ) + '\n' )
21
+
22
+ const parentId = await createParentNotes ( relFilePath )
18
23
19
24
let data = ''
20
25
try {
21
- const filePath = `${ notesDir } /${ relFilePath } `
22
26
data = readFileSync ( `${ notesDir } /${ relFilePath } ` , 'utf8' )
23
27
} catch ( e ) {
24
28
console . error ( 'error in file: ' , relFilePath , '\n' , e )
@@ -28,52 +32,55 @@ async function importNotes() {
28
32
const noteInfo = {
29
33
title : relFilePath . split ( '/' ) . pop ( ) . split ( '.' ) [ 0 ] ,
30
34
filePath : relFilePath ,
35
+ parentId,
31
36
data
32
37
}
33
- console . log ( noteInfo )
38
+ // console.log(noteInfo)
34
39
await saveNoteToNotion ( noteInfo )
35
40
}
36
41
}
37
42
38
- function createParentNotes ( relFilePath ) {
39
- let dirPath = notesDir
40
- for ( const dir of relFilePath . split ( '/' ) . slice ( 0 , - 1 ) ) {
41
- dirPath += '/' + dir
42
- if ( importedNotes . hasOwnProperty ( dirPath ) ) {
43
- console . info ( `dir ${ dirPath } already exists` )
44
- continue
43
+ async function createParentNotes ( relFilePath ) {
44
+ let dirPath = ''
45
+ let parentId = ''
46
+ for ( const dirName of relFilePath . split ( '/' ) . slice ( 0 , - 1 ) ) {
47
+ if ( dirPath ) {
48
+ dirPath += '/'
45
49
}
50
+ dirPath += dirName
46
51
47
- console . log ( 'dir' , dir )
52
+ parentId = ( await saveNoteToNotion ( {
53
+ title : dirName ,
54
+ filePath : dirPath ,
55
+ parentId
56
+ } ) ) . noteId
48
57
}
58
+
59
+ return parentId
49
60
}
50
61
51
62
async function saveNoteToNotion ( noteInfo ) {
52
63
if ( importedNotes . hasOwnProperty ( noteInfo . filePath ) ) {
53
- console . info ( `Note ${ noteInfo . filePath } already exists` )
54
- return
64
+ console . info ( `Note " ${ noteInfo . filePath } " already exists` )
65
+ return importedNotes [ noteInfo . filePath ]
55
66
}
56
67
57
- const page = await createPage ( { title : noteInfo . title } )
68
+ const page = await createPage ( { title : noteInfo . title , pageId : noteInfo . parentId } )
58
69
noteInfo . noteId = page . id
70
+ noteInfo . imported = true
59
71
importedNotes [ noteInfo . filePath ] = noteInfo
60
72
61
- console . log ( `\n${ noteInfo . filePath } ` )
62
- console . log ( '-' . repeat ( 30 ) + '\n' )
63
-
64
- if ( ! noteInfo . data ) {
65
- return
66
- }
67
-
68
- const blocks = markdownToBlocks ( noteInfo . data )
69
- try {
70
- await addBlocks ( { parentId : page . id , blocks } )
71
- noteInfo . imported = true
72
- } catch ( e ) {
73
- noteInfo . imported = false
74
- noteInfo . error = JSON . stringify ( e )
75
- console . error ( 'ERROR' )
73
+ if ( noteInfo . data ) {
74
+ const blocks = markdownToBlocks ( noteInfo . data )
75
+ try {
76
+ await addBlocks ( { parentId : page . id , blocks} )
77
+ } catch ( e ) {
78
+ noteInfo . imported = false
79
+ noteInfo . error = e
80
+ console . error ( 'ERROR' )
81
+ }
76
82
}
77
83
78
84
await addImportStatus ( noteInfo )
85
+ return noteInfo
79
86
}
0 commit comments