From b3ff9d1a3870b446b8e33ceea0d76b007a3e9297 Mon Sep 17 00:00:00 2001 From: mProjectsCode Date: Fri, 13 May 2022 11:16:22 +0200 Subject: [PATCH] better lists in templates --- README.md | 15 +++++++++++++ src/utils/Utils.ts | 54 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index b999407..a3fd81f 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,21 @@ Allows you to search by an ID that varies from API to API. Concrete info can be 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. + +For arrays there are two special ways of displaying them. +- using `{{ LIST:variable_name }}` will result in + ``` + - element 1 + - element 2 + - element 3 + - ... + ``` +- using `{{ ENUM:variable_name }}` will result in + ``` + element 1, element 2, element 3, ... + ``` + + I also published my own templates [here](https://github.com/mProjectsCode/obsidian-media-db-templates). ### Currently supported media types diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 5bc9a68..906970f 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -17,29 +17,65 @@ export function replaceIllegalFileNameCharactersInString(string: string) { } export function replaceTags(template: string, mediaTypeModel: MediaTypeModel): string { - const resolvedTemplate = template.replace(new RegExp('{{ .*? }}', 'g'), (match: string) => replaceTag(match, mediaTypeModel)); + 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); + tag = tag.substring(2); + tag = tag.substring(0, tag.length - 2); + tag = tag.trim(); - let parts = tag.split('.'); + let parts = tag.split(':'); + if (parts.length === 1) { + let path = parts[0].split('.'); + let obj = traverseMetaData(path, mediaTypeModel); + + if (obj === undefined) { + return '{{ INVALID TEMPLATE TAG }}'; + } + + return obj; + } else if (parts.length === 2) { + let operator = parts[0]; + + let path = parts[1].split('.'); + + let obj = traverseMetaData(path, mediaTypeModel); + + if (obj === undefined) { + return '{{ INVALID TEMPLATE TAG }}'; + } + + if (operator === 'LIST') { + if (!Array.isArray(obj)) { + return '{{ INVALID TEMPLATE TAG - operator LIST is only applicable on an array }}'; + } + return obj.map((e: any) => `- ${e}`).join('\n'); + } else if (operator === 'ENUM') { + if (!Array.isArray(obj)) { + return '{{ INVALID TEMPLATE TAG - operator ENUM is only applicable on an array }}'; + } + return obj.join(', '); + } + + return `{{ INVALID TEMPLATE TAG - unknown operator ${operator} }}`; + } + + return '{{ INVALID TEMPLATE TAG }}'; +} + +function traverseMetaData(path: Array, mediaTypeModel: MediaTypeModel): any { let o: any = mediaTypeModel; - for (let part of parts) { + for (let part of path) { if (o !== undefined) { o = o[part]; } } - if (o === undefined) { - o = '{{ INVALID TEMPLATE TAG }}'; - } - return o; }