Skip to content

Commit

Permalink
feat: new clipping parser
Browse files Browse the repository at this point in the history
  • Loading branch information
theBenForce committed May 31, 2022
1 parent 24a0646 commit 6f887be
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/utils/parseKindleHighlights.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import { parseClipping, parseTitleLine, parseMetaLine } from "./parseKindleHighlights";
import { parseClipping, parseTitleLine, parseMetaLine, parseKindleHighlights } from "./parseKindleHighlights";

const CLIPPING_1 = `How to Take Smart Notes: One Simple Technique to Boost Writing, Learning and Thinking – for Students, Academics and Nonfiction Book Writers (Ahrens, Sönke)
- Your Highlight on page 2 | Location 129-130 | Added on Tuesday, August 17, 2021 5:53:38 AM
Expand All @@ -17,6 +17,13 @@ const NOTE_CLIPPING = `Effective Notetaking (Study Skills Book 1) (McPherson, Fi
Thinking about a subject before reading about it can increase retention.`;

describe('parseKindleHighlights', () => {
describe('parseKindleHighlights', () => {
it('should combine highlights from the same book', () => {
const result = parseKindleHighlights([CLIPPING_1, BOOKMARK_CLIPPING, NOTE_CLIPPING].join('\n==========\n'));
expect(result).toHaveLength(2);
});
});

describe('parseMetaLine', () => {
const META_LINE = '- Your Note on page 7 | Location 64 | Added on Monday, August 23, 2021 6:52:21 AM';

Expand Down
40 changes: 33 additions & 7 deletions src/utils/parseKindleHighlights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export interface KindleHighlight {
timestamp: Date;
type: 'Highlight' | 'Note' | 'Bookmark';
content: string;
content?: string;
page?: number;
location: {
start: number;
Expand All @@ -17,15 +17,17 @@ export interface KindleBook extends BookMetadata {

interface BookMetadata {
title: string;
author: string;
author?: string;
}

const SEPARATOR = '\n==========\n';

export const parseTitleLine = (titleLine: string) => {
const title = titleLine.replace(/\([^)]+\)$/g, '').trim();
const authorMatches = /\((?<name>[^)]+)\)$/g.exec(titleLine);

// @ts-ignore
const author = authorMatches.groups["name"]!.trim();
const author = authorMatches?.groups?.["name"]?.trim();

return { title, author };
}
Expand Down Expand Up @@ -82,8 +84,9 @@ export const parseMetaLine = (metaLine: string): KindleHighlight => {
}

export const parseClipping = (clipping: string): KindleHighlight & BookMetadata => {
const lines = clipping.split('\n');
if (lines.length < 4) {
const lines = clipping.trim().split(/\n/);
console.info({ lines });
if (lines.length < 2) {
throw new Error(`Could not parse clipping, not enough lines: ${clipping}`);
}

Expand All @@ -101,8 +104,31 @@ export const parseClipping = (clipping: string): KindleHighlight & BookMetadata

export const parseKindleHighlights = (content: string): Array<KindleBook> => {
const clippings = content.split(/^==========$/gm).map(parseClipping);
const result = [] as Array<KindleBook>;
return clippings.reduce((result, clipping) => {
let book = result.find((b) => b.title === clipping.title && b.author === clipping.author);

if (!book) {
book = {
title: clipping.title,
author: clipping.author,
highlights: [],
lastHighlight: clipping.timestamp
};
result.push(book);
}

if (book.lastHighlight < clipping.timestamp) {
book.lastHighlight = clipping.timestamp;
}

book.highlights.push({
content: clipping.content,
location: clipping.location,
timestamp: clipping.timestamp,
type: clipping.type,
page: clipping.page
});

return result;
return result;
}, [] as Array<KindleBook>);
}

0 comments on commit 6f887be

Please sign in to comment.