From 1b31436e58ae77eb077a372ad82a670dbc73d5cf Mon Sep 17 00:00:00 2001 From: mProjectsCode Date: Fri, 13 May 2022 10:09:07 +0200 Subject: [PATCH] templates \o/ --- README.md | 6 ++++++ src/main.ts | 34 +++++++++++++++------------------- src/settings/Settings.ts | 13 +++++++++++++ src/utils/Utils.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index f82bd26..b999407 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,12 @@ Search a movie, series, anime or game by its name across multiple APIs. #### Search by ID Allows you to search by an ID that varies from API to API. Concrete info can be found in the description of the individual APIs. +#### Templates +The plugin allows you to set a template note that gets added to the end of any note created by this plugin. +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. +Note that "template tags" are surrounded with two curly braces and that the spaces inside the curly braces are important. +I also published my own templates [here](https://github.com/mProjectsCode/obsidian-media-db-templates). + ### Currently supported media types - movies (including specials) - series (including OVAs) diff --git a/src/main.ts b/src/main.ts index a2e82a6..5cad2c0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,7 +3,7 @@ import {DEFAULT_SETTINGS, MediaDbPluginSettings, MediaDbSettingTab} from './sett import {APIManager} from './api/APIManager'; import {TestAPI} from './api/apis/TestAPI'; import {MediaTypeModel} from './models/MediaTypeModel'; -import {replaceIllegalFileNameCharactersInString} from './utils/Utils'; +import {replaceIllegalFileNameCharactersInString, replaceTags} from './utils/Utils'; import {OMDbAPI} from './api/apis/OMDbAPI'; import {MediaDbAdvancedSearchModal} from './modals/MediaDbAdvancedSearchModal'; import {MediaDbSearchResultModal} from './modals/MediaDbSearchResultModal'; @@ -60,27 +60,23 @@ export default class MediaDbPlugin extends Plugin { let fileContent = `---\n${data.toMetaData()}---\n`; + let templateFile: TFile = null; + if (data.type === 'movie' && this.settings.movieTemplate) { - const templateFile = this.app.vault.getFiles().filter((f: TFile) => f.name === this.settings.movieTemplate).first(); - if (templateFile) { - let template = await this.app.vault.read(templateFile); - // console.log(template); - fileContent += template; - } + templateFile = this.app.vault.getFiles().filter((f: TFile) => f.name === this.settings.movieTemplate).first(); } else if (data.type === 'series' && this.settings.seriesTemplate) { - const templateFile = this.app.vault.getFiles().filter((f: TFile) => f.name === this.settings.seriesTemplate).first(); - if (templateFile) { - let template = await this.app.vault.read(templateFile); - // console.log(template); - fileContent += template; - } - } else if (data.type === 'game' && this.settings.seriesTemplate) { - const templateFile = this.app.vault.getFiles().filter((f: TFile) => f.name === this.settings.gameTemplate).first(); - if (templateFile) { - let template = await this.app.vault.read(templateFile); - // console.log(template); - fileContent += template; + templateFile = this.app.vault.getFiles().filter((f: TFile) => f.name === this.settings.seriesTemplate).first(); + } else if (data.type === 'game' && this.settings.gameTemplate) { + templateFile = this.app.vault.getFiles().filter((f: TFile) => f.name === this.settings.gameTemplate).first(); + } + + if (templateFile) { + let template = await this.app.vault.read(templateFile); + // console.log(template); + if (this.settings.templates) { + template = replaceTags(template, data); } + fileContent += template; } const fileName = replaceIllegalFileNameCharactersInString(data.getFileName()); diff --git a/src/settings/Settings.ts b/src/settings/Settings.ts index 8f27dac..8b85f32 100644 --- a/src/settings/Settings.ts +++ b/src/settings/Settings.ts @@ -12,6 +12,7 @@ export interface MediaDbPluginSettings { movieTemplate: string, seriesTemplate: string, gameTemplate: string, + templates: boolean, } export const DEFAULT_SETTINGS: MediaDbPluginSettings = { @@ -21,6 +22,7 @@ export const DEFAULT_SETTINGS: MediaDbPluginSettings = { movieTemplate: '', seriesTemplate: '', gameTemplate: '', + templates: true, }; export class MediaDbSettingTab extends PluginSettingTab { @@ -51,6 +53,17 @@ export class MediaDbSettingTab extends PluginSettingTab { }); }); + new Setting(containerEl) + .setName('Resolve {{ tags }} in templates') + .setDesc('Whether to resolve {{ tags }} in templates. The spaces inside the curly braces are important.') + .addToggle(cb => { + cb.setValue(this.plugin.settings.templates) + .onChange(data => { + this.plugin.settings.templates = data; + this.plugin.saveSettings(); + }); + }); + new Setting(containerEl) .setName('Movie template') .setDesc('Template file to be used when creating a new note for a movie.') diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 4e277c9..5bc9a68 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -15,3 +15,31 @@ export function getFileName(item: MediaTypeModel) { export function replaceIllegalFileNameCharactersInString(string: string) { return string.replace(/[\\,#%&{}/*<>$"@.]*/g, '').replace(/:+/g, ' -'); } + +export function replaceTags(template: string, mediaTypeModel: MediaTypeModel): string { + const resolvedTemplate = template.replace(new RegExp('{{ .*? }}', 'g'), (match: string) => replaceTag(match, mediaTypeModel)); + + return resolvedTemplate; +} + +function replaceTag(match: string, mediaTypeModel: MediaTypeModel): string { + let tag = match; + tag = tag.substring(3); + tag = tag.substring(0, tag.length - 3); + + let parts = tag.split('.'); + + let o: any = mediaTypeModel; + + for (let part of parts) { + if (o !== undefined) { + o = o[part]; + } + } + + if (o === undefined) { + o = '{{ INVALID TEMPLATE TAG }}'; + } + + return o; +}