Skip to content

Commit

Permalink
[CHG] Makes template-file checks more lenient
Browse files Browse the repository at this point in the history
If the `template-file` can't be found, the plugin will search the
template folder set in Templates or Templater, respectively
  • Loading branch information
czottmann committed Mar 27, 2024
1 parent 40a55fb commit 5da464a
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/routes/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ import {
zodAlwaysFalse,
zodEmptyStringChangedToDefaultString,
zodExistingNotePath,
zodExistingTemplaterPath,
zodExistingTemplatesPath,
zodOptionalBoolean,
zodSanitizedNotePath,
zodUndefinedChangedToDefaultValue,
Expand Down Expand Up @@ -102,11 +104,11 @@ const createParams = z.discriminatedUnion("apply", [
}),
createBaseParams.extend({
apply: z.literal("templater"),
"template-file": zodExistingNotePath,
"template-file": zodExistingTemplaterPath,
}),
createBaseParams.extend({
apply: z.literal("templates"),
"template-file": zodExistingNotePath,
"template-file": zodExistingTemplatesPath,
}),
createBaseParams.extend({
apply: zodEmptyStringChangedToDefaultString("content"),
Expand Down
7 changes: 4 additions & 3 deletions src/routes/periodic-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ import { focusOrOpenFile } from "../utils/ui";
import {
zodAlwaysFalse,
zodEmptyStringChangedToDefaultString,
zodExistingNotePath,
zodExistingTemplaterPath,
zodExistingTemplatesPath,
zodOptionalBoolean,
zodUndefinedChangedToDefaultValue,
} from "../utils/zod";
Expand Down Expand Up @@ -95,11 +96,11 @@ const createParams = z.discriminatedUnion("apply", [
}),
createBaseParams.extend({
apply: z.literal("templater"),
"template-file": zodExistingNotePath,
"template-file": zodExistingTemplaterPath,
}),
createBaseParams.extend({
apply: z.literal("templates"),
"template-file": zodExistingNotePath,
"template-file": zodExistingTemplatesPath,
}),
createBaseParams.extend({
apply: zodEmptyStringChangedToDefaultString("content"),
Expand Down
104 changes: 92 additions & 12 deletions src/utils/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { z } from "zod";
import { TAbstractFile, TFile, TFolder } from "obsidian";
import { activeVault } from "./file-handling";
import { sanitizeFilePath } from "./file-handling";
import { getEnabledCommunityPlugin, getEnabledCorePlugin } from "./plugins";

// The absence of a parameter `blah`, a `blah=false` and a value-less `blah=`
// should all be treated as `false`. My reign shall be merciful.
Expand Down Expand Up @@ -100,6 +101,28 @@ export const zodExistingNotePath = z.preprocess(
z.instanceof(TFile, { message: "Note doesn't exist" }),
);

/**
* A schema which tests the passed-in string to see if it's a valid path to an
* existing template. If it is, returns a `TFile` instance.
*/
export const zodExistingTemplaterPath = z.preprocess(
lookupAbstractFileForTemplaterPath,
z.instanceof(TFile, {
message: "Template doesn't exist or Templater isn't enabled",
}),
);

/**
* A schema which tests the passed-in string to see if it's a valid path to an
* existing template. If it is, returns a `TFile` instance.
*/
export const zodExistingTemplatesPath = z.preprocess(
lookupAbstractFileForTemplatesPath,
z.instanceof(TFile, {
message: "Template doesn't exist or Templates isn't enabled",
}),
);

/**
* A schema which tests the passed-in string to see if it's a valid path to an
* existing file. If it is, returns a `TFile` instance.
Expand Down Expand Up @@ -158,12 +181,9 @@ export const zodEmptyStringChangedToDefaultString = (defaultString: string) =>
* @param path Any incoming zod parameter
*/
function lookupAbstractFileForNotePath(path: any): TAbstractFile | null {
if (typeof path !== "string" || !path) {
return null;
}

const filepath = sanitizeFilePath(path as string);
return activeVault().getAbstractFileByPath(filepath);
return (typeof path === "string" && path.length > 0)
? sanitizeFilePathAndGetAbstractFile(path)
: null;
}

/**
Expand All @@ -174,25 +194,85 @@ function lookupAbstractFileForNotePath(path: any): TAbstractFile | null {
* @param path Any incoming zod parameter
*/
function lookupAbstractFileForFilePath(path: any): TAbstractFile | null {
return (typeof path === "string" && path.length > 0)
? sanitizeFilePathAndGetAbstractFile(path, false)
: null;
}

/**
* Takes an incoming parameter and returns the corresponding `TAbstractFile` if
* the parameter is a string and the string corresponds to an existing file or
* folder. Otherwise returns `null`.
*
* @param path Any incoming zod parameter
*/
function lookupAbstractFolderForPath(path: any): TAbstractFile | null {
return (typeof path === "string" && path.length > 0)
? activeVault().getAbstractFileByPath(path as string)
: null;
}

function sanitizeFilePathAndGetAbstractFile(
path: string,
isNote?: boolean,
): TAbstractFile | null {
return activeVault().getAbstractFileByPath(sanitizeFilePath(path, isNote));
}

/**
* Takes an incoming parameter and returns the corresponding `TAbstractFile` if
* the parameter is a string and the string corresponds to an existing template
* file. If the passed in path can't be found, the function will also check
* Templater's template folder path for the file. Returns `null` when the search
* came up empty.
*
* @param path Any incoming zod parameter
* @returns
*/
function lookupAbstractFileForTemplaterPath(path: any): TAbstractFile | null {
if (typeof path !== "string" || !path) {
return null;
}

const filepath = sanitizeFilePath(path as string, false);
return activeVault().getAbstractFileByPath(filepath);
const abstractFile = sanitizeFilePathAndGetAbstractFile(path, false) ||
sanitizeFilePathAndGetAbstractFile(`${path}.md`, false);
if (abstractFile) return abstractFile;

const res = getEnabledCommunityPlugin("templater-obsidian");
if (res.isSuccess) {
const folder = res.result.settings?.templates_folder;
return sanitizeFilePathAndGetAbstractFile(`${folder}/${path}`, false) ||
sanitizeFilePathAndGetAbstractFile(`${folder}/${path}.md`, false);
}

return null;
}

/**
* Takes an incoming parameter and returns the corresponding `TAbstractFile` if
* the parameter is a string and the string corresponds to an existing file or
* folder. Otherwise returns `null`.
* the parameter is a string and the string corresponds to an existing template
* file. If the passed in path can't be found, the function will also check
* Templates' template folder path for the file. Returns `null` when the search
* came up empty.
*
* @param path Any incoming zod parameter
* @returns
*/
function lookupAbstractFolderForPath(path: any): TAbstractFile | null {
function lookupAbstractFileForTemplatesPath(path: any): TAbstractFile | null {
if (typeof path !== "string" || !path) {
return null;
}

return activeVault().getAbstractFileByPath(path as string);
const abstractFile = sanitizeFilePathAndGetAbstractFile(path, false) ||
sanitizeFilePathAndGetAbstractFile(`${path}.md`, false);
if (abstractFile) return abstractFile;

const res = getEnabledCorePlugin("templates");
if (res.isSuccess) {
const folder = res.result.options?.folder;
return sanitizeFilePathAndGetAbstractFile(`${folder}/${path}`, false) ||
sanitizeFilePathAndGetAbstractFile(`${folder}/${path}.md`, false);
}

return null;
}

0 comments on commit 5da464a

Please sign in to comment.