Skip to content

Commit

Permalink
Merge pull request #226 from chhoumann/fix-bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann authored Apr 5, 2022
2 parents 427621e + b674783 commit fe81eaf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 28 deletions.
83 changes: 56 additions & 27 deletions src/engine/CaptureChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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);
Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/formatters/captureChoiceFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit fe81eaf

Please sign in to comment.