Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Book ID's to MD5 to reduce collisions #274

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"nunjucks-date-filter": "^0.1.1",
"sanitize-filename": "^1.6.3",
"svelte-loading-spinners": "^0.1.4",
"ts-md5": "^1.3.1",
"typed-emitter": "^1.4.0"
},
"devDependencies": {
Expand Down Expand Up @@ -80,4 +81,4 @@
"*.ts": "eslint --cache --fix",
"*.{js,css,md}": "prettier --write"
}
}
}
8 changes: 7 additions & 1 deletion src/fileManager/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { get } from 'svelte/store';
import type { Book, KindleFrontmatter } from '~/models';
import { getRenderers } from '~/rendering';
import { settingsStore } from '~/store';
import { hash, hash_short } from "~/utils"

/**
* Returns a file path for a given book relative to the current Obsidian
Expand All @@ -31,8 +32,13 @@ export const bookToFrontMatter = (book: Book, highlightsCount: number): KindleFr

export const frontMatterToBook = (frontmatter: KindleFrontmatter): Book => {
const formats = ['MMM DD, YYYY', 'YYYY-MM-DD'];
let book_id: string = frontmatter.bookId
// If this note is still using a fletcher ID, convert it to MD5 for comparison
if (frontmatter.bookId == hash_short(frontmatter.title)) {
book_id = hash(frontmatter.title)
}
return {
id: frontmatter.bookId,
id: book_id,
title: frontmatter.title,
author: frontmatter.author,
asin: frontmatter.asin,
Expand Down
9 changes: 4 additions & 5 deletions src/scraper/scrapeBookHighlights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Root } from 'cheerio';

import { currentAmazonRegion } from '~/amazonRegion';
import type { Book, Highlight } from '~/models';
import { br2ln, hash } from '~/utils';
import { br2ln, hash_short } from '~/utils';

import { loadRemoteDom } from './loadRemoteDom';

Expand All @@ -18,9 +18,8 @@ export const mapTextToColor = (highlightClasses: string): Highlight['color'] =>

const highlightsUrl = (book: Book, state?: NextPageState): string => {
const region = currentAmazonRegion();
return `${region.notebookUrl}?asin=${book.asin}&contentLimitState=${
state?.contentLimitState ?? ''
}&token=${state?.token ?? ''}`;
return `${region.notebookUrl}?asin=${book.asin}&contentLimitState=${state?.contentLimitState ?? ''
}&token=${state?.token ?? ''}`;
};

const parseNextPageState = ($: Root): NextPageState | null => {
Expand All @@ -40,7 +39,7 @@ const parseHighlights = ($: Root): Highlight[] => {

const text = $('#highlight', highlightEl).text()?.trim();
return {
id: hash(text),
id: hash_short(text),
text,
color,
location: $('#kp-annotation-location', highlightEl).val(),
Expand Down
4 changes: 2 additions & 2 deletions src/sync/syncClippings/parseBooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Book, groupToBooks, readMyClippingsFile } from '@hadynz/kindle-clipping
import fs from 'fs';

import type { BookHighlight, Highlight } from '~/models';
import { hash } from '~/utils';
import { hash, hash_short } from '~/utils';

const toBookHighlight = (book: Book): BookHighlight => {
return {
Expand All @@ -15,7 +15,7 @@ const toBookHighlight = (book: Book): BookHighlight => {
.filter((entry) => entry.type === 'HIGHLIGHT' || entry.type === 'UNKNOWN')
.map(
(entry): Highlight => ({
id: hash(entry.content),
id: hash_short(entry.content),
text: entry.content,
note: entry.note,
location: entry.location?.display,
Expand Down
7 changes: 6 additions & 1 deletion src/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import fletcher16 from 'fletcher';
import { Md5 } from 'ts-md5';

export const hash = (value: string): string => {
return fletcher16(Buffer.from(value.toLowerCase())).toString();
return Md5.hashStr(value.toLowerCase());
};

export const hash_short = (value: string): string => {
return fletcher16(Buffer.from(value.toLowerCase())).toString();
};