Skip to content

Commit 6439e02

Browse files
committed
Added parent note creating
1 parent 3902bbe commit 6439e02

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

src/index.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,67 @@ import { addBlocks, createPage } from './notion_api.js'
44
import { addImportStatus } from './db.js'
55

66

7+
const notesDir = '/home/phpusr/tmp/notes/NOTES/car'
8+
const importedNotes = {}
79
await importNotes()
810

911
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())
1215

1316
for (const relFilePath of files) {
14-
const filePath = `${notesDir}/${relFilePath}`
15-
if (!lstatSync(filePath).isFile()) {
16-
continue
17-
}
17+
createParentNotes(relFilePath)
1818

1919
let data = ''
2020
try {
21+
const filePath = `${notesDir}/${relFilePath}`
2122
data = readFileSync(`${notesDir}/${relFilePath}`, 'utf8')
2223
} catch (e) {
23-
console.error('error in file: ', relFilePath)
24-
console.error(e)
24+
console.error('error in file: ', relFilePath, '\n', e)
2525
continue
2626
}
2727

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)
2934
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)
3148
}
3249
}
3350

3451
async function saveNoteToNotion(noteInfo) {
52+
if (importedNotes.hasOwnProperty(noteInfo.filePath)) {
53+
console.info(`Note ${noteInfo.filePath} already exists`)
54+
return
55+
}
56+
3557
const page = await createPage({ title: noteInfo.title })
3658
noteInfo.noteId = page.id
59+
importedNotes[noteInfo.filePath] = noteInfo
3760

3861
console.log(`\n${noteInfo.filePath}`)
3962
console.log('-'.repeat(30) + '\n')
4063

64+
if (!noteInfo.data) {
65+
return
66+
}
67+
4168
const blocks = markdownToBlocks(noteInfo.data)
4269
try {
4370
await addBlocks({ parentId: page.id, blocks })
@@ -47,4 +74,6 @@ async function saveNoteToNotion(noteInfo) {
4774
noteInfo.error = JSON.stringify(e)
4875
console.error('ERROR')
4976
}
77+
78+
await addImportStatus(noteInfo)
5079
}

src/notion_api.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ const notion = new Client({
66
auth: process.env.NOTION_TOKEN
77
})
88

9+
let rootPageId = null
10+
911
export async function createPage({ title, pageId }) {
1012
if (!pageId) {
11-
pageId = ROOT_PAGE_ID
13+
if (!rootPageId) {
14+
// Creating root page with date
15+
rootPageId = (await createPage({
16+
title: new Date().toLocaleDateString(),
17+
pageId: ROOT_PAGE_ID
18+
})).id
19+
}
20+
pageId = rootPageId
1221
}
1322

1423
return await notion.pages.create({

0 commit comments

Comments
 (0)