Skip to content

Commit

Permalink
Merge pull request #196 from hadynz/feature/author-vars
Browse files Browse the repository at this point in the history
Add author names as template variables
  • Loading branch information
hadynz authored Sep 6, 2022
2 parents f601ce3 + 5038f67 commit 2baa566
Show file tree
Hide file tree
Showing 18 changed files with 325 additions and 118 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
run: npm run lint

- name: Run tests
run: npm run test
run: npm run test-verbose
2 changes: 1 addition & 1 deletion .github/workflows/releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: |
npm install
npm run lint
npm run test
npm run test-verbose
npm run build
zip -r -j ${{ env.PLUGIN_NAME }}.zip dist -x "*.map"
ls
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-kindle-plugin",
"name": "Kindle Highlights",
"version": "1.6.10",
"version": "1.7.0",
"description": "Sync your Kindle book highlights using your Amazon login or uploading your My Clippings file",
"minAppVersion": "0.10.2",
"author": "Hady Osman",
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-kindle-plugin",
"version": "1.6.10",
"version": "1.7.0",
"description": "Sync your Kindle book highlights using your Amazon login or uploading your My Clippings file",
"main": "src/index.ts",
"repository": {
Expand All @@ -18,7 +18,8 @@
"prettier": "prettier --write '**/*.{ts,js,css,html}'",
"lint": "tsc --noemit && svelte-check && eslint . --ext .ts",
"clean": "rimraf dist main.js*",
"test": "jest --verbose",
"test": "jest",
"test-verbose": "jest --verbose",
"test-watch": "jest --watch",
"dev": "NODE_ENV=development webpack && cp ./dist/main.js* .",
"build": "NODE_ENV=production webpack",
Expand Down
31 changes: 0 additions & 31 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,6 @@ export type BookMetadata = {
authorUrl?: string;
};

export type FileRenderTemplate = {
title: string;
author: string;
asin?: string;
url?: string;
imageUrl?: string;
lastAnnotatedDate?: string;
longTitle: string;
appLink?: string;
isbn?: string;
pages: string;
publicationDate: string;
publisher: string;
authorUrl: string;
highlightsCount: number;
highlights: string;
};

export type HighlightRenderTemplate = {
id: string;
title: string;
longTitle: string;
text: string;
location?: string;
page?: string;
note?: string;
color?: 'pink' | 'blue' | 'yellow' | 'orange';
createdDate?: Date;
appLink?: string;
};

export type SyncMode = 'amazon' | 'my-clippings';

export type AmazonAccountRegion =
Expand Down
2 changes: 1 addition & 1 deletion src/rendering/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import bookTemplate from './templates/bookTemplate.njk';
import defaultHighlightTemplate from './templates/defaultHighlightTemplate.njk';
import { FileNameRenderer, FileRenderer, HighlightRenderer } from './renderer';

export const DefaultFileNameTemplate = '{{shortTitle}}';
export const DefaultFileNameTemplate = '{{authorsLastNames}}-{{title}}';
export const DefaultFileTemplate = bookTemplate;
export const DefaultHighlightTemplate = defaultHighlightTemplate;

Expand Down
11 changes: 5 additions & 6 deletions src/rendering/renderer/fileNameRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import nunjucks, { Environment } from 'nunjucks';
import sanitize from 'sanitize-filename';

import type { Book } from '~/models';
import { shortenTitle } from '~/utils';

import { fileNameTemplateVariables } from './templateVariables';

export default class FileNameRenderer {
private nunjucks: Environment;
Expand All @@ -21,11 +22,9 @@ export default class FileNameRenderer {
}

public render(book: Partial<Book>): string {
const fileName = this.nunjucks.renderString(this.template, {
shortTitle: shortenTitle(book.title),
longTitle: book.title,
author: book.author,
});
const templateVariables = fileNameTemplateVariables(book);

const fileName = this.nunjucks.renderString(this.template, templateVariables);

return `${sanitize(fileName)}.md`;
}
Expand Down
38 changes: 11 additions & 27 deletions src/rendering/renderer/fileRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import moment from 'moment';
import { Environment } from 'nunjucks';

import type { BookHighlight, FileRenderTemplate } from '~/models';
import { shortenTitle } from '~/utils';
import type { BookHighlight } from '~/models';

import { TrimAllEmptyLinesExtension } from '../nunjucks.extensions';
import { generateAppLink } from '../utils';

import HighlightRenderer from './highlightRenderer';
import { fileTemplateVariables } from './templateVariables';

export default class FileRenderer {
private nunjucks: Environment;
Expand All @@ -30,28 +28,14 @@ export default class FileRenderer {
}

public render(entry: BookHighlight): string {
const { book, highlights, metadata } = entry;

const params: FileRenderTemplate = {
title: shortenTitle(book.title),
longTitle: book.title,
author: book.author,
asin: book.asin,
url: book.url,
imageUrl: book.imageUrl,
lastAnnotatedDate: book.lastAnnotatedDate
? moment(book.lastAnnotatedDate).format('YYYY-MM-DD').toString()
: undefined,
appLink: generateAppLink(book.asin),
isbn: metadata?.isbn,
pages: metadata?.pages,
publicationDate: metadata?.publicationDate,
publisher: metadata?.publisher,
authorUrl: metadata?.authorUrl,
highlightsCount: highlights.length,
highlights: highlights.map((h) => this.highlightRenderer.render(h, book)).join('\n'),
};

return this.nunjucks.renderString(this.fileTemplate, params);
const { book, highlights } = entry;

const renderedHighlights = highlights
.map((h) => this.highlightRenderer.render(h, book))
.join('\n');

const templateVariables = fileTemplateVariables(entry, renderedHighlights);

return this.nunjucks.renderString(this.fileTemplate, templateVariables);
}
}
16 changes: 6 additions & 10 deletions src/rendering/renderer/highlightRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Environment } from 'nunjucks';
import dateFilter from 'nunjucks-date-filter';

import type { Book, Highlight, HighlightRenderTemplate } from '~/models';
import type { Book, Highlight } from '~/models';
import highlightTemplateWrapper from '~/rendering//templates/highlightTemplateWrapper.njk';
import { shortenTitle } from '~/utils';

import { BlockReferenceExtension } from '../nunjucks.extensions';
import { generateAppLink, trimMultipleLines } from '../utils';

import { highlightTemplateVariables } from './templateVariables';
import { trimMultipleLines } from './utils';

export const HighlightIdBlockRefPrefix = '^ref-';

Expand All @@ -31,16 +32,11 @@ export default class HighlightRenderer {
}

public render(highlight: Highlight, book: Book): string {
const highlightParams: HighlightRenderTemplate = {
...highlight,
title: shortenTitle(book.title),
longTitle: book.title,
appLink: generateAppLink(book.asin, highlight),
};
const templateVariables = highlightTemplateVariables(highlight, book);

const highlightTemplate = highlightTemplateWrapper.replace('{{ content }}', this.template);

const renderedHighlight = this.nunjucks.renderString(highlightTemplate, highlightParams);
const renderedHighlight = this.nunjucks.renderString(highlightTemplate, templateVariables);

return trimMultipleLines(renderedHighlight);
}
Expand Down
41 changes: 41 additions & 0 deletions src/rendering/renderer/templateVariables.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { AuthorsTemplateVariables, authorsTemplateVariables } from './templateVariables';

describe('authorsTemplateVariables', () => {
it('Breaks one author correctly', () => {
const variables = authorsTemplateVariables('Michael Port');
expect(variables).toEqual<AuthorsTemplateVariables>({
author: 'Michael Port',
authorsLastNames: 'Port',
firstAuthorFirstName: 'Michael',
firstAuthorLastName: 'Port',
secondAuthorFirstName: undefined,
secondAuthorLastName: undefined,
});
});

it('Breaks two authors correctly', () => {
const variables = authorsTemplateVariables('Robert Kegan and Lisa Laskow Lahey');
expect(variables).toEqual<AuthorsTemplateVariables>({
author: 'Robert Kegan and Lisa Laskow Lahey',
authorsLastNames: 'Kegan-Lahey',
firstAuthorFirstName: 'Robert',
firstAuthorLastName: 'Kegan',
secondAuthorFirstName: 'Lisa',
secondAuthorLastName: 'Lahey',
});
});

it('Breaks three authors correctly', () => {
const variables = authorsTemplateVariables(
'Vicki Robin, Joe Dominguez, And Mr. Money Mustache'
);
expect(variables).toEqual<AuthorsTemplateVariables>({
author: 'Vicki Robin, Joe Dominguez, And Mr. Money Mustache',
authorsLastNames: 'Robin_et_al',
firstAuthorFirstName: 'Vicki',
firstAuthorLastName: 'Robin',
secondAuthorFirstName: 'Joe',
secondAuthorLastName: 'Dominguez',
});
});
});
120 changes: 120 additions & 0 deletions src/rendering/renderer/templateVariables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import moment from 'moment';

import type { Book, BookHighlight, Highlight } from '~/models';
import { parseAuthors, shortenTitle } from '~/utils';

import { generateAppLink } from './utils';

export type AuthorsTemplateVariables = {
author: string;
authorsLastNames: string;
firstAuthorFirstName?: string;
firstAuthorLastName: string;
secondAuthorFirstName?: string;
secondAuthorLastName?: string;
};

type CommonTemplateVariables = AuthorsTemplateVariables & {
title: string;
longTitle: string;
};

type FileNameTemplateVariables = CommonTemplateVariables & {
shortTitle: string; // TODO: Eventually deprecate
};

type FileTemplateVariables = CommonTemplateVariables & {
asin?: string;
url?: string;
imageUrl?: string;
lastAnnotatedDate?: string;
appLink?: string;
isbn?: string;
pages: string;
publicationDate: string;
publisher: string;
authorUrl: string;
highlightsCount: number;
highlights: string;
};

type HighlightTemplateVariables = CommonTemplateVariables & {
id: string;
text: string;
location?: string;
page?: string;
note?: string;
color?: 'pink' | 'blue' | 'yellow' | 'orange';
createdDate?: Date;
appLink?: string;
};

export const authorsTemplateVariables = (author: string): AuthorsTemplateVariables => {
const authors = parseAuthors(author);

let authorsLastNames = authors[0].lastName;

if (authors.length == 2) {
authorsLastNames += `-${authors[1].lastName}`;
} else if (authors.length > 2) {
authorsLastNames += `_et_al`;
}

return {
author: author,
authorsLastNames,
firstAuthorFirstName: authors[0].firstName,
firstAuthorLastName: authors[0].lastName,
secondAuthorFirstName: authors[1]?.firstName,
secondAuthorLastName: authors[1]?.lastName,
};
};

export const commonTemplateVariables = (book: Partial<Book>): FileNameTemplateVariables => {
return {
title: shortenTitle(book.title),
shortTitle: shortenTitle(book.title),
longTitle: book.title,
...authorsTemplateVariables(book.author),
};
};

export const fileNameTemplateVariables = (book: Partial<Book>): FileNameTemplateVariables => {
return commonTemplateVariables(book);
};

export const highlightTemplateVariables = (
highlight: Highlight,
book: Book
): HighlightTemplateVariables => {
return {
...highlight,
...commonTemplateVariables(book),
appLink: generateAppLink(book.asin, highlight),
};
};

export const fileTemplateVariables = (
entry: BookHighlight,
renderedHighlights: string
): FileTemplateVariables => {
const { book, highlights, metadata } = entry;

return {
...commonTemplateVariables(book),
asin: book.asin,
url: book.url,
imageUrl: book.imageUrl,
lastAnnotatedDate: book.lastAnnotatedDate
? moment(book.lastAnnotatedDate).format('YYYY-MM-DD').toString()
: undefined,
appLink: generateAppLink(book.asin),
isbn: metadata?.isbn,
pages: metadata?.pages,
publicationDate: metadata?.publicationDate,
publisher: metadata?.publisher,
authorUrl: metadata?.authorUrl,
highlightsCount: highlights.length,
highlights: renderedHighlights,
};
};
File renamed without changes.
Loading

0 comments on commit 2baa566

Please sign in to comment.