Last active
June 7, 2020 11:54
-
-
Save elvisciotti/0eea3a258b5ed47a5382b05a4179d121 to your computer and use it in GitHub Desktop.
Node script to reorganise kindle's "My Clippings.txt" into a human readable markdown file
This file contains 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
#!/usr/local/bin/node | |
/** | |
* Node script to convert kindle notes into | |
* ./kindleParse.js > myClippings.md | |
*/ | |
const endOfLine = "\r\n"; | |
const kindleNoteDelimiter = "==========" + endOfLine; | |
const titlePrefix = endOfLine + "# "; | |
const titleSuffix = endOfLine + endOfLine; | |
let inputFile = "My Clippings.txt"; | |
if (typeof process.argv[2] !== "undefined") { | |
inputFile = process.argv[2]; | |
} | |
fs = require('fs'); | |
fs.readFile(inputFile, 'utf8', function parseFile(err, data) { | |
if (err) { | |
throw new Error(err); | |
} | |
// read into map of sets | |
let organisedNotes = new Map(); | |
data.split(kindleNoteDelimiter).slice(0, 300).forEach(note => { | |
lines = note.split(endOfLine); | |
title = lines[0]; | |
body = lines.slice(2).join(endOfLine); | |
if (!organisedNotes.has(title)) { | |
organisedNotes.set(title, new Set()); | |
} | |
let bodyTrimmed = body.replace(/([\. \r\n]+)$/, "").replace(/(^[\. \r\n]+)/, "") + '.'; | |
bodyTrimmed = bodyTrimmed.charAt(0).toUpperCase() + bodyTrimmed.slice(1) | |
organisedNotes.get(title).add(bodyTrimmed); | |
}); | |
let stringOutput = ''; | |
organisedNotes.forEach((bodySet, title) => { | |
stringOutput += titlePrefix + title + titleSuffix; | |
bodySet.forEach(bodyEntry => stringOutput += bodyEntry + endOfLine + endOfLine) | |
}) | |
console.log(stringOutput); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install:
Usage
kindleSplitter ~/Downloads/My\ Clippings.txt > ~/Downloads/kindle.md