Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying custom extension in create_file #1456

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/core/Templater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ export class Templater {
template: TFile | string,
folder?: TFolder | string,
filename?: string,
open_new_note = true
open_new_note = true,
extension?: string
): Promise<TFile | undefined> {
// TODO: Maybe there is an obsidian API function for that
if (!folder) {
Expand All @@ -136,8 +137,8 @@ export class Templater {
}
}

const extension =
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the case, as you can tell from this line that overrides the extension in the newly created note

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only if you're providing a string template, as opposed to providing a file for the template. If you're providing a file then it will use the same extension as the provided file. I've used this for .canvas files in the past. Are you generating a string in code and creating a file using that generated string?

I'm hesitant to add yet another argument to this function if it isn't strictly needed, it has a lot of arguments already, and I don't suspect this argument would be used much since most use cases can already be covered.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My usecase isn't too niche (I believe), I have a script that prompts for a filename and creates a file named "yyyy-mm-dd-filename" (which I'd like to be able to support arbitrary extensions)

template instanceof TFile ? template.extension || "md" : "md";
extension = 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ export class InternalModuleFile extends InternalModule {
template: TFile | string,
filename: string,
open_new: boolean,
folder?: TFolder | string
folder?: TFolder | string,
extension?: string
) => Promise<TFile | undefined> {
return async (
template: TFile | string,
filename: string,
open_new = false,
folder?: TFolder | string
folder?: TFolder | string,
extension?: string
) => {
this.create_new_depth += 1;
if (this.create_new_depth > DEPTH_LIMIT) {
Expand All @@ -84,7 +86,8 @@ export class InternalModuleFile extends InternalModule {
template,
folder,
filename,
open_new
open_new,
extension
);

this.create_new_depth -= 1;
Expand Down