generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This update includes: - Add 5 sorting criteria for highlights - Add basic tests - Refactor tests to support sorting - Update documentation with sorting criteria settings and examples
- Loading branch information
1 parent
e154122
commit 5ac0b62
Showing
16 changed files
with
740 additions
and
186 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,65 @@ | ||
{ | ||
"name": "obsidian-apple-books-highlights-plugin", | ||
"version": "1.3.0", | ||
"description": "Import highlights and notes from your Apple Books to Obsidian", | ||
"main": "main.js", | ||
"scripts": { | ||
"dev": "node esbuild.config.mjs", | ||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", | ||
"lint": "eslint . --ext .ts", | ||
"version": "node version-bump.mjs", | ||
"test": "vitest", | ||
"coverage": "vitest run --coverage", | ||
"db:generate": "drizzle-kit generate:sqlite", | ||
"db:migrate": "npx tsx src/db/migrate.ts", | ||
"db:seed": "npx tsx src/db/seed.ts", | ||
"prepare": "husky", | ||
"docs:dev": "vitepress dev docs --port 3000", | ||
"docs:build": "vitepress build docs", | ||
"docs:preview": "vitepress preview docs" | ||
}, | ||
"keywords": [ | ||
"obsidian", | ||
"plugin", | ||
"apple", | ||
"books", | ||
"applebooks", | ||
"iBooks", | ||
"highlights", | ||
"notes", | ||
"importer" | ||
], | ||
"author": "bandantonio", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/bandantonio/obsidian-apple-books-highlights-plugin.git" | ||
}, | ||
"engines": { | ||
"node": ">=20.8.0", | ||
"npm": ">=10.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/better-sqlite3": "^7.6.10", | ||
"@types/node": "^18.4.1", | ||
"@typescript-eslint/eslint-plugin": "5.29.0", | ||
"@typescript-eslint/parser": "5.29.0", | ||
"@vitest/coverage-istanbul": "^1.5.1", | ||
"better-sqlite3": "^9.5.0", | ||
"builtin-modules": "3.3.0", | ||
"drizzle-kit": "^0.20.17", | ||
"drizzle-orm": "^0.30.9", | ||
"esbuild": "0.17.3", | ||
"eslint": "^8.57.0", | ||
"husky": "^9.0.11", | ||
"obsidian": "latest", | ||
"tslib": "2.4.0", | ||
"typescript": "4.7.4", | ||
"vitepress": "^1.3.1", | ||
"vitest": "^1.5.1" | ||
}, | ||
"dependencies": { | ||
"dayjs": "^1.11.10", | ||
"handlebars": "^4.7.8" | ||
} | ||
} | ||
"name": "obsidian-apple-books-highlights-plugin", | ||
"version": "1.4.0", | ||
"description": "Import highlights and notes from your Apple Books to Obsidian", | ||
"main": "main.js", | ||
"scripts": { | ||
"dev": "node esbuild.config.mjs", | ||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", | ||
"lint": "eslint . --ext .ts", | ||
"version": "node version-bump.mjs", | ||
"test": "vitest", | ||
"coverage": "vitest run --coverage", | ||
"db:generate": "drizzle-kit generate:sqlite", | ||
"db:migrate": "npx tsx src/db/migrate.ts", | ||
"db:seed": "npx tsx src/db/seed.ts", | ||
"prepare": "husky", | ||
"docs:dev": "vitepress dev docs --port 3000", | ||
"docs:build": "vitepress build docs", | ||
"docs:preview": "vitepress preview docs" | ||
}, | ||
"keywords": [ | ||
"obsidian", | ||
"plugin", | ||
"apple", | ||
"books", | ||
"applebooks", | ||
"iBooks", | ||
"highlights", | ||
"notes", | ||
"importer" | ||
], | ||
"author": "bandantonio", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/bandantonio/obsidian-apple-books-highlights-plugin.git" | ||
}, | ||
"engines": { | ||
"node": ">=20.8.0", | ||
"npm": ">=10.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/better-sqlite3": "^7.6.10", | ||
"@types/node": "^18.4.1", | ||
"@typescript-eslint/eslint-plugin": "5.29.0", | ||
"@typescript-eslint/parser": "5.29.0", | ||
"@vitest/coverage-istanbul": "^1.5.1", | ||
"better-sqlite3": "^9.5.0", | ||
"builtin-modules": "3.3.0", | ||
"drizzle-kit": "^0.20.17", | ||
"drizzle-orm": "^0.30.9", | ||
"esbuild": "0.17.3", | ||
"eslint": "^8.57.0", | ||
"husky": "^9.0.11", | ||
"obsidian": "latest", | ||
"tslib": "2.4.0", | ||
"typescript": "4.7.4", | ||
"vitepress": "^1.3.1", | ||
"vitest": "^1.5.1" | ||
}, | ||
"dependencies": { | ||
"dayjs": "^1.11.10", | ||
"handlebars": "^4.7.8" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { IHighlight, ICombinedBooksAndHighlights, IHighlightsSortingCriterion } from 'src/types'; | ||
|
||
export const sortHighlights = (combinedHighlight: ICombinedBooksAndHighlights, highlightsSortingCriterion: string) => { | ||
let sortedHighlights: IHighlight[] = []; | ||
|
||
switch (highlightsSortingCriterion) { | ||
case IHighlightsSortingCriterion.CreationDateOldToNew: | ||
sortedHighlights = combinedHighlight.annotations.sort((a, b) => a.highlightCreationDate - b.highlightCreationDate); | ||
break; | ||
case IHighlightsSortingCriterion.CreationDateNewToOld: | ||
sortedHighlights = combinedHighlight.annotations.sort((a, b) => b.highlightCreationDate - a.highlightCreationDate); | ||
break; | ||
case IHighlightsSortingCriterion.LastModifiedDateOldToNew: | ||
sortedHighlights = combinedHighlight.annotations.sort((a, b) => a.highlightModificationDate - b.highlightModificationDate); | ||
break; | ||
case IHighlightsSortingCriterion.LastModifiedDateNewToOld: | ||
sortedHighlights = combinedHighlight.annotations.sort((a, b) => b.highlightModificationDate - a.highlightModificationDate); | ||
break; | ||
case IHighlightsSortingCriterion.Book: | ||
sortedHighlights = combinedHighlight.annotations.sort((a, b) => { | ||
const firstHighlightLocation = highlightLocationToNumber(a.highlightLocation); | ||
const secondHighlightLocation = highlightLocationToNumber(b.highlightLocation); | ||
|
||
return compareLocations(firstHighlightLocation, secondHighlightLocation); | ||
}); | ||
break; | ||
} | ||
|
||
return { | ||
...combinedHighlight, | ||
annotations: sortedHighlights | ||
}; | ||
} | ||
|
||
// The function converts a highlight location string to an array of numbers | ||
export const highlightLocationToNumber = (highlightLocation: string): number[] => { | ||
// epubcfi example structure: epubcfi(/6/2[body01]!/4/2/2/1:0) | ||
return highlightLocation | ||
Check warning on line 38 in src/methods/sortHighlights.ts GitHub Actions / ✅ Linter
|
||
.slice(8, -1) // Get rid of the epubcfi() wrapper | ||
.split(',') // Split the locator into three parts: the common parent, the start subpath, and the end subpath | ||
.slice(0, -1) // Get rid of the end subpath (third part) | ||
.join(',') // Join the first two parts back together | ||
.match(/(?<!\[)[/:]\d+(?!\])/g)! // Extract all the numbers (except those in square brackets) from the first two parts | ||
.map(match => parseInt(match.slice(1))) // Get rid of the leading slash or colon and convert the string to a number | ||
} | ||
|
||
// The function performs lexicographic comparison of two locations to determine their relative position in a book | ||
export const compareLocations = (firstLocation: number[], secondLocation: number[]) => { | ||
// Loop through each element of both arrays up to the length of the shorter one | ||
for (let i = 0; i < Math.min(firstLocation.length, secondLocation.length); i++) { | ||
if (firstLocation[i] < secondLocation[i]) { | ||
return -1; | ||
} | ||
if (firstLocation[i] > secondLocation[i]) { | ||
return 1; | ||
} | ||
} | ||
|
||
// If the loop didn't return, the arrays are equal up to the length of the shorter array | ||
// so the function returns the difference in lengths to determine the order of the corresponding locations | ||
return firstLocation.length - secondLocation.length; | ||
} |
Oops, something went wrong.