This commit is contained in:
Jacob Williams 2025-02-03 11:18:04 -08:00
parent 390b651f26
commit 833f806733
5 changed files with 3449 additions and 41 deletions

9
jest.config.js Normal file
View file

@ -0,0 +1,9 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.[jt]sx?$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};

3399
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -6,18 +6,23 @@
"scripts": {
"dev": "node esbuild.config.mjs",
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
"version": "node version-bump.mjs && git add manifest.json versions.json"
"version": "node version-bump.mjs && git add manifest.json versions.json",
"test": "jest",
"test:watch": "jest --watch"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0",
"builtin-modules": "3.3.0",
"esbuild": "0.17.3",
"jest": "^29.7.0",
"obsidian": "latest",
"ts-jest": "^29.2.5",
"tslib": "2.4.0",
"typescript": "4.7.4"
}

70
src/quotes.test.ts Normal file
View file

@ -0,0 +1,70 @@
import { parseQuote } from './quotes';
describe('parseQuote', () => {
it('should fall back to using the raw clipboard contents if it cannot parse the quote', () => {
const raw = 'This is a simple quote';
const result = parseQuote(raw);
expect(result).toEqual({
raw: raw,
body: raw
});
});
it('should parse Kindle format quotes', () => {
const raw = 'We think these are our own thoughts, but they are not. They are like frozen-dinner thoughts. We buy them already made and then heat them up in our brains a little and then think them. As if they are our own. As if thoughts didnt take any effort.\n\nO\'Neill, Heather. Daydreams of Angels: Stories (p. 56). (Function). Kindle Edition. ';
const result = parseQuote(raw);
expect(result).toEqual({
raw: raw,
body: 'We think these are our own thoughts, but they are not. They are like frozen-dinner thoughts. We buy them already made and then heat them up in our brains a little and then think them. As if they are our own. As if thoughts didnt take any effort.',
authors: ['O\'Neill, Heather'],
title: 'Daydreams of Angels: Stories',
page: '56'
});
});
it('should handle quotes with multiple lines of content', () => {
const raw = 'First line\nSecond line\nThird line\nSmith, John. Some Book (p. 123). Kindle Edition';
const result = parseQuote(raw);
expect(result).toEqual({
raw: raw,
body: 'First line\nSecond line\nThird line',
authors: ['Smith, John'],
title: 'Some Book',
page: '123'
});
});
it('should handle missing page numbers', () => {
const raw = 'Like my cat, I often simply do what I want to do.\n\nParfit, Derek. Reasons and Persons (Function). Kindle Edition. ';
const result = parseQuote(raw);
expect(result).toEqual({
raw: raw,
body: 'Like my cat, I often simply do what I want to do.',
authors: ['Parfit, Derek'],
title: 'Reasons and Persons'
});
});
it('should handle titles with parentheses and page number', () => {
const raw = 'Good stuff.\n\nBody, Some. An (Un-)Amazing Book (p. 12345). (Function). Kindle Edition.';
const result = parseQuote(raw);
expect(result).toEqual({
raw: raw,
body: 'Good stuff.',
authors: ['Body, Some'],
title: 'An (Un-)Amazing Book',
page: '12345'
});
});
it('should handle titles with parenthese and no page number', () => {
const raw = 'Good stuff.\n\nBody, Some. An (Un-)Amazing Book (Function). Kindle Edition.';
const result = parseQuote(raw);
expect(result).toEqual({
raw: raw,
body: 'Good stuff.',
authors: ['Body, Some'],
title: 'An (Un-)Amazing Book'
});
});
});

View file

@ -32,7 +32,10 @@ export function parseQuote(raw: string): Quote {
result.authors = authors;
if (citationParts.length >= 2) {
result.title = citationParts[1].trim();
// If there's a page number, citationParts[1] looks like "Title (p"; if there's not, it looks like "Title (Function)".
// (Right now I literally see "(Function)" every time I copy a quote. I assume this is a bug in the Kindle app and it's supposed to contain the publisher or something.)
// The following is meant to trim the unnecessary stuff off the end while still allowing titles with parentheses.
result.title = citationParts[1].replace(/\s*\([^(]*$/, '').trim();
}
}