Simple script to export highlights from a Kindle.
Requirments
- macOS host
- Kindle plugged into Mac
- Deno installed on Mac
chmod +x ./parse-kindle-highlights.ts
./parse-kindle-highlights.ts > highlights.json
Simple script to export highlights from a Kindle.
Requirments
chmod +x ./parse-kindle-highlights.ts
./parse-kindle-highlights.ts > highlights.json
#!/usr/bin/env -S deno run --allow-read=/Volumes/Kindle/documents | |
const withPageMatcher = | |
/- Your Highlight on page (\d*) \| Location (\d*)-(\d*) \| Added on (.*)/; | |
const withoutPageMatcher = | |
/- Your Highlight on Location (\d*)-(\d*) \| Added on (.*)/; | |
const raw = await Deno.readTextFile( | |
"/Volumes/Kindle/documents/My Clippings.txt", | |
); | |
const clippings = raw.split("==========").map((clip) => | |
clip.split("\r\n").filter((a) => a.length > 0) | |
).filter((a) => a.length === 3).map((parts) => { | |
const withPageParts = withPageMatcher.exec(parts[1]); | |
const withoutPageParts = withoutPageMatcher.exec(parts[1]); | |
return { | |
book: parts[0].trim(), | |
page: withPageParts ? withPageParts[1] : null, | |
location: withPageParts | |
? [parseInt(withPageParts[2]), parseInt(withPageParts[3])] | |
: ( | |
withoutPageParts | |
? [parseInt(withoutPageParts[1]), parseInt(withoutPageParts[2])] | |
: null | |
), | |
added_on: withPageParts ? withPageParts[4] : ( | |
withoutPageParts ? withoutPageParts[3] : null | |
), | |
content: parts[2], | |
}; | |
}); | |
console.log(JSON.stringify(clippings, null, 4)); |