Skip to content

Commit

Permalink
Expose publicationDate in FileNameTemplateVariables
Browse files Browse the repository at this point in the history
  • Loading branch information
cewood committed May 20, 2023
1 parent 99cab6e commit 76c3e14
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 19 deletions.
9 changes: 5 additions & 4 deletions src/fileManager/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MetadataCache, normalizePath, TAbstractFile, TFile, TFolder, Vault } from 'obsidian';

import type { Book, KindleFile, KindleFrontmatter } from '~/models';
import type { Book, BookMetadata, KindleFile, KindleFrontmatter } from '~/models';
import { mergeFrontmatter } from '~/utils';

import { bookFilePath, bookToFrontMatter, frontMatterToBook } from './mappers';
Expand Down Expand Up @@ -52,10 +52,11 @@ export default class FileManager {

public async createFile(
book: Book,
metadata: BookMetadata,
content: string,
highlightsCount: number
): Promise<void> {
const filePath = this.generateUniqueFilePath(book);
const filePath = this.generateUniqueFilePath(book, metadata);
const frontmatterContent = this.generateBookContent(book, content, highlightsCount);

try {
Expand Down Expand Up @@ -92,8 +93,8 @@ export default class FileManager {
});
}

private generateUniqueFilePath(book: Book): string {
const filePath = bookFilePath(book);
private generateUniqueFilePath(book: Book, metadata: BookMetadata): string {
const filePath = bookFilePath(book, metadata);

const isDuplicate = this.vault
.getMarkdownFiles()
Expand Down
6 changes: 3 additions & 3 deletions src/fileManager/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import moment from 'moment';
import path from 'path';
import { get } from 'svelte/store';

import type { Book, KindleFrontmatter } from '~/models';
import type { Book, BookMetadata, KindleFrontmatter } from '~/models';
import { getRenderers } from '~/rendering';
import { settingsStore } from '~/store';

/**
* Returns a file path for a given book relative to the current Obsidian
* vault directory.
*/
export const bookFilePath = (book: Book): string => {
const fileName = getRenderers().fileNameRenderer.render(book);
export const bookFilePath = (book: Book, metadata: BookMetadata): string => {
const fileName = getRenderers().fileNameRenderer.render(book, metadata);
return path.join(get(settingsStore).highlightsFolder, fileName);
};

Expand Down
30 changes: 26 additions & 4 deletions src/rendering/renderer/fileNameRenderer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Book } from '~/models';
import type { Book, BookMetadata } from '~/models';

import FileNameRenderer from './fileNameRenderer';

Expand All @@ -25,28 +25,50 @@ describe('FileNameRenderer', () => {
const book: Partial<Book> = {
title: 'Immunity to change: How to overcome it',
};
const metadata: Partial<BookMetadata> = {
publicationDate: '2010'
};

const renderer = new FileNameRenderer('{{shortTitle}}');
expect(renderer.render(book)).toBe('Immunity to change.md');
expect(renderer.render(book, metadata)).toBe('Immunity to change.md');
});

it('File name with book title as is', () => {
const book: Partial<Book> = {
title: 'Immunity to change: How to overcome it',
};
const metadata: Partial<BookMetadata> = {
publicationDate: '2010'
};

const renderer = new FileNameRenderer('{{longTitle}}');
expect(renderer.render(book)).toBe('Immunity to change How to overcome it.md');
expect(renderer.render(book, metadata)).toBe('Immunity to change How to overcome it.md');
});

it('File name with author', () => {
const book: Partial<Book> = {
title: 'Immunity to change: How to overcome it',
author: 'John Doe',
};
const metadata: Partial<BookMetadata> = {
publicationDate: '2010'
};

const renderer = new FileNameRenderer('{{author}}');
expect(renderer.render(book)).toBe('John Doe.md');
expect(renderer.render(book, metadata)).toBe('John Doe.md');
});

it('File name with publication date', () => {
const book: Partial<Book> = {
title: 'Immunity to change: How to overcome it',
author: 'John Doe',
};
const metadata: Partial<BookMetadata> = {
publicationDate: '2010'
};

const renderer = new FileNameRenderer('{{publicationDate}} - {{author}}');
expect(renderer.render(book, metadata)).toBe('2010 - John Doe.md');
});
});
});
6 changes: 3 additions & 3 deletions src/rendering/renderer/fileNameRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nunjucks, { Environment } from 'nunjucks';
import sanitize from 'sanitize-filename';

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

import { fileNameTemplateVariables } from './templateVariables';

Expand All @@ -21,8 +21,8 @@ export default class FileNameRenderer {
}
}

public render(book: Partial<Book>): string {
const templateVariables = fileNameTemplateVariables(book);
public render(book: Partial<Book>, metadata: Partial<BookMetadata>): string {
const templateVariables = fileNameTemplateVariables(book, metadata);

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

Expand Down
13 changes: 10 additions & 3 deletions src/rendering/renderer/templateVariables.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import moment from 'moment';

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

import { generateAppLink } from './utils';
Expand All @@ -21,6 +21,7 @@ type CommonTemplateVariables = AuthorsTemplateVariables & {

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

type FileTemplateVariables = CommonTemplateVariables & {
Expand Down Expand Up @@ -79,8 +80,14 @@ export const commonTemplateVariables = (book: Partial<Book>): FileNameTemplateVa
};
};

export const fileNameTemplateVariables = (book: Partial<Book>): FileNameTemplateVariables => {
return commonTemplateVariables(book);
export const fileNameTemplateVariables = (
book: Partial<Book>,
metadata: Partial<BookMetadata>
): FileNameTemplateVariables => {
return {
publicationDate: metadata?.publicationDate,
...commonTemplateVariables(book),
};
};

export const highlightTemplateVariables = (
Expand Down
9 changes: 9 additions & 0 deletions src/settings/templateEditorModal/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const data: BookHighlight[] = [
title: 'Animal Farm (Classics To Go)',
author: 'George Orwell',
},
metadata: {
publicationDate: '1945',
},
highlights: [
{
id: '1-A',
Expand All @@ -24,6 +27,9 @@ const data: BookHighlight[] = [
title: 'An Everyone Culture',
author: 'Robert Kegan and Lisa Laskow Lahey',
},
metadata: {
publicationDate: '2016',
},
highlights: [
{
id: '2-A',
Expand All @@ -45,6 +51,9 @@ const data: BookHighlight[] = [
title: 'The Girl on the Train: A Novel',
author: 'Paula Hawkins',
},
metadata: {
publicationDate: '2015',
},
highlights: [
{
id: '3-A',
Expand Down
2 changes: 1 addition & 1 deletion src/settings/templateEditorModal/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default (): TemplateEditorModalStore => {
const fileNameTemplate = $fileNameTemplateField || DefaultFileNameTemplate;
try {
const renderer = new FileNameRenderer(fileNameTemplate);
return renderer.render($selectedBook.book);
return renderer.render($selectedBook.book, $selectedBook.metadata);
} catch (error) {
return InvalidRender;
}
Expand Down
2 changes: 1 addition & 1 deletion src/sync/syncManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class SyncManager {

const content = getRenderers().fileRenderer.render({ book, highlights, metadata });

await this.fileManager.createFile(book, content, highlights.length);
await this.fileManager.createFile(book, metadata, content, highlights.length);
}

private async syncMetadata(book: Book): Promise<BookMetadata> {
Expand Down

0 comments on commit 76c3e14

Please sign in to comment.