Skip to content

Instantly share code, notes, and snippets.

@cmoog
Last active December 12, 2022 21:00
Show Gist options
  • Save cmoog/1955729ff6599a5b9014e7c430c89cb0 to your computer and use it in GitHub Desktop.
Save cmoog/1955729ff6599a5b9014e7c430c89cb0 to your computer and use it in GitHub Desktop.
Kindle highlight parser and exporter

Simple script to export highlights from a Kindle.

Requirments

  • macOS host
  • Kindle plugged into Mac
  • Deno installed on Mac

Usage

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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment