diff --git a/README.md b/README.md index 7cd31f3..8769bad 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/bun.lockb b/bun.lockb index 419cad8..cef7de2 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/src/settings/Settings.ts b/src/settings/Settings.ts index 6ccb6b3..65263f0 100644 --- a/src/settings/Settings.ts +++ b/src/settings/Settings.ts @@ -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); @@ -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; @@ -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') diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 9b25b34..4584698 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -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) { @@ -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) { @@ -62,11 +71,39 @@ 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)) { @@ -74,14 +111,26 @@ function replaceTag(match: string, mediaTypeModel: MediaTypeModel, ignoreUndefin } 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} }}`;