Skip to content

Commit 3369a4c

Browse files
committed
feat: rely on templater for code execution
1 parent f4fcc3d commit 3369a4c

File tree

2 files changed

+12
-37
lines changed

2 files changed

+12
-37
lines changed

src/main.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
markdownTable,
99
replaceIllegalFileNameCharactersInString,
1010
unCamelCase,
11-
executeInlineScriptsTemplates,
11+
hasTemplaterPlugin,
1212
useTemplaterPluginInFile,
1313
} from './utils/Utils';
1414
import { OMDbAPI } from './api/apis/OMDbAPI';
@@ -376,10 +376,13 @@ export default class MediaDbPlugin extends Plugin {
376376
frontMatter.dataSource = mediaTypeModel.dataSource;
377377
}
378378

379-
// Only support stringifyYaml for templater plugin
380-
fileContent = `---\n${stringifyYaml(frontMatter)}---\n${fileContent}`;
381-
382-
fileContent = executeInlineScriptsTemplates(mediaTypeModel, fileContent);
379+
if (hasTemplaterPlugin(this.app)) {
380+
// Only support stringifyYaml for templater plugin
381+
// Include the media variable in all templater commands by using a top level JavaScript execution command.
382+
fileContent = `---\n<%* const media = ${JSON.stringify(mediaTypeModel)} %>\n${stringifyYaml(frontMatter)}---\n${fileContent}`;
383+
} else {
384+
fileContent = `---\n${this.settings.useCustomYamlStringifier ? YAMLConverter.toYaml(frontMatter) : stringifyYaml(frontMatter)}---\n` + fileContent;
385+
}
383386

384387
return fileContent;
385388
}

src/utils/Utils.ts

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MediaTypeModel } from '../models/MediaTypeModel';
2-
import { TFile, TFolder } from 'obsidian';
2+
import { TFile, TFolder, App } from 'obsidian';
33

44
export const pluginName: string = 'obsidian-media-db-plugin';
55
export const contactEmail: string = '[email protected]';
@@ -203,38 +203,10 @@ export function unCamelCase(str: string): string {
203203
);
204204
}
205205

206-
// Copied from https://github.com/anpigon/obsidian-book-search-plugin
207-
// Licensed under the MIT license. Copyright (c) 2020 Jake Runzer
208-
export function getFunctionConstructor(): typeof Function {
209-
try {
210-
return new Function('return (function(){}).constructor')();
211-
} catch (err) {
212-
console.warn(err);
213-
if (err instanceof SyntaxError) {
214-
throw Error('Bad template syntax');
215-
} else {
216-
throw err;
217-
}
218-
}
219-
}
206+
export function hasTemplaterPlugin(app: App) {
207+
const templater = app.plugins.plugins['templater-obsidian'];
220208

221-
// Modified from https://github.com/anpigon/obsidian-book-search-plugin
222-
// Licensed under the MIT license. Copyright (c) 2020 Jake Runzer
223-
export function executeInlineScriptsTemplates(media: MediaTypeModel, text: string) {
224-
const commandRegex = /<%(?:=)(.+)%>/g;
225-
const ctor = getFunctionConstructor();
226-
const matchedList = [...text.matchAll(commandRegex)];
227-
return matchedList.reduce((result, [matched, script]) => {
228-
try {
229-
const outputs = new ctor(
230-
['const [media] = arguments', `const output = ${script}`, 'if(typeof output === "string") return output', 'return JSON.stringify(output)'].join(';'),
231-
)(media);
232-
return result.replace(matched, outputs);
233-
} catch (err) {
234-
console.warn(err);
235-
}
236-
return result;
237-
}, text);
209+
return !!templater;
238210
}
239211

240212
// Copied from https://github.com/anpigon/obsidian-book-search-plugin

0 commit comments

Comments
 (0)