generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #356 from danielo515/feat/templater-in-templates
Feat/templater-in-templates
- Loading branch information
Showing
12 changed files
with
434 additions
and
32 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"alwaysUpdateLinks": true, | ||
"attachmentFolderPath": "attachments" | ||
"attachmentFolderPath": "attachments", | ||
"promptDelete": false | ||
} |
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
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
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,36 @@ | ||
import { TE } from "@std"; | ||
import { App, normalizePath, TFile } 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 = ( | ||
templateContent: string, | ||
targetFolder: string, | ||
filename: string, | ||
openNewNote: boolean, | ||
): TE.TaskEither<TemplateError, void> => | ||
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")); | ||
} |
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,14 @@ | ||
import { TE } from "@std"; | ||
import { TemplateError } from "./TemplateError"; | ||
|
||
export interface TemplateService { | ||
/** | ||
* Creates a note from a template content | ||
*/ | ||
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,50 @@ | ||
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; | ||
const result = await this.templaterApi.create_new_note_from_template( | ||
templateContent, | ||
targetFolder, | ||
title, | ||
openNewNote, | ||
); | ||
if (result === undefined) { | ||
throw new Error("Templater API returned undefined, probably a parsing error"); | ||
} | ||
}, | ||
(e) => | ||
e instanceof Error | ||
? TemplateError.of(e.message)(e) | ||
: TemplateError.of("Unknown error")(e), | ||
); | ||
} |
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"; |
Oops, something went wrong.