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

Auto-generated block references added end of annotation line #97

Merged
merged 4 commits into from
Nov 8, 2021
Merged
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
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.0.5",
"version": "1.1.0",
"minAppVersion": "0.10.2",
"description": "Sync your Kindle book highlights using your Amazon login or uploading your My Clippings file",
"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.0.5",
"version": "1.1.0",
"description": "Sync your Kindle book highlights using your Amazon login or uploading your My Clippings file",
"main": "src/index.ts",
"repository": {
Expand Down
32 changes: 19 additions & 13 deletions src/renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@ import { get } from 'svelte/store';

import bookTemplate from './templates/bookTemplate.njk';
import defaultHighlightTemplate from './templates/defaultHighlightTemplate.njk';
import { TrimAllEmptyLinesExtension } from './nunjucks.extensions';
import highlightTemplateWrapper from './templates/highlightTemplateWrapper.njk';
import { BlockReferenceExtension, TrimAllEmptyLinesExtension } from './nunjucks.extensions';
import { sanitizeTitle } from '~/utils';
import { settingsStore } from '~/store';
import { trimMultipleLines } from './helper';
import type { Book, BookHighlight, Highlight, RenderTemplate } from '~/models';

export const HighlightIdBlockRefPrefix = '^ref-';

const appLink = (book: Book, highlight?: Highlight): string => {
if (book.asin == null) {
return null;
}
if (highlight != null) {
return `kindle://book?action=open&asin=${book.asin}&location=${highlight.location}`;
}
return `kindle://book?action=open&asin=${book.asin}`;
};

export class Renderer {
private nunjucks: Environment;

constructor() {
this.nunjucks = new nunjucks.Environment(null, { autoescape: false });
this.nunjucks.addExtension('BlockRef', new BlockReferenceExtension());
this.nunjucks.addExtension('Trim', new TrimAllEmptyLinesExtension());
}

Expand All @@ -35,13 +47,11 @@ export class Renderer {
public render(entry: BookHighlight): string {
const { book, highlights } = entry;

const appLink = book.asin ? `kindle://book?action=open&asin=${book.asin}` : null;

const params: RenderTemplate = {
...book,
fullTitle: book.title,
title: sanitizeTitle(book.title),
appLink,
appLink: appLink(book),
...entry.metadata,
highlights: this.renderHighlights(book, highlights),
};
Expand All @@ -50,20 +60,16 @@ export class Renderer {
}

public renderHighlight(book: Book, highlight: Highlight): string {
const appLink = book.asin
? `kindle://book?action=open&asin=${book.asin}&location=${highlight.location}`
: null;

const highlightParams = { ...highlight, appLink };
const highlightParams = { ...highlight, appLink: appLink(book, highlight) };

const userTemplate =
get(settingsStore).highlightTemplate || this.defaultHighlightTemplate();

const renderedHighlight = this.nunjucks.renderString(userTemplate, highlightParams);
const trimmedHighlight = trimMultipleLines(renderedHighlight);
const highlightTemplate = highlightTemplateWrapper.replace('{{content}}', userTemplate);

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

// Surround all highlights with a block reference to enable re-sync functionality
return `${HighlightIdBlockRefPrefix}${highlight.id}\n${trimmedHighlight}`;
return trimMultipleLines(renderedHighlight);
}

private renderHighlights(book: Book, highlights: Highlight[]): string {
Expand Down
74 changes: 71 additions & 3 deletions src/renderer/nunjucks.extensions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import nunjucks from 'nunjucks';

import { HighlightIdBlockRefPrefix } from '~/renderer';
import { sb } from '~/utils';

type SubClass = {
lineno: number;
colno: number;
value: string;
children?: SubClass[];
};

type ParsedSignature = {
children: SubClass[];
};

type Context = {
ctx: Record<string, string>;
};

function TrimAllEmptyLinesExtension(): void {
this.tags = ['trim'];

this.parse = function (parser, nodes) {
const tok = parser.nextToken(); // Get the tag token

// Parse the args and move after the block end.
const args = parser.parseSignature(null, true);
const args: ParsedSignature = parser.parseSignature(null, true);
parser.advanceAfterBlockEnd(tok.value);

// Parse the body
const body = parser.parseUntilBlocks('trim', 'endtrim');
const body: ParsedSignature = parser.parseUntilBlocks('trim', 'endtrim');
parser.advanceAfterBlockEnd();

// Actually do work on block body and arguments
Expand All @@ -25,4 +43,54 @@ function TrimAllEmptyLinesExtension(): void {
};
}

export { TrimAllEmptyLinesExtension };
/**
* // TODO: description goes here...
* {% blockref "text", "id" %}
* ...
* {% endblockref %}
*/
function BlockReferenceExtension(): void {
this.tags = ['blockref'];

this.parse = function (parser, nodes) {
const tok = parser.nextToken(); // Get the tag token

// Parse the args and move after the block end.
const args: ParsedSignature = parser.parseSignature(null, true);
parser.advanceAfterBlockEnd(tok.value);

// Parse the body
const body: ParsedSignature = parser.parseUntilBlocks('blockref', 'endblockref');
parser.advanceAfterBlockEnd();

// Find line number of argument 1 variable in template
const needle = args.children[0].value;

const needleSubclass = body.children.find(
(c) => c.children?.length > 0 && c.children[0].value === needle
);

this.lineNumber = needleSubclass.lineno;

// Actually do work on block body and arguments
return new nodes.CallExtension(this, 'run', args, [body]);
};

this.run = function (
context: Context,
_needle: keyof Context['ctx'],
highlightId: keyof Context['ctx'],
bodyCallback: () => string
) {
const renderedTemplate: string = bodyCallback();
const buffer = sb(renderedTemplate);

const blockRef = `${HighlightIdBlockRefPrefix}${context.ctx[highlightId]}`;
const blockRefSuffixLine = `${buffer.getLine(this.lineNumber + 1)} ${blockRef}`;

buffer.replace({ line: this.lineNumber, content: blockRefSuffixLine });
return new nunjucks.runtime.SafeString(buffer.toString());
};
}

export { BlockReferenceExtension, TrimAllEmptyLinesExtension };
3 changes: 3 additions & 0 deletions src/renderer/templates/highlightTemplateWrapper.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% blockref "text", "id" %}
{{content}}
{% endblockref %}
15 changes: 10 additions & 5 deletions src/sync/diffManager/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _ from 'lodash';

import { sb, StringBuffer } from '~/utils';
import { HighlightIdBlockRefPrefix, Renderer } from '~/renderer';
import { diffLists } from './helpers';
Expand Down Expand Up @@ -43,13 +45,16 @@ export class DiffManager {
}

private async parseRenderedHighlights(): Promise<RenderedHighlight[]> {
const needle = _.escapeRegExp(HighlightIdBlockRefPrefix);
const endsWithRegex = new RegExp(`.*(${needle}.*)$`);

return this.fileBuffer
.find((lineEntry) => lineEntry.content.startsWith(HighlightIdBlockRefPrefix))
.map((lineEntry): RenderedHighlight => {
const { line, content } = lineEntry;
.match(endsWithRegex)
.filter((lem) => lem.match != null)
.map((lem) => {
return {
line,
highlightId: content.replace(HighlightIdBlockRefPrefix, ''),
line: lem.line,
highlightId: lem.match[1].replace(HighlightIdBlockRefPrefix, ''),
};
});
}
Expand Down
16 changes: 16 additions & 0 deletions src/utils/stringBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ type LineEntry = {
content: string;
};

type LineEntryMatch = LineEntry & {
match: RegExpMatchArray;
};

export class StringBuffer {
private lines: string[];

Expand All @@ -24,6 +28,13 @@ export class StringBuffer {
.filter(predicate);
}

public match(regex: RegExp): LineEntryMatch[] {
return this.lines.map((content, index) => {
const match = content.match(regex);
return { line: index + 1, content, match };
});
}

public insertLinesAt(newLines: LineEntry[]): StringBuffer {
if (newLines.some((l) => l.line <= 0)) {
throw new Error('Line numbers must start from 1');
Expand All @@ -37,6 +48,11 @@ export class StringBuffer {
return this;
}

public replace(line: LineEntry): StringBuffer {
this.lines[line.line] = line.content;
return this;
}

public append(newLines: string[]): StringBuffer {
this.lines.push(...newLines);
return this;
Expand Down