@@ -4,40 +4,67 @@ import { addBlocks, createPage } from './notion_api.js'
4
4
import { addImportStatus } from './db.js'
5
5
6
6
7
+ const notesDir = '/home/phpusr/tmp/notes/NOTES/car'
8
+ const importedNotes = { }
7
9
await importNotes ( )
8
10
9
11
async function importNotes ( ) {
10
- const notesDir = '/home/phpusr/tmp/notes/NOTES/car' ;
11
- const files = readdirSync ( notesDir , { recursive : true } ) . slice ( 0 , 6 )
12
+ // noinspection JSValidateTypes
13
+ const files = readdirSync ( notesDir , { recursive : true } )
14
+ . filter ( it => lstatSync ( `${ notesDir } /${ it } ` ) . isFile ( ) )
12
15
13
16
for ( const relFilePath of files ) {
14
- const filePath = `${ notesDir } /${ relFilePath } `
15
- if ( ! lstatSync ( filePath ) . isFile ( ) ) {
16
- continue
17
- }
17
+ createParentNotes ( relFilePath )
18
18
19
19
let data = ''
20
20
try {
21
+ const filePath = `${ notesDir } /${ relFilePath } `
21
22
data = readFileSync ( `${ notesDir } /${ relFilePath } ` , 'utf8' )
22
23
} catch ( e ) {
23
- console . error ( 'error in file: ' , relFilePath )
24
- console . error ( e )
24
+ console . error ( 'error in file: ' , relFilePath , '\n' , e )
25
25
continue
26
26
}
27
27
28
- const noteInfo = { title : relFilePath , filePath : relFilePath , data }
28
+ const noteInfo = {
29
+ title : relFilePath . split ( '/' ) . pop ( ) . split ( '.' ) [ 0 ] ,
30
+ filePath : relFilePath ,
31
+ data
32
+ }
33
+ console . log ( noteInfo )
29
34
await saveNoteToNotion ( noteInfo )
30
- await addImportStatus ( noteInfo )
35
+ }
36
+ }
37
+
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
45
+ }
46
+
47
+ console . log ( 'dir' , dir )
31
48
}
32
49
}
33
50
34
51
async function saveNoteToNotion ( noteInfo ) {
52
+ if ( importedNotes . hasOwnProperty ( noteInfo . filePath ) ) {
53
+ console . info ( `Note ${ noteInfo . filePath } already exists` )
54
+ return
55
+ }
56
+
35
57
const page = await createPage ( { title : noteInfo . title } )
36
58
noteInfo . noteId = page . id
59
+ importedNotes [ noteInfo . filePath ] = noteInfo
37
60
38
61
console . log ( `\n${ noteInfo . filePath } ` )
39
62
console . log ( '-' . repeat ( 30 ) + '\n' )
40
63
64
+ if ( ! noteInfo . data ) {
65
+ return
66
+ }
67
+
41
68
const blocks = markdownToBlocks ( noteInfo . data )
42
69
try {
43
70
await addBlocks ( { parentId : page . id , blocks } )
@@ -47,4 +74,6 @@ async function saveNoteToNotion(noteInfo) {
47
74
noteInfo . error = JSON . stringify ( e )
48
75
console . error ( 'ERROR' )
49
76
}
77
+
78
+ await addImportStatus ( noteInfo )
50
79
}
0 commit comments