Skip to content

Commit

Permalink
Merge pull request #190 from hadynz/feature/highlight-tpl-book-var
Browse files Browse the repository at this point in the history
Add title and longTitle to highlight template variables
  • Loading branch information
hadynz authored Sep 4, 2022
2 parents 2c43793 + f148f6c commit 3d7d024
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 17 deletions.
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.7",
"version": "1.6.8",
"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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-kindle-plugin",
"version": "1.6.7",
"version": "1.6.8",
"description": "Sync your Kindle book highlights using your Amazon login or uploading your My Clippings file",
"main": "src/index.ts",
"repository": {
Expand Down
2 changes: 2 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ export type FileRenderTemplate = {

export type HighlightRenderTemplate = {
id: string;
title: string;
longTitle: string;
text: string;
location?: string;
page?: string;
Expand Down
4 changes: 1 addition & 3 deletions src/rendering/renderer/fileRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ export default class FileRenderer {
publisher: metadata?.publisher,
authorUrl: metadata?.authorUrl,
highlightsCount: highlights.length,
highlights: highlights
.map((h) => this.highlightRenderer.render(h, book.asin))
.join('\n'),
highlights: highlights.map((h) => this.highlightRenderer.render(h, book)).join('\n'),
};

return this.nunjucks.renderString(this.fileTemplate, params);
Expand Down
22 changes: 15 additions & 7 deletions src/rendering/renderer/highlightRenderer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import faker from 'faker';

import type { Highlight } from '~/models';
import type { Book, Highlight } from '~/models';

import HighlightRenderer from './highlightRenderer';

Expand All @@ -18,6 +18,12 @@ describe('HighlightRenderer', () => {
});

describe('render', () => {
const book: Book = {
id: faker.datatype.uuid(),
title: 'Book title',
author: faker.name.findName(),
};

describe('highlight template variables', () => {
const highlight: Highlight = {
id: '123',
Expand All @@ -35,9 +41,11 @@ describe('HighlightRenderer', () => {
['{{note}}', 'my smart note'],
['{{color}}', 'pink'],
['{{createdDate}}', ''],
['{{title}}', 'Book title'],
['{{longTitle}}', 'Book title'],
])('template variable "%s" evaluated as "%s"', (template, expected) => {
const renderer = new HighlightRenderer(template);
expect(renderer.render(highlight)).toBe(expected);
expect(renderer.render(highlight, book)).toBe(expected);
});

it.each([
Expand All @@ -57,20 +65,20 @@ describe('HighlightRenderer', () => {
};

const renderer = new HighlightRenderer(template);
expect(renderer.render(highlight)).toBe(expected);
expect(renderer.render(highlight, book)).toBe(expected);
}
);
});

it('appLink template variable is set when a book has an ASIN value', () => {
const asin = 'A1234';
const myBook: Book = { ...book, asin: 'A1234' };
const highlight: Highlight = {
id: faker.datatype.uuid(),
text: 'highlighted text',
};

const renderer = new HighlightRenderer('{{text}} - {{appLink}}');
expect(renderer.render(highlight, asin)).toMatch(
expect(renderer.render(highlight, myBook)).toMatch(
new RegExp('^highlighted text - kindle://(.*) \\^ref-.*$')
);
});
Expand All @@ -83,7 +91,7 @@ describe('HighlightRenderer', () => {

const renderer = new HighlightRenderer('{{text}} - {{appLink}}');

expect(renderer.render(highlight)).toMatch(
expect(renderer.render(highlight, book)).toMatch(
// eslint-disable-next-line no-regex-spaces
new RegExp('^highlighted text - \\^ref-.*$')
);
Expand All @@ -99,7 +107,7 @@ describe('HighlightRenderer', () => {

const renderer = new HighlightRenderer(templateWithTrailingLines);

expect(renderer.render(highlight)).toMatch(
expect(renderer.render(highlight, book)).toMatch(
new RegExp('^highlighted text \\^ref-.*\\n\\n110$')
);
});
Expand Down
9 changes: 6 additions & 3 deletions src/rendering/renderer/highlightRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Environment } from 'nunjucks';
import dateFilter from 'nunjucks-date-filter';

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

import { BlockReferenceExtension } from '../nunjucks.extensions';
import { generateAppLink, trimMultipleLines } from '../utils';
Expand All @@ -29,10 +30,12 @@ export default class HighlightRenderer {
}
}

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

const highlightTemplate = highlightTemplateWrapper.replace('{{ content }}', this.template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@
<h3>Highlight template variables</h3>
<table>
<tbody>
<tr>
<td><Chip title={'longTitle'} /></td>
<td>Book title - full <span class="mute">- (always set)</span></td>
</tr>
<tr>
<td><Chip title={'title'} /></td>
<td>Book title - short <span class="mute">- (always set)</span></td>
</tr>
<tr>
<tr>
<td><Chip title={'text'} /></td>
<td>Highlighted text<span class="mute">- (always set)</span></td>
Expand Down
4 changes: 2 additions & 2 deletions src/sync/diffManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ export class DiffManager {
.filter((d) => d.nextRenderedHighlight)
.map((d) => ({
line: d.nextRenderedHighlight?.line,
content: highlightRenderer.render(d.remoteHighlight, this.kindleFile.book.asin),
content: highlightRenderer.render(d.remoteHighlight, this.kindleFile.book),
}));

const appendList = diffs
.filter((d) => d.nextRenderedHighlight == null)
.map((d) => highlightRenderer.render(d.remoteHighlight, this.kindleFile.book.asin));
.map((d) => highlightRenderer.render(d.remoteHighlight, this.kindleFile.book));

const modifiedFileContents = this.fileBuffer
.insertLinesAt(insertList)
Expand Down

0 comments on commit 3d7d024

Please sign in to comment.