Skip to content
Merged
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
12 changes: 6 additions & 6 deletions src/engine/CaptureChoiceEngine.selection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe("CaptureChoiceEngine selection-as-value resolution", () => {
(engine as any).getFormattedPathToCaptureTo = vi
.fn()
.mockResolvedValue("Test.md");
(engine as any).fileExists = vi.fn().mockResolvedValue(true);
(engine as any).vaultFileService.fileExists = vi.fn().mockResolvedValue(true);
(engine as any).onFileExists = vi.fn().mockResolvedValue({
file,
newFileContent: "content",
Expand Down Expand Up @@ -355,7 +355,7 @@ describe("CaptureChoiceEngine capture target resolution", () => {
createExecutor(),
);

(engine as any).createFileWithInput = vi.fn(async (path: string) => ({
(engine as any).vaultFileService.createFileWithInput = vi.fn(async (path: string) => ({
path,
basename: path.split("/").pop()?.replace(/\.(base|canvas)$/i, "") ?? "",
extension: path.endsWith(".base") ? "base" : "canvas",
Expand Down Expand Up @@ -414,7 +414,7 @@ describe("CaptureChoiceEngine capture target resolution", () => {
newFileContent: "updated",
captureContent: "capture",
}));
(engine as any).fileExists = fileExistsMock;
(engine as any).vaultFileService.fileExists = fileExistsMock;
(engine as any).onFileExists = onFileExistsMock;

await engine.run();
Expand Down Expand Up @@ -473,7 +473,7 @@ describe("CaptureChoiceEngine capture target resolution", () => {
createExecutor(),
);

(engine as any).fileExists = vi.fn(async () => true);
(engine as any).vaultFileService.fileExists = vi.fn(async () => true);
(engine as any).onFileExists = vi.fn(async () => ({
file: linkedFile,
newFileContent: "updated",
Expand Down Expand Up @@ -588,7 +588,7 @@ describe("CaptureChoiceEngine capture target resolution", () => {
}),
);

(engine as any).fileExists = fileExistsMock;
(engine as any).vaultFileService.fileExists = fileExistsMock;
(engine as any).onCreateFileIfItDoesntExist =
onCreateFileIfItDoesntExistMock;

Expand Down Expand Up @@ -636,7 +636,7 @@ describe("CaptureChoiceEngine capture target resolution", () => {

const fileExistsMock = vi.fn();
const onFileExistsMock = vi.fn();
(engine as any).fileExists = fileExistsMock;
(engine as any).vaultFileService.fileExists = fileExistsMock;
(engine as any).onFileExists = onFileExistsMock;

await engine.run();
Expand Down
22 changes: 11 additions & 11 deletions src/engine/CaptureChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
linkOptions?: AppendLinkOptions,
) => Promise<{ file: TFile; newFileContent: string; captureContent: string }>;
let getFileAndAddContentFn: GetFileAndAddContentFn;
const fileAlreadyExists = await this.fileExists(filePath);
const fileAlreadyExists = await this.vaultFileService.fileExists(filePath);

if (fileAlreadyExists) {
getFileAndAddContentFn =
Expand Down Expand Up @@ -504,7 +504,7 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
// 3) trailing "/" => folder picker (explicit)
// 4) known file extension => file
// 5) ambiguous => folder if it exists and no same-name file exists; else file
const normalizedCaptureTo = this.stripLeadingSlash(
const normalizedCaptureTo = this.vaultFileService.stripLeadingSlash(
formattedCaptureTo.trim(),
);

Expand Down Expand Up @@ -540,7 +540,7 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
}

// Guard against ambiguity where a folder and file share the same name.
const fileCandidatePath = this.normalizeMarkdownFilePath("", folderPath);
const fileCandidatePath = this.vaultFileService.normalizeMarkdownFilePath("", folderPath);
const fileCandidate = this.app.vault.getAbstractFileByPath(
fileCandidatePath,
);
Expand Down Expand Up @@ -631,7 +631,7 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
newFileContent: string;
captureContent: string;
}> {
const file: TFile = this.getFileByPath(filePath);
const file: TFile = this.vaultFileService.getFileByPath(filePath);
if (!file) throw new Error("File not found");
Comment thread
chhoumann marked this conversation as resolved.

// Set the title to the existing file's basename
Expand Down Expand Up @@ -736,16 +736,16 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
}

// Create the new file with the (optional) template content
const file: TFile = await this.createFileWithInput(filePath, fileContent, {
const file: TFile = await this.vaultFileService.createFileWithInput(filePath, fileContent, {
suppressTemplaterOnCreate:
this.choice.createFileIfItDoesntExist.createWithTemplate,
});

// Post-process front matter for template property types if we used a template
if (this.choice.createFileIfItDoesntExist.createWithTemplate &&
this.templatePropertyVars &&
this.shouldPostProcessFrontMatter(file, this.templatePropertyVars)) {
await this.postProcessFrontMatter(file, this.templatePropertyVars);
this.frontmatterPropertyService.shouldPostProcessFrontMatter(file, this.templatePropertyVars)) {
await this.frontmatterPropertyService.postProcessFrontMatter(file, this.templatePropertyVars);
}

// Process Templater commands in the template if a template was used
Expand Down Expand Up @@ -786,7 +786,7 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
}

private normalizeCaptureFilePath(path: string): string {
const normalizedPath = this.stripLeadingSlash(path);
const normalizedPath = this.vaultFileService.stripLeadingSlash(path);
if (BASE_FILE_EXTENSION_REGEX.test(normalizedPath)) {
throw new ChoiceAbortError(
`Capture to '.base' files is not supported (${normalizedPath}). Use a Template choice instead.`,
Expand All @@ -799,7 +799,7 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
return normalizedPath;
}

return this.normalizeMarkdownFilePath("", normalizedPath);
return this.vaultFileService.normalizeMarkdownFilePath("", normalizedPath);
}

private mergeCapturePropertyVars(vars: Map<string, unknown>): void {
Expand All @@ -821,15 +821,15 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
return;
}

if (!this.shouldPostProcessFrontMatter(file, this.capturePropertyVars)) {
if (!this.frontmatterPropertyService.shouldPostProcessFrontMatter(file, this.capturePropertyVars)) {
this.capturePropertyVars.clear();
return;
}

log.logMessage(
`CaptureChoiceEngine: Post-processing front matter with ${this.capturePropertyVars.size} capture variables`
);
await this.postProcessFrontMatter(file, this.capturePropertyVars);
await this.frontmatterPropertyService.postProcessFrontMatter(file, this.capturePropertyVars);
this.capturePropertyVars.clear();
}
}
33 changes: 16 additions & 17 deletions src/engine/QuickAddEngine.test.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import { describe, expect, it } from "vitest";
import { QuickAddEngine } from "./QuickAddEngine";
import { VaultFileService } from "../services/VaultFileService";

class TestEngine extends QuickAddEngine {
public constructor() {
super({} as any);
}

public normalize(folderPath: string, fileName: string): string {
return this.normalizeMarkdownFilePath(folderPath, fileName);
}

public run(): void {}
}

describe("QuickAddEngine path normalization", () => {
const engine = new TestEngine();
describe("VaultFileService path normalization", () => {
const service = new VaultFileService({} as any);

it("strips leading slashes from folder and file", () => {
expect(engine.normalize("/daily", "/note")).toBe("daily/note.md");
expect(service.normalizeMarkdownFilePath("/daily", "/note")).toBe(
"daily/note.md",
);
});

it("strips leading slashes from file-only paths", () => {
expect(engine.normalize("", "/review/daily")).toBe("review/daily.md");
expect(service.normalizeMarkdownFilePath("", "/review/daily")).toBe(
"review/daily.md",
);
});

it("omits empty folder prefixes and de-duplicates markdown suffixes", () => {
expect(service.normalizeMarkdownFilePath("", "note.md")).toBe("note.md");
expect(service.normalizeMarkdownFilePath("/folder", "note.md")).toBe(
"folder/note.md",
);
});
});
Loading
Loading