Skip to content
Open
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
67 changes: 62 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,77 @@ The plugin allows you to set a template note that gets added to the end of any n
The plugin also offers simple template tags, for example `{{ title }}`, which will be replaced by the title of the media being imported.
Note that template tags are surrounded by two curly braces and spaces. The spaces inside the curly braces are important!

For arrays, there are two special ways of displaying them:
**Array Operators**

For arrays, there are several operators available for different formatting needs:

Body Text Operators (no quotes)

- `{{ LIST:variable_name }}` - Unlinked list:

- using `{{ LIST:variable_name }}` will result in:
```
- element 1
- element 2
- element 3
- ...
```
- using `{{ ENUM:variable_name }}` will result in:

- `{{ LIST:[[variable_name]] }}` - Wikilinked list:

```
- [[element 1]]
- [[element 2]]
- [[element 3]]
```

- `{{ ENUM:variable_name }}` - Unlinked comma-separated:

```
element 1, element 2, element 3
```

- `{{ ENUM:[[variable_name]] }}` - Wikilinked comma-separated:
```
[[element 1]], [[element 2]], [[element 3]]
```
element 1, element 2, element 3, ...

YAML Frontmatter Operators (with quotes)

- `{{ LISTYAML:variable_name }}` - Unlinked YAML list:

```yaml
variable_name:
- element 1
- element 2
- element 3
```

- `{{ LISTYAML:[[variable_name]] }}` - Wikilinked YAML list (recognized by Obsidian):

```yaml
variable_name:
- '[[element 1]]'
- '[[element 2]]'
- '[[element 3]]'
```

- `{{ ENUMYAML:variable_name }}` - Comma-separated for YAML (no wikilink support):
```yaml
variable_name: element 1, element 2, element 3
```

Single Element Operators

- `{{ FIRST:variable_name }}` - Returns the first element of an array
- `{{ FIRST:[[variable_name]] }}` - Returns the first element as a wikilink: `[[element 1]]`
- `{{ LAST:variable_name }}` - Returns the last element of an array
- `{{ LAST:[[variable_name]] }}` - Returns the last element as a wikilink: `[[element 3]]`

**Wikilink Support**

Wrap the variable name in double brackets `[[variable_name]]` to create wikilinks for array elements. This works with `LIST`, `LISTYAML`, `ENUM`, `FIRST`, and `LAST` operators. Note that `ENUMYAML` does not support wikilinks - the `[[]]` syntax will be ignored.

**Important:** Use `YAML` variants (`LISTYAML`, `ENUMYAML`) in frontmatter and regular variants (`LIST`, `ENUM`) in the note body.

Available variables that can be used in template tags are any front-matter properties.

I also published my own templates [here](https://github.com/mProjectsCode/obsidian-media-db-templates).
Expand Down
Binary file modified bun.lockb
Binary file not shown.
6 changes: 3 additions & 3 deletions src/settings/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ export class MediaDbSettingTab extends PluginSettingTab {
apiKeyGroup.addSetting(
setting =>
void setting
.setName('OMDb API key')
.setName('OMDb API Key')
.setDesc('API key for "www.omdbapi.com".')
// .addComponent((el) => {
// let component = new SecretComponent(this.app, el);
Expand All @@ -559,7 +559,7 @@ export class MediaDbSettingTab extends PluginSettingTab {
.setName('TMDB API Token')
.setDesc('API Read Access Token for "https://www.themoviedb.org".')
.addText(cb => {
cb.setPlaceholder('API key')
cb.setPlaceholder('API token')
.setValue(this.plugin.settings.TMDBKey)
.onChange(data => {
this.plugin.settings.TMDBKey = data;
Expand All @@ -570,7 +570,7 @@ export class MediaDbSettingTab extends PluginSettingTab {
apiKeyGroup.addSetting(
setting =>
void setting
.setName('Moby Games key')
.setName('Moby Games Key')
.setDesc('API key for "www.mobygames.com".')
.addText(cb => {
cb.setPlaceholder('API key')
Expand Down
59 changes: 54 additions & 5 deletions src/utils/Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ function replaceTag(match: string, mediaTypeModel: MediaTypeModel, ignoreUndefin
tag = tag.trim();

const parts = tag.split(':');

if (parts.length === 1) {
const path = parts[0].split('.');

const obj = traverseMetaData(path, mediaTypeModel);

if (obj === undefined) {
Expand All @@ -48,9 +48,18 @@ function replaceTag(match: string, mediaTypeModel: MediaTypeModel, ignoreUndefin
return obj?.toString() ?? 'null';
} else if (parts.length === 2) {
const operator = parts[0];
let pathString = parts[1];

const path = parts[1].split('.');
// Check if the path is wrapped in [[]] for wikilinks
const wikilinkMatch = pathString.match(/^\[\[(.+?)\]\]$/);
const useWikilinks = !!wikilinkMatch;

// Extract the actual path (remove [[ ]] if present)
if (wikilinkMatch) {
pathString = wikilinkMatch[1];
}

const path = pathString.split('.');
const obj = traverseMetaData(path, mediaTypeModel);

if (obj === undefined) {
Expand All @@ -62,26 +71,66 @@ function replaceTag(match: string, mediaTypeModel: MediaTypeModel, ignoreUndefin
return '{{ INVALID TEMPLATE TAG - operator LIST is only applicable on an array }}';
}

return obj.map((e: unknown) => `- ${e}`).join('\n');
if (useWikilinks) {
// LIST with wikilinks: no quotes for body text
return obj.map((e: unknown) => `\n - [[${e}]]`).join('');
} else {
return obj.map((e: unknown) => `\n - ${e}`).join('');
}
} else if (operator === 'LISTYAML') {
if (!Array.isArray(obj)) {
return '{{ INVALID TEMPLATE TAG - operator LISTYAML is only applicable on an array }}';
}

if (useWikilinks) {
// LISTYAML with wikilinks: quoted for frontmatter
return obj.map((e: unknown) => `\n - "[[${e}]]"`).join('');
} else {
return obj.map((e: unknown) => `\n - ${e}`).join('');
}
} else if (operator === 'ENUM') {
if (!Array.isArray(obj)) {
return '{{ INVALID TEMPLATE TAG - operator ENUM is only applicable on an array }}';
}

if (useWikilinks) {
// ENUM with wikilinks: no quotes for body text
return obj.map((e: unknown) => `[[${e}]]`).join(', ');
} else {
return obj.join(', ');
}
} else if (operator === 'ENUMYAML') {
if (!Array.isArray(obj)) {
return '{{ INVALID TEMPLATE TAG - operator ENUMYAML is only applicable on an array }}';
}
// ENUMYAML - always comma-separated, no wikilinks support and [[]] syntax is simply ignored since they are not valid in comma-separated YAML values
return obj.join(', ');
} else if (operator === 'FIRST') {
if (!Array.isArray(obj)) {
return '{{ INVALID TEMPLATE TAG - operator FIRST is only applicable on an array }}';
}

const first = obj[0] as unknown;
return first?.toString() ?? 'null';
const value = first?.toString() ?? 'null';

if (useWikilinks && value !== 'null') {
return `[[${value}]]`;
} else {
return value;
}
} else if (operator === 'LAST') {
if (!Array.isArray(obj)) {
return '{{ INVALID TEMPLATE TAG - operator LAST is only applicable on an array }}';
}

const last = obj[obj.length - 1] as unknown;
return last?.toString() ?? 'null';
const value = last?.toString() ?? 'null';

if (useWikilinks && value !== 'null') {
return `[[${value}]]`;
} else {
return value;
}
}

return `{{ INVALID TEMPLATE TAG - unknown operator ${operator} }}`;
Expand Down