diff --git a/src/engine/CaptureChoiceEngine.ts b/src/engine/CaptureChoiceEngine.ts index 0f4f130..96d7ef0 100644 --- a/src/engine/CaptureChoiceEngine.ts +++ b/src/engine/CaptureChoiceEngine.ts @@ -40,41 +40,25 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine { } const filePath = await this.getFilePath(captureTo); - let content = await this.getCaptureContent(); - let file: TFile; + const content = await this.getCaptureContent(); + let getFileAndAddContentFn: (fileContent: string, content: string) => Promise<{file: TFile, content: string}>; + if (await this.fileExists(filePath)) { - file = await this.getFileByPath(filePath); - if (!file) return; - - const fileContent: string = await this.app.vault.read(file); - const newFileContent: string = await this.formatter.formatContentWithFile(content, this.choice, fileContent, file); - - await this.app.vault.modify(file, newFileContent); + getFileAndAddContentFn = this.onFileExists; } else if (this.choice?.createFileIfItDoesntExist?.enabled) { - let fileContent: string = ""; - - if (this.choice.createFileIfItDoesntExist.createWithTemplate) { - const singleTemplateEngine: SingleTemplateEngine = - new SingleTemplateEngine(this.app, this.plugin, this.choice.createFileIfItDoesntExist.template, this.choiceExecutor); - - fileContent = await singleTemplateEngine.run(); - } - - file = await this.createFileWithInput(filePath, fileContent); - await replaceTemplaterTemplatesInCreatedFile(this.app, file); - - const updatedFileContent: string = await this.app.vault.cachedRead(file); - const newFileContent: string = await this.formatter.formatContentWithFile(content, this.choice, updatedFileContent, file); - await this.app.vault.modify(file, newFileContent); - + getFileAndAddContentFn = this.onCreateFileIfItDoesntExist; } else { log.logWarning(`The file ${filePath} does not exist and "Create file if it doesn't exist" is disabled.`); return; } - if (this.choice.appendLink) - appendToCurrentLine(this.app.fileManager.generateMarkdownLink(file, ''), this.app); + const { file, content: newFileContent } = await getFileAndAddContentFn.bind(this)(filePath, content); + + if (this.choice.appendLink) { + const markdownLink = this.app.fileManager.generateMarkdownLink(file, ''); + appendToCurrentLine(markdownLink, this.app); + } if (this.choice?.openFile) { await openFile(this.app, file, { @@ -84,6 +68,8 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine { mode: this.choice.openFileInMode }); } + + await this.app.vault.modify(file, newFileContent); } catch (e) { log.logMessage(e); @@ -104,6 +90,49 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine { return content; } + private async onFileExists(filePath: string, content: string) + : Promise<{ file: TFile, content: string }> + { + const file: TFile = await this.getFileByPath(filePath); + if (!file) return; + + const fileContent: string = await this.app.vault.read(file); + const newFileContent: string = await this.formatter.formatContentWithFile(content, this.choice, fileContent, file); + + return {file, content: newFileContent}; + } + + + private async onCreateFileIfItDoesntExist(filePath: string, content: string) + : Promise<{ file: TFile, content: string }> + { + let fileContent: string = ""; + + if (this.choice.createFileIfItDoesntExist.createWithTemplate) { + const singleTemplateEngine: SingleTemplateEngine = new SingleTemplateEngine( + this.app, + this.plugin, + this.choice.createFileIfItDoesntExist.template, + this.choiceExecutor + ); + + fileContent = await singleTemplateEngine.run(); + } + + const file: TFile = await this.createFileWithInput(filePath, fileContent); + await replaceTemplaterTemplatesInCreatedFile(this.app, file); + + const updatedFileContent: string = await this.app.vault.cachedRead(file); + const newFileContent: string = await this.formatter.formatContentWithFile( + content, + this.choice, + updatedFileContent, + file + ); + + return {file, content: newFileContent}; + } + private async getFilePath(captureTo: string) { const formattedCaptureTo: string = await this.formatter.formatFileName(captureTo, this.choice.name); return this.formatFilePath("", formattedCaptureTo); diff --git a/src/formatters/captureChoiceFormatter.ts b/src/formatters/captureChoiceFormatter.ts index 59a55e7..a3ccbbb 100644 --- a/src/formatters/captureChoiceFormatter.ts +++ b/src/formatters/captureChoiceFormatter.ts @@ -133,7 +133,10 @@ export class CaptureChoiceFormatter extends CompleteFormatter { private insertTextAfterPositionInBody(text: string, body: string, pos: number): string { if (pos === -1) { - return `${text}\n${body}`; + // For the case that there is no frontmatter and we're adding to the top of the file. + // We already add a linebreak for the task in CaptureChoiceEngine.tsx in getCapturedContent. + const shouldAddLinebreak = !this.choice.task; + return `${text}${shouldAddLinebreak ? "\n" : ""}${body}`; } const splitContent = body.split("\n");