mirror of
https://github.com/lostpaul/obsidian-folder-notes.git
synced 2026-07-22 07:40:24 +00:00
92 lines
No EOL
3.1 KiB
TypeScript
92 lines
No EOL
3.1 KiB
TypeScript
// Thanks to @mgmeyers for the creating the obsidian kanban plugin https://github.com/mgmeyers/obsidian-kanban
|
|
// from where I got the template code for this plugin
|
|
// https://github.com/mgmeyers/obsidian-kanban/blob/48e6c278ce9140b7e034b181432321f697d6e45e/src/components/helpers.ts
|
|
|
|
import { TFile, MarkdownView, App } from 'obsidian';
|
|
import FolderNotesPlugin from './main';
|
|
export async function applyTemplate(
|
|
plugin: FolderNotesPlugin,
|
|
templatePath?: string
|
|
) {
|
|
const templateFile = templatePath
|
|
? plugin.app.vault.getAbstractFileByPath(templatePath)
|
|
: null;
|
|
|
|
if (templateFile && templateFile instanceof TFile) {
|
|
const activeView = app.workspace.getActiveViewOfType(MarkdownView);
|
|
|
|
try {
|
|
// Force the view to source mode, if needed
|
|
if (activeView?.getMode() !== 'source') {
|
|
await activeView?.setState(
|
|
{
|
|
...activeView.getState(),
|
|
mode: 'source',
|
|
},
|
|
{}
|
|
);
|
|
}
|
|
|
|
const {
|
|
templatesEnabled,
|
|
templaterEnabled,
|
|
templatesPlugin,
|
|
templaterPlugin,
|
|
} = getTemplatePlugins(plugin.app);
|
|
|
|
const templateContent = await plugin.app.vault.read(templateFile);
|
|
|
|
// If both plugins are enabled, attempt to detect templater first
|
|
if (templatesEnabled && templaterEnabled) {
|
|
if (/<%/.test(templateContent)) {
|
|
return await templaterPlugin.append_template_to_active_file(
|
|
templateFile
|
|
);
|
|
}
|
|
|
|
return await templatesPlugin.instance.insertTemplate(templateFile);
|
|
}
|
|
|
|
if (templatesEnabled) {
|
|
return await templatesPlugin.instance.insertTemplate(templateFile);
|
|
}
|
|
|
|
if (templaterEnabled) {
|
|
return await templaterPlugin.append_template_to_active_file(
|
|
templateFile
|
|
);
|
|
}
|
|
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function getTemplatePlugins(app: App) {
|
|
const templatesPlugin = (app as any).internalPlugins.plugins.templates;
|
|
const templatesEnabled = templatesPlugin.enabled;
|
|
const templaterPlugin = (app as any).plugins.plugins['templater-obsidian'];
|
|
const templaterEnabled = (app as any).plugins.enabledPlugins.has(
|
|
'templater-obsidian'
|
|
);
|
|
const templaterEmptyFileTemplate =
|
|
templaterPlugin &&
|
|
(this.app as any).plugins.plugins['templater-obsidian'].settings
|
|
?.empty_file_template;
|
|
|
|
const templateFolder = templatesEnabled
|
|
? templatesPlugin.instance.options.folder
|
|
: templaterPlugin
|
|
? templaterPlugin.settings.template_folder
|
|
: undefined;
|
|
|
|
return {
|
|
templatesPlugin,
|
|
templatesEnabled,
|
|
templaterPlugin: templaterPlugin?.templater,
|
|
templaterEnabled,
|
|
templaterEmptyFileTemplate,
|
|
templateFolder,
|
|
};
|
|
} |