|
1 | 1 | import { MediaTypeModel } from '../models/MediaTypeModel'; |
2 | | -import { TFile, TFolder } from 'obsidian'; |
| 2 | +import { TFile, TFolder, App } from 'obsidian'; |
3 | 3 |
|
4 | 4 | export const pluginName: string = 'obsidian-media-db-plugin'; |
5 | 5 | export const contactEmail: string = '[email protected]'; |
@@ -203,38 +203,10 @@ export function unCamelCase(str: string): string { |
203 | 203 | ); |
204 | 204 | } |
205 | 205 |
|
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']; |
220 | 208 |
|
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; |
238 | 210 | } |
239 | 211 |
|
240 | 212 | // Copied from https://github.com/anpigon/obsidian-book-search-plugin |
|
0 commit comments