Skip to content

Commit b3ff9d1

Browse files
committed
better lists in templates
1 parent 1b31436 commit b3ff9d1

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@ Allows you to search by an ID that varies from API to API. Concrete info can be
1313
The plugin allows you to set a template note that gets added to the end of any note created by this plugin.
1414
The plugin also offers simple "template tgs". E.g. if the template includes `{{ title }}`, it will be replaced by the title of the movie, show or game.
1515
Note that "template tags" are surrounded with two curly braces and that the spaces inside the curly braces are important.
16+
17+
For arrays there are two special ways of displaying them.
18+
- using `{{ LIST:variable_name }}` will result in
19+
```
20+
- element 1
21+
- element 2
22+
- element 3
23+
- ...
24+
```
25+
- using `{{ ENUM:variable_name }}` will result in
26+
```
27+
element 1, element 2, element 3, ...
28+
```
29+
30+
1631
I also published my own templates [here](https://github.com/mProjectsCode/obsidian-media-db-templates).
1732

1833
### Currently supported media types

src/utils/Utils.ts

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,65 @@ export function replaceIllegalFileNameCharactersInString(string: string) {
1717
}
1818

1919
export function replaceTags(template: string, mediaTypeModel: MediaTypeModel): string {
20-
const resolvedTemplate = template.replace(new RegExp('{{ .*? }}', 'g'), (match: string) => replaceTag(match, mediaTypeModel));
20+
const resolvedTemplate = template.replace(new RegExp('{{.*?}}', 'g'), (match: string) => replaceTag(match, mediaTypeModel));
2121

2222
return resolvedTemplate;
2323
}
2424

2525
function replaceTag(match: string, mediaTypeModel: MediaTypeModel): string {
2626
let tag = match;
27-
tag = tag.substring(3);
28-
tag = tag.substring(0, tag.length - 3);
27+
tag = tag.substring(2);
28+
tag = tag.substring(0, tag.length - 2);
29+
tag = tag.trim();
2930

30-
let parts = tag.split('.');
31+
let parts = tag.split(':');
32+
if (parts.length === 1) {
33+
let path = parts[0].split('.');
3134

35+
let obj = traverseMetaData(path, mediaTypeModel);
36+
37+
if (obj === undefined) {
38+
return '{{ INVALID TEMPLATE TAG }}';
39+
}
40+
41+
return obj;
42+
} else if (parts.length === 2) {
43+
let operator = parts[0];
44+
45+
let path = parts[1].split('.');
46+
47+
let obj = traverseMetaData(path, mediaTypeModel);
48+
49+
if (obj === undefined) {
50+
return '{{ INVALID TEMPLATE TAG }}';
51+
}
52+
53+
if (operator === 'LIST') {
54+
if (!Array.isArray(obj)) {
55+
return '{{ INVALID TEMPLATE TAG - operator LIST is only applicable on an array }}';
56+
}
57+
return obj.map((e: any) => `- ${e}`).join('\n');
58+
} else if (operator === 'ENUM') {
59+
if (!Array.isArray(obj)) {
60+
return '{{ INVALID TEMPLATE TAG - operator ENUM is only applicable on an array }}';
61+
}
62+
return obj.join(', ');
63+
}
64+
65+
return `{{ INVALID TEMPLATE TAG - unknown operator ${operator} }}`;
66+
}
67+
68+
return '{{ INVALID TEMPLATE TAG }}';
69+
}
70+
71+
function traverseMetaData(path: Array<string>, mediaTypeModel: MediaTypeModel): any {
3272
let o: any = mediaTypeModel;
3373

34-
for (let part of parts) {
74+
for (let part of path) {
3575
if (o !== undefined) {
3676
o = o[part];
3777
}
3878
}
3979

40-
if (o === undefined) {
41-
o = '{{ INVALID TEMPLATE TAG }}';
42-
}
43-
4480
return o;
4581
}

0 commit comments

Comments
 (0)