|
1 | | -import { lstatSync, readdirSync, readFileSync } from 'node:fs' |
| 1 | +import { lstatSync, readdirSync, readFileSync, existsSync } from 'node:fs' |
2 | 2 | import { markdownToBlocks } from '@tryfabric/martian' |
3 | | -import { addBlocks, createPage } from './notion_api.js' |
4 | | -import { addImportStatus, loadImportStatuses } from './db.js' |
| 3 | +import { addBlocks, archivePage, createPage } from './notion_api.js' |
| 4 | +import { addImportStatus, loadImportStatuses, updateImportStatus } from './db.js' |
5 | 5 |
|
6 | 6 |
|
7 | 7 | const importedNotes = {} |
@@ -48,6 +48,57 @@ export async function importNotes(notesDir, onlyWithErrors = false) { |
48 | 48 | console.info('\nDONE.') |
49 | 49 | } |
50 | 50 |
|
| 51 | +export async function importNotesWithErrors(notesDir) { |
| 52 | + const notes = (await loadImportStatuses()).filter(it => it.error) |
| 53 | + let fileIndex = 0 |
| 54 | + |
| 55 | + for (const noteInfo of notes) { |
| 56 | + const progress = Math.round(++fileIndex / notes.length * 100) |
| 57 | + console.log(`\n[${progress}%][${fileIndex}/${notes.length}] ${noteInfo.filePath}`) |
| 58 | + console.log('-'.repeat(100) + '\n') |
| 59 | + const filePath = `${notesDir}/${noteInfo.filePath}` |
| 60 | + |
| 61 | + noteInfo.imported = true |
| 62 | + noteInfo.error = null |
| 63 | + |
| 64 | + if (!existsSync(filePath)) { |
| 65 | + console.log(`File: "${filePath}" was deleted and will be archive in Notion`) |
| 66 | + try { |
| 67 | + await archivePage(noteInfo.noteId) |
| 68 | + } catch (e) { |
| 69 | + noteInfo.imported = false |
| 70 | + noteInfo.error = e.body || e |
| 71 | + console.error(`ERROR with note id: ${noteInfo.id}`) |
| 72 | + } |
| 73 | + |
| 74 | + await updateImportStatus(noteInfo) |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + noteInfo.data = '' |
| 79 | + try { |
| 80 | + noteInfo.data = readFileSync(filePath, 'utf8') |
| 81 | + } catch (e) { |
| 82 | + console.error('error in file: ', noteInfo.filePath, '\n', e) |
| 83 | + } |
| 84 | + |
| 85 | + if (!noteInfo.data) { |
| 86 | + console.log(`File: "${filePath}" is empty`) |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + const blocks = markdownToBlocks(noteInfo.data) |
| 91 | + try { |
| 92 | + await addBlocks({ parentId: noteInfo.noteId, blocks }) |
| 93 | + } catch (e) { |
| 94 | + noteInfo.imported = false |
| 95 | + noteInfo.error = e.body || e |
| 96 | + console.error('ERROR') |
| 97 | + } |
| 98 | + await updateImportStatus(noteInfo) |
| 99 | + } |
| 100 | +} |
| 101 | + |
51 | 102 | async function createParentNotes(relFilePath) { |
52 | 103 | let dirPath = '' |
53 | 104 | let parentId = '' |
|
0 commit comments