Fix applying templates & using templates without plugin

This commit is contained in:
Lost Paul 2025-02-04 21:36:25 +01:00
parent 7aad817dfb
commit e09f190985
2 changed files with 53 additions and 71 deletions

View file

@ -1,7 +1,4 @@
// Credits go to Liam's Periodic Notes Plugin: https://github.com/liamcain/obsidian-periodic-notes and https://github.com/SilentVoid13/Templater
import { TAbstractFile, TFile, TFolder, Vault } from 'obsidian';
import { TextInputSuggest } from './Suggest';
import { TAbstractFile, TFile, TFolder, Vault, AbstractInputSuggest } from 'obsidian';
import FolderNotesPlugin from '../main';
import { getTemplatePlugins } from 'src/template';
export enum FileSuggestMode {
@ -9,12 +6,12 @@ export enum FileSuggestMode {
ScriptFiles,
}
export class TemplateSuggest extends TextInputSuggest<TFile> {
export class TemplateSuggest extends AbstractInputSuggest<TFile> {
constructor(
public inputEl: HTMLInputElement,
plugin: FolderNotesPlugin
public plugin: FolderNotesPlugin
) {
super(inputEl, plugin);
super(plugin.app, inputEl);
}
@ -28,37 +25,48 @@ export class TemplateSuggest extends TextInputSuggest<TFile> {
}
getSuggestions(input_str: string): TFile[] {
const { templateFolder, templaterPlugin } = getTemplatePlugins(this.plugin.app);
if ((!templateFolder || templateFolder?.trim() === '') && !templaterPlugin) {
this.plugin.settings.templatePath = '';
this.plugin.saveSettings();
return [];
}
let folder: TFolder;
if (templaterPlugin) {
folder = this.plugin.app.vault.getAbstractFileByPath(templaterPlugin.plugin?.settings?.templates_folder as string) as TFolder;
} else {
folder = this.plugin.app.vault.getAbstractFileByPath(templateFolder) as TFolder;
}
const files: TFile[] = [];
const { templateFolder, templaterPlugin } = getTemplatePlugins(this.app);
let files: TFile[] = [];
const lower_input_str = input_str.toLowerCase();
Vault.recurseChildren(folder, (file: TAbstractFile) => {
if (file instanceof TFile &&
file.path.toLowerCase().contains(lower_input_str)
) {
files.push(file);
if ((!templateFolder || templateFolder.trim() === '') && !templaterPlugin) {
console.log('Template folder not found');
console.log(lower_input_str)
files = this.plugin.app.vault.getFiles().filter((file) =>
file.path.toLowerCase().includes(lower_input_str)
);
} else {
let folder: TFolder;
if (templaterPlugin) {
folder = this.plugin.app.vault.getAbstractFileByPath(
templaterPlugin.plugin?.settings?.templates_folder as string
) as TFolder;
} else {
folder = this.plugin.app.vault.getAbstractFileByPath(templateFolder) as TFolder;
}
});
Vault.recurseChildren(folder, (file: TAbstractFile) => {
if (file instanceof TFile && file.path.toLowerCase().includes(lower_input_str)) {
files.push(file);
}
});
}
return files;
}
renderSuggestion(file: TFile, el: HTMLElement): void {
el.setText(file.name.replace('.md', ''));
const { templateFolder, templaterPlugin } = getTemplatePlugins(this.app);
if ((!templateFolder || templateFolder.trim() === '') && !templaterPlugin) {
el.setText(`${file.parent?.path}/${file.name.replace('.md', '')}`);
} else {
el.setText(file.name.replace('.md', ''));
}
}
selectSuggestion(file: TFile): void {
this.inputEl.value = file.name.replace('.md', '');

View file

@ -1,9 +1,6 @@
// 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, App, WorkspaceLeaf } from 'obsidian';
import FolderNotesPlugin from './main';
export async function applyTemplate(
plugin: FolderNotesPlugin,
file: TFile,
@ -16,7 +13,6 @@ export async function applyTemplate(
if (templateFile && templateFile instanceof TFile) {
try {
const {
templatesEnabled,
templaterEnabled,
@ -24,37 +20,20 @@ export async function applyTemplate(
templaterPlugin,
} = getTemplatePlugins(plugin.app);
const templateContent = await plugin.app.vault.read(templateFile);
if (templateContent.includes('==⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠==')) { return; }
// If both plugins are enabled, attempt to detect templater first
if (templatesEnabled && templaterEnabled) {
if (/<%/.test(templateContent)) {
return await templaterPlugin.write_template_to_file(
templateFile,
file
);
} else {
if (leaf instanceof WorkspaceLeaf) {
leaf.openFile(file).then(async () => {
return await templatesPlugin.instance.insertTemplate(templateFile, file);
});
}
}
if (templateContent.includes('==⚠ Switch to EXCALIDRAW VIEW in the MORE OPTIONS menu of this document. ⚠==')) {
return;
}
if (templatesEnabled) {
// Prioritize Templater if both plugins are enabled
if (templaterEnabled) {
return await templaterPlugin.write_template_to_file(templateFile, file);
} else if (templatesEnabled) {
if (leaf instanceof WorkspaceLeaf) {
leaf.openFile(file)
await leaf.openFile(file);
}
return await templatesPlugin.instance.insertTemplate(templateFile);
}
if (templaterEnabled) {
return await templaterPlugin.write_template_to_file(
templateFile,
file
);
} else {
await plugin.app.vault.modify(file, templateContent);
}
} catch (e) {
@ -67,19 +46,14 @@ 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 templaterEnabled = (app as any).plugins.enabledPlugins.has('templater-obsidian');
const templaterEmptyFileTemplate =
templaterPlugin &&
(this.app as any).plugins.plugins['templater-obsidian'].settings
?.empty_file_template;
templaterPlugin && templaterPlugin.settings?.empty_file_template;
const templateFolder = templatesEnabled
? templatesPlugin.instance.options.folder
: templaterPlugin
? templaterPlugin.settings.template_folder
: undefined;
: templaterPlugin?.settings.template_folder;
return {
templatesPlugin,