templates \o/

This commit is contained in:
mProjectsCode 2022-05-13 10:09:07 +02:00
parent a70e576822
commit 1b31436e58
4 changed files with 62 additions and 19 deletions

View file

@ -9,6 +9,12 @@ Search a movie, series, anime or game by its name across multiple APIs.
#### Search by ID #### 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. 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 ### Currently supported media types
- movies (including specials) - movies (including specials)
- series (including OVAs) - series (including OVAs)

View file

@ -3,7 +3,7 @@ import {DEFAULT_SETTINGS, MediaDbPluginSettings, MediaDbSettingTab} from './sett
import {APIManager} from './api/APIManager'; import {APIManager} from './api/APIManager';
import {TestAPI} from './api/apis/TestAPI'; import {TestAPI} from './api/apis/TestAPI';
import {MediaTypeModel} from './models/MediaTypeModel'; import {MediaTypeModel} from './models/MediaTypeModel';
import {replaceIllegalFileNameCharactersInString} from './utils/Utils'; import {replaceIllegalFileNameCharactersInString, replaceTags} from './utils/Utils';
import {OMDbAPI} from './api/apis/OMDbAPI'; import {OMDbAPI} from './api/apis/OMDbAPI';
import {MediaDbAdvancedSearchModal} from './modals/MediaDbAdvancedSearchModal'; import {MediaDbAdvancedSearchModal} from './modals/MediaDbAdvancedSearchModal';
import {MediaDbSearchResultModal} from './modals/MediaDbSearchResultModal'; import {MediaDbSearchResultModal} from './modals/MediaDbSearchResultModal';
@ -60,27 +60,23 @@ export default class MediaDbPlugin extends Plugin {
let fileContent = `---\n${data.toMetaData()}---\n`; let fileContent = `---\n${data.toMetaData()}---\n`;
let templateFile: TFile = null;
if (data.type === 'movie' && this.settings.movieTemplate) { if (data.type === 'movie' && this.settings.movieTemplate) {
const templateFile = this.app.vault.getFiles().filter((f: TFile) => f.name === this.settings.movieTemplate).first(); 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;
}
} else if (data.type === 'series' && this.settings.seriesTemplate) { } else if (data.type === 'series' && this.settings.seriesTemplate) {
const templateFile = this.app.vault.getFiles().filter((f: TFile) => f.name === this.settings.seriesTemplate).first(); templateFile = this.app.vault.getFiles().filter((f: TFile) => f.name === this.settings.seriesTemplate).first();
if (templateFile) { } else if (data.type === 'game' && this.settings.gameTemplate) {
let template = await this.app.vault.read(templateFile); templateFile = this.app.vault.getFiles().filter((f: TFile) => f.name === this.settings.gameTemplate).first();
// console.log(template); }
fileContent += template;
} if (templateFile) {
} else if (data.type === 'game' && this.settings.seriesTemplate) { let template = await this.app.vault.read(templateFile);
const templateFile = this.app.vault.getFiles().filter((f: TFile) => f.name === this.settings.gameTemplate).first(); // console.log(template);
if (templateFile) { if (this.settings.templates) {
let template = await this.app.vault.read(templateFile); template = replaceTags(template, data);
// console.log(template);
fileContent += template;
} }
fileContent += template;
} }
const fileName = replaceIllegalFileNameCharactersInString(data.getFileName()); const fileName = replaceIllegalFileNameCharactersInString(data.getFileName());

View file

@ -12,6 +12,7 @@ export interface MediaDbPluginSettings {
movieTemplate: string, movieTemplate: string,
seriesTemplate: string, seriesTemplate: string,
gameTemplate: string, gameTemplate: string,
templates: boolean,
} }
export const DEFAULT_SETTINGS: MediaDbPluginSettings = { export const DEFAULT_SETTINGS: MediaDbPluginSettings = {
@ -21,6 +22,7 @@ export const DEFAULT_SETTINGS: MediaDbPluginSettings = {
movieTemplate: '', movieTemplate: '',
seriesTemplate: '', seriesTemplate: '',
gameTemplate: '', gameTemplate: '',
templates: true,
}; };
export class MediaDbSettingTab extends PluginSettingTab { 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) new Setting(containerEl)
.setName('Movie template') .setName('Movie template')
.setDesc('Template file to be used when creating a new note for a movie.') .setDesc('Template file to be used when creating a new note for a movie.')

View file

@ -15,3 +15,31 @@ export function getFileName(item: MediaTypeModel) {
export function replaceIllegalFileNameCharactersInString(string: string) { export function replaceIllegalFileNameCharactersInString(string: string) {
return string.replace(/[\\,#%&{}/*<>$"@.]*/g, '').replace(/:+/g, ' -'); 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;
}