generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): template service to use any available template engine to …
…post-process templates
- Loading branch information
1 parent
f75b1e4
commit a3f9d4e
Showing
6 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |