generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
templateVariables.ts
120 lines (104 loc) · 3.08 KB
/
templateVariables.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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,
};
};