Skip to content

Commit

Permalink
chore: fix basic template service
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Dec 17, 2024
1 parent 4509c65 commit 49038ea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
28 changes: 19 additions & 9 deletions src/core/template/BasicTemplateService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TE } from "@std";
import { App, normalizePath } from "obsidian";
import { App, normalizePath, TFile } from "obsidian";
import { Logger } from "src/utils/Logger";
import { TemplateError } from "./TemplateError";
import { TemplateService } from "./TemplateService";
Expand All @@ -14,13 +14,23 @@ export class BasicTemplateService implements TemplateService {
) {}

createNoteFromTemplate = (
template: string,
targetPath: string,
templateContent: string,
targetFolder: string,
filename: string,
openNewNote: boolean,
): TE.TaskEither<TemplateError, void> =>
TE.tryCatch(
async () => {
await this.app.vault.create(normalizePath(targetPath), template);
},
TemplateError.of("Error creating note from template"),
);
TE.tryCatch(async () => {
const fullPath = normalizePath(`${targetFolder}/${filename}.md`);
await this.app.vault.create(fullPath, templateContent);
if (openNewNote) {
const file = this.app.vault.getAbstractFileByPath(fullPath);
if (!file) {
this.logger.error("File not found", fullPath);
return;
}
if (file instanceof TFile) {
await this.app.workspace.getLeaf("split").openFile(file);
}
}
}, TemplateError.of("Error creating note from template"));
}
2 changes: 0 additions & 2 deletions src/core/template/TemplateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { TemplateError } from "./TemplateError";
export interface TemplateService {
/**
* Creates a note from a template content
* @param template The template content
* @param targetPath Path where the new note should be created
*/
createNoteFromTemplate(
templateContent: string,
Expand Down

0 comments on commit 49038ea

Please sign in to comment.