Created
August 16, 2025 17:32
-
-
Save nilscox/5ab44363e9af2d25461801efcd78cb13 to your computer and use it in GitHub Desktop.
Create files from inkdrop backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { mkdirSync, readdirSync, readFileSync, utimesSync, writeFileSync } from 'fs'; | |
| import { join } from 'path'; | |
| type Book = { | |
| _id: string; | |
| name: string; | |
| }; | |
| type Note = { | |
| _id: string; | |
| bookId: string; | |
| createdAt: number; | |
| updatedAt: string; | |
| title: string; | |
| body: string; | |
| }; | |
| const books = new Map<string, Book>(); | |
| const notes = new Map<string, Note>(); | |
| const root = process.argv[2]; | |
| const out = process.argv[3]; | |
| for (const file of readdirSync(join(root, 'book'))) { | |
| const path = join(join(root, 'book'), file); | |
| const data = JSON.parse(readFileSync(path).toString()); | |
| books.set(data._id, data); | |
| } | |
| for (const file of readdirSync(join(root, 'note'))) { | |
| const path = join(join(root, 'note'), file); | |
| const data = JSON.parse(readFileSync(path).toString()); | |
| notes.set(data._id, data); | |
| } | |
| for (const book of Object.values(books)) { | |
| mkdirSync(join(out, book.name), { recursive: true }); | |
| } | |
| mkdirSync(join(out, 'unknown'), { recursive: true }); | |
| for (const note of Object.values(notes)) { | |
| if (note.bookId === 'trash') { | |
| continue; | |
| } | |
| const book = books.get(note.bookId); | |
| const path = join( | |
| out, | |
| book?.name ?? 'unknown', | |
| (note.title ? note.title.replaceAll(/[@\/]/g, '-') : `${Math.random().toString(36).slice(-6)}`) + '.md' | |
| ); | |
| writeFileSync(path, note.body); | |
| utimesSync(path, new Date(note.createdAt), new Date(note.updatedAt)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment