Skip to content

Instantly share code, notes, and snippets.

@elvisciotti
Last active June 7, 2020 11:54
Show Gist options
  • Save elvisciotti/0eea3a258b5ed47a5382b05a4179d121 to your computer and use it in GitHub Desktop.
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
#!/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);
});
@elvisciotti
Copy link
Author

elvisciotti commented Jun 7, 2020

Install:

curl -o /usr/local/bin/kindleSplitter https://gist.githubusercontent.com/elvisciotti/0eea3a258b5ed47a5382b05a4179d121/raw/bb446a5e7355937de45a5ad80df4d8acad154e24/kindleParser.js
chmod +x  /usr/local/bin/kindleSplitter 

Usage
kindleSplitter ~/Downloads/My\ Clippings.txt > ~/Downloads/kindle.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment