feat: rely on templater for code execution
This commit is contained in:
parent
f4fcc3df88
commit
3369a4c6cc
2 changed files with 12 additions and 37 deletions
13
src/main.ts
13
src/main.ts
|
|
@ -8,7 +8,7 @@ import {
|
||||||
markdownTable,
|
markdownTable,
|
||||||
replaceIllegalFileNameCharactersInString,
|
replaceIllegalFileNameCharactersInString,
|
||||||
unCamelCase,
|
unCamelCase,
|
||||||
executeInlineScriptsTemplates,
|
hasTemplaterPlugin,
|
||||||
useTemplaterPluginInFile,
|
useTemplaterPluginInFile,
|
||||||
} from './utils/Utils';
|
} from './utils/Utils';
|
||||||
import { OMDbAPI } from './api/apis/OMDbAPI';
|
import { OMDbAPI } from './api/apis/OMDbAPI';
|
||||||
|
|
@ -376,10 +376,13 @@ export default class MediaDbPlugin extends Plugin {
|
||||||
frontMatter.dataSource = mediaTypeModel.dataSource;
|
frontMatter.dataSource = mediaTypeModel.dataSource;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only support stringifyYaml for templater plugin
|
if (hasTemplaterPlugin(this.app)) {
|
||||||
fileContent = `---\n${stringifyYaml(frontMatter)}---\n${fileContent}`;
|
// Only support stringifyYaml for templater plugin
|
||||||
|
// Include the media variable in all templater commands by using a top level JavaScript execution command.
|
||||||
fileContent = executeInlineScriptsTemplates(mediaTypeModel, fileContent);
|
fileContent = `---\n<%* const media = ${JSON.stringify(mediaTypeModel)} %>\n${stringifyYaml(frontMatter)}---\n${fileContent}`;
|
||||||
|
} else {
|
||||||
|
fileContent = `---\n${this.settings.useCustomYamlStringifier ? YAMLConverter.toYaml(frontMatter) : stringifyYaml(frontMatter)}---\n` + fileContent;
|
||||||
|
}
|
||||||
|
|
||||||
return fileContent;
|
return fileContent;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { MediaTypeModel } from '../models/MediaTypeModel';
|
import { MediaTypeModel } from '../models/MediaTypeModel';
|
||||||
import { TFile, TFolder } from 'obsidian';
|
import { TFile, TFolder, App } from 'obsidian';
|
||||||
|
|
||||||
export const pluginName: string = 'obsidian-media-db-plugin';
|
export const pluginName: string = 'obsidian-media-db-plugin';
|
||||||
export const contactEmail: string = 'm.projects.code@gmail.com';
|
export const contactEmail: string = 'm.projects.code@gmail.com';
|
||||||
|
|
@ -203,38 +203,10 @@ export function unCamelCase(str: string): string {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copied from https://github.com/anpigon/obsidian-book-search-plugin
|
export function hasTemplaterPlugin(app: App) {
|
||||||
// Licensed under the MIT license. Copyright (c) 2020 Jake Runzer
|
const templater = app.plugins.plugins['templater-obsidian'];
|
||||||
export function getFunctionConstructor(): typeof Function {
|
|
||||||
try {
|
|
||||||
return new Function('return (function(){}).constructor')();
|
|
||||||
} catch (err) {
|
|
||||||
console.warn(err);
|
|
||||||
if (err instanceof SyntaxError) {
|
|
||||||
throw Error('Bad template syntax');
|
|
||||||
} else {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Modified from https://github.com/anpigon/obsidian-book-search-plugin
|
return !!templater;
|
||||||
// Licensed under the MIT license. Copyright (c) 2020 Jake Runzer
|
|
||||||
export function executeInlineScriptsTemplates(media: MediaTypeModel, text: string) {
|
|
||||||
const commandRegex = /<%(?:=)(.+)%>/g;
|
|
||||||
const ctor = getFunctionConstructor();
|
|
||||||
const matchedList = [...text.matchAll(commandRegex)];
|
|
||||||
return matchedList.reduce((result, [matched, script]) => {
|
|
||||||
try {
|
|
||||||
const outputs = new ctor(
|
|
||||||
['const [media] = arguments', `const output = ${script}`, 'if(typeof output === "string") return output', 'return JSON.stringify(output)'].join(';'),
|
|
||||||
)(media);
|
|
||||||
return result.replace(matched, outputs);
|
|
||||||
} catch (err) {
|
|
||||||
console.warn(err);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}, text);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copied from https://github.com/anpigon/obsidian-book-search-plugin
|
// Copied from https://github.com/anpigon/obsidian-book-search-plugin
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue