Skip to content

Commit

Permalink
feat(core): template service to use any available template engine to …
Browse files Browse the repository at this point in the history
…post-process templates
  • Loading branch information
danielo515 committed Dec 17, 2024
1 parent f75b1e4 commit a3f9d4e
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/core/template/BasicTemplateService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TE } from "@std";
import { App, normalizePath } from "obsidian";
import { Logger } from "src/utils/Logger";
import { TemplateError } from "./TemplateError";
import { TemplateService } from "./TemplateService";

/**
* Basic template service that creates notes with unchanged content
*/
export class BasicTemplateService implements TemplateService {
constructor(
private app: App,
private logger: Logger,
) {}

createNoteFromTemplate = (
template: string,
targetPath: string,
): TE.TaskEither<TemplateError, void> =>
TE.tryCatch(
async () => {
await this.app.vault.create(normalizePath(targetPath), template);
},
TemplateError.of("Error creating note from template"),
);
}
14 changes: 14 additions & 0 deletions src/core/template/TemplateError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export class TemplateError extends Error {
public readonly _tag = "TemplateError";
constructor(
message: string,
public readonly cause?: unknown,
) {
super(message);
this.name = "TemplateError";
}

static of(message: string) {
return (cause: unknown) => new TemplateError(message, cause);
}
}
16 changes: 16 additions & 0 deletions src/core/template/TemplateService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TE } from "@std";
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,
targetFolder: string,
filename: string,
openNewNote: boolean,
): TE.TaskEither<TemplateError, void>;
}
41 changes: 41 additions & 0 deletions src/core/template/TemplaterService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { TE } from "@std";
import { App } from "obsidian";
import { Logger } from "src/utils/Logger";
import { TemplateError } from "./TemplateError";
import { TemplateService } from "./TemplateService";

export interface TemplaterApi {
create_new_note_from_template: (
content: string,
folder: string,
title: string,
openNewNote: boolean,
) => Promise<void>;
}

/**
* Template service that uses the Templater plugin
*/
export class TemplaterService implements TemplateService {
constructor(
private app: App,
private logger: Logger,
private templaterApi: TemplaterApi,
) {}

createNoteFromTemplate = (
templateContent: string,
targetFolder: string,
filename: string,
openNewNote: boolean,
): TE.TaskEither<TemplateError, void> =>
TE.tryCatch(async () => {
const title = filename;
await this.templaterApi.create_new_note_from_template(
templateContent,
targetFolder,
title,
openNewNote,
);
}, TemplateError.of("Error creating note from template"));
}
16 changes: 16 additions & 0 deletions src/core/template/getTemplateService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { App } from "obsidian";
import { Logger } from "src/utils/Logger";
import { BasicTemplateService } from "./BasicTemplateService";
import { TemplaterService } from "./TemplaterService";
import { TemplateService } from "./TemplateService";

export function getTemplateService(app: App, logger: Logger): TemplateService {
const templaterApi = app.plugins.plugins["templater-obsidian"]?.templater;
if (templaterApi) {
logger.debug("Using Templater plugin for templates");
return new TemplaterService(app, logger, templaterApi);
}

logger.debug("Using basic template service");
return new BasicTemplateService(app, logger);
}
5 changes: 5 additions & 0 deletions src/core/template/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./TemplateService";
export * from "./TemplateError";
export * from "./BasicTemplateService";
export * from "./TemplaterService";
export * from "./getTemplateService";

0 comments on commit a3f9d4e

Please sign in to comment.