Skip to content

Commit

Permalink
Bug squashing
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Jun 12, 2021
1 parent eb6ca45 commit 627ad68
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
61 changes: 34 additions & 27 deletions src/engine/CaptureChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,41 +19,48 @@ export class CaptureChoiceEngine extends QuickAddChoiceEngine {
}

async run(): Promise<void> {
const captureTo = this.choice.captureTo;
if (!captureTo) {
log.logError(`Invalid capture to for ${this.choice.name}`);
return;
}
try {
const captureTo = this.choice.captureTo;
if (!captureTo) {
log.logError(`Invalid capture to for ${this.choice.name}`);
return;
}

const filePath = await this.getFilePath(captureTo);
let content: string;
const filePath = await this.getFilePath(captureTo);
let content: string;

if (!this.choice.format.enabled)
content = await GenericInputPrompt.Prompt(this.app, this.choice.name);
else
content = this.choice.format.format;
if (!this.choice.format.enabled)
content = await GenericInputPrompt.Prompt(this.app, this.choice.name);
else
content = this.choice.format.format;

if (this.choice.task)
content = `- [ ] ${content}`;
if (this.choice.task)
content = `- [ ] ${content}`;

if (await this.fileExists(filePath)) {
const file: TFile = await this.getFileByPath(filePath);
if (!file) return;
if (await this.fileExists(filePath)) {
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.formatContent(content, this.choice, fileContent, file);
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);
} else {
const createdFile = await this.createFileWithInput(filePath, content);
if (!createdFile) {
log.logError(`could not create '${filePath}.'`);
return;
await this.app.vault.modify(file, newFileContent);
} else {
const formattedContent = await this.formatter.formatContent(content, this.choice);
const createdFile = await this.createFileWithInput(filePath, formattedContent);

if (!createdFile) {
log.logError(`could not create '${filePath}.'`);
return;
}
}
}

if (this.choice.appendLink)
appendToCurrentLine(`[[${filePath.replace(MARKDOWN_FILE_EXTENSION_REGEX, '')}]]`, this.app);
if (this.choice.appendLink)
appendToCurrentLine(`[[${filePath.replace(MARKDOWN_FILE_EXTENSION_REGEX, '')}]]`, this.app);
}
catch (e) {
log.logError(e);
}
}

private async getFilePath(captureTo: string) {
Expand Down
15 changes: 11 additions & 4 deletions src/formatters/captureChoiceFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import type QuickAdd from "../main";

export class CaptureChoiceFormatter extends CompleteFormatter {
private choice: ICaptureChoice;
private file: TFile;
private fileContent: string;
private file: TFile = null;
private fileContent: string = "";

constructor(app: App, plugin: QuickAdd) {
super(app, plugin);
}

public async formatContent(input: string, choice: ICaptureChoice, fileContent: string, file: TFile): Promise<string> {
public async formatContentWithFile(input: string, choice: ICaptureChoice, fileContent: string, file: TFile): Promise<string> {
this.choice = choice;
this.file = file;
this.fileContent = fileContent;
Expand All @@ -22,6 +22,13 @@ export class CaptureChoiceFormatter extends CompleteFormatter {
return await this.formatFileContent(input);
}

public async formatContent(input: string, choice: ICaptureChoice): Promise<string> {
this.choice = choice;
if(!choice) return input;

return await this.formatFileContent(input);
}

async formatFileContent(input: string): Promise<string> {
const formatted = await super.formatFileContent(input);

Expand All @@ -39,7 +46,7 @@ export class CaptureChoiceFormatter extends CompleteFormatter {
return this.insertTextAfterPositionInBody(formatted, this.fileContent, targetPosition);
}

const frontmatterEndPosition = await this.getFrontmatterEndPosition(this.file);
const frontmatterEndPosition = this.file ? await this.getFrontmatterEndPosition(this.file) : null;
if (!frontmatterEndPosition)
return `${formatted}${this.fileContent}`;

Expand Down

0 comments on commit 627ad68

Please sign in to comment.