generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from hadynz/feature/author-vars
Add author names as template variables
- Loading branch information
Showing
18 changed files
with
325 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,4 @@ jobs: | |
run: npm run lint | ||
|
||
- name: Run tests | ||
run: npm run test | ||
run: npm run test-verbose |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Oops, something went wrong.