Skip to content

Commit

Permalink
feat: Support string for folder argument for tp.file.create_new()
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachatoo committed May 8, 2024
1 parent 7378d7b commit df3709b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
12 changes: 8 additions & 4 deletions docs/documentation.toml
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ example = "<% tp.file.content %>"
[tp.file.functions.create_new]
name = "create_new"
description = "Creates a new file using a specified template or with a specified content."
definition = "tp.file.create_new(template: TFile ⎮ string, filename?: string, open_new: boolean = false, folder?: TFolder)"
definition = "tp.file.create_new(template: TFile ⎮ string, filename?: string, open_new: boolean = false, folder?: TFolder | string)"

[[tp.file.functions.create_new.args]]
name = "template"
Expand All @@ -201,7 +201,7 @@ description = "Whether to open or not the newly created file. Warning: if you us

[[tp.file.functions.create_new.args]]
name = "folder"
description = "The folder to put the new file in, defaults to Obsidian's default location. If you want the file to appear in a different folder, specify it with `app.vault.getAbstractFileByPath(\"FOLDERNAME\")`."
description = "The folder to put the new file in, defaults to Obsidian's default location. If you want the file to appear in a different folder, specify it with `"PATH/TO/FOLDERNAME"` or `app.vault.getAbstractFileByPath(\"PATH/TO/FOLDERNAME\")`."

[[tp.file.functions.create_new.examples]]
name = "File creation"
Expand All @@ -217,10 +217,14 @@ example = """<%* await tp.file.create_new("MyFileContent", "MyFilename", true) %

[[tp.file.functions.create_new.examples]]
name = "File creation in current folder"
example = """<%* await tp.file.create_new("MyFileContent", "MyFilename", false, tp.file.folder()) %>"""
example = """<%* await tp.file.create_new("MyFileContent", "MyFilename", false, tp.file.folder(true)) %>"""

[[tp.file.functions.create_new.examples]]
name = "File creation in specified folder"
name = "File creation in specified folder with string path"
example = """<%* await tp.file.create_new("MyFileContent", "MyFilename", false, "Path/To/MyFolder") %>"""

[[tp.file.functions.create_new.examples]]
name = "File creation in specified folder with TFolder"
example = """<%* await tp.file.create_new("MyFileContent", "MyFilename", false, app.vault.getAbstractFileByPath("MyFolder")) %>"""

[[tp.file.functions.create_new.examples]]
Expand Down
7 changes: 3 additions & 4 deletions src/core/Templater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class Templater {

async create_new_note_from_template(
template: TFile | string,
folder?: TFolder,
folder?: TFolder | string,
filename?: string,
open_new_note = true
): Promise<TFile | undefined> {
Expand Down Expand Up @@ -140,10 +140,9 @@ export class Templater {
const extension =
template instanceof TFile ? template.extension || "md" : "md";
const created_note = await errorWrapper(async () => {
const folderPath = folder instanceof TFolder ? folder.path : folder;
const path = app.vault.getAvailablePath(
normalizePath(
`${folder?.path ?? ""}/${filename || "Untitled"}`
),
normalizePath(`${folderPath ?? ""}/${filename || "Untitled"}`),
extension
);
const folder_path = get_folder_path_from_file_path(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export class InternalModuleFile extends InternalModule {
template: TFile | string,
filename: string,
open_new: boolean,
folder?: TFolder
folder?: TFolder | string
) => Promise<TFile | undefined> {
return async (
template: TFile | string,
filename: string,
open_new = false,
folder?: TFolder
folder?: TFolder | string
) => {
this.create_new_depth += 1;
if (this.create_new_depth > DEPTH_LIMIT) {
Expand Down

0 comments on commit df3709b

Please sign in to comment.