Skip to content

Commit

Permalink
Macro execution in formats
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Jun 12, 2021
1 parent 4a409bb commit 83c3a7a
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ main.js

# obsidian
data.json

**/*.map
src/**/*.js
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export const DATE_VARIABLE_REGEX: RegExp = new RegExp(/{{VDATE:([^\n\r},]*),\s*(
export const LINK_TO_CURRENT_FILE_REGEX: RegExp = new RegExp(/{{LINKCURRENT}}/);
export const MARKDOWN_FILE_EXTENSION_REGEX: RegExp = new RegExp(/\.md$/);
export const JAVASCRIPT_FILE_EXTENSION_REGEX: RegExp = new RegExp(/\.js$/);
export const MACRO_REGEX: RegExp = new RegExp(/{{MACRO:([^\n\r}]*)}}/);
5 changes: 3 additions & 2 deletions src/engine/CaptureChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import GenericInputPrompt from "../gui/GenericInputPrompt/genericInputPrompt";
import {CaptureChoiceFormatter} from "../formatters/captureChoiceFormatter";
import {appendToCurrentLine} from "../utility";
import {MARKDOWN_FILE_EXTENSION_REGEX} from "../constants";
import type QuickAdd from "../main";

export class CaptureChoiceEngine extends QuickAddEngine {
choice: ICaptureChoice;
private formatter: CaptureChoiceFormatter;

constructor(app: App, choice: ICaptureChoice) {
constructor(app: App, plugin: QuickAdd, choice: ICaptureChoice) {
super(app);
this.choice = choice;
this.formatter = new CaptureChoiceFormatter(app);
this.formatter = new CaptureChoiceFormatter(app, plugin);
}

async run(): Promise<void> {
Expand Down
6 changes: 4 additions & 2 deletions src/engine/MacroChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {ICommand} from "../types/macros/ICommand";

export class MacroChoiceEngine extends QuickAddEngine {
choice: IMacroChoice;
protected output: string;

constructor(app: App, choice: IMacroChoice) {
super(app);
Expand Down Expand Up @@ -55,12 +56,13 @@ export class MacroChoiceEngine extends QuickAddEngine {
return;
}

await userScript.default({app: this.app, quickAddApi: QuickAddApi.GetApi(this.app)});
this.output = await userScript.default({app: this.app, quickAddApi: QuickAddApi.GetApi(this.app)});
}
}

protected executeObsidianCommand(command: IObsidianCommand) {
// @ts-ignore
this.app.commands.executeCommandById(command.id);
}
}
}

17 changes: 17 additions & 0 deletions src/engine/SingleMacroEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type {App} from "obsidian";
import type {IMacro} from "../types/macros/IMacro";
import {MacroChoiceEngine} from "./MacroChoiceEngine";

export class SingleMacroEngine extends MacroChoiceEngine {
constructor(app: App, private macros: IMacro[]) {
super(app, null);
}

public async runAndGetOutput(macroName: string): Promise<string> {
const macro = this.macros.find(macro => macro.name === macroName);
if (!macro) return;

await this.executeCommands(macro.commands)
return this.output;
}
}
5 changes: 3 additions & 2 deletions src/engine/TemplateChoiceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import GenericInputPrompt from "../gui/GenericInputPrompt/genericInputPrompt";
import GenericSuggester from "../gui/GenericSuggester/genericSuggester";
import {QuickAddEngine} from "./QuickAddEngine";
import {log} from "../logger/logManager";
import type QuickAdd from "../main";

export class TemplateChoiceEngine extends QuickAddEngine {
public choice: ITemplateChoice;
private formatter: CompleteFormatter;
private readonly templater;

constructor(app: App, choice: ITemplateChoice) {
constructor(app: App, private plugin: QuickAdd, choice: ITemplateChoice) {
super(app);
this.choice = choice;
this.templater = getTemplater(app);
this.formatter = new CompleteFormatter(app);
this.formatter = new CompleteFormatter(app, plugin);
}

public async run(): Promise<void> {
Expand Down
5 changes: 3 additions & 2 deletions src/formatters/captureChoiceFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import {CompleteFormatter} from "./completeFormatter";
import type ICaptureChoice from "../types/choices/ICaptureChoice";
import type {App, TFile} from "obsidian";
import {log} from "../logger/logManager";
import type QuickAdd from "../main";

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

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

public async formatContent(input: string, choice: ICaptureChoice, fileContent: string, file: TFile): Promise<string> {
Expand Down
22 changes: 19 additions & 3 deletions src/formatters/completeFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import {Formatter} from "./formatter";
import type {App, TFile} from "obsidian";
import {MARKDOWN_FILE_EXTENSION_REGEX} from "../constants";
import {MACRO_REGEX, MARKDOWN_FILE_EXTENSION_REGEX} from "../constants";
import {getNaturalLanguageDates} from "../utility";
import GenericInputPrompt from "../gui/GenericInputPrompt/genericInputPrompt";
import GenericSuggester from "../gui/GenericSuggester/genericSuggester";
import {log} from "../logger/logManager";
import type QuickAdd from "../main";
import {SingleMacroEngine} from "../engine/SingleMacroEngine";

export class CompleteFormatter extends Formatter {
private valueHeader: string;

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

Expand All @@ -21,6 +23,7 @@ export class CompleteFormatter extends Formatter {
output = await this.replaceValueInString(output);
output = await this.replaceDateVariableInString(output);
output = await this.replaceVariableInString(output);
output = await this.replaceMacrosInString(output);

return output;
}
Expand All @@ -29,6 +32,20 @@ export class CompleteFormatter extends Formatter {
}
}

private async replaceMacrosInString(input: string): Promise<string> {
const macroEngine: SingleMacroEngine = new SingleMacroEngine(this.app, this.plugin.settings.macros);
let output: string = input;

while(MACRO_REGEX.test(output)) {
const macroName = MACRO_REGEX.exec(output)[1];
const macroOutput = await macroEngine.runAndGetOutput(macroName);

output.replace(MACRO_REGEX, macroOutput.toString());
}

return output;
}

async formatFileName(input: string, valueHeader: string): Promise<string> {
this.valueHeader = valueHeader;
return await this.format(input);
Expand Down Expand Up @@ -68,5 +85,4 @@ export class CompleteFormatter extends Formatter {
protected async suggestForValue(suggestedValues: string[]) {
return await GenericSuggester.Suggest(this.app, suggestedValues, suggestedValues);
}

}
4 changes: 2 additions & 2 deletions src/gui/choiceSuggester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class ChoiceSuggester extends FuzzySuggestModal<IChoice> {
return;
}

await new TemplateChoiceEngine(this.app, templateChoice).run();
await new TemplateChoiceEngine(this.app, this.plugin, templateChoice).run();
}

private async onChooseCaptureType(captureChoice: ICaptureChoice) {
Expand All @@ -76,7 +76,7 @@ export default class ChoiceSuggester extends FuzzySuggestModal<IChoice> {
return;
}

await new CaptureChoiceEngine(this.app, captureChoice).run();
await new CaptureChoiceEngine(this.app, this.plugin, captureChoice).run();
}

private async onChooseMacroType(macroChoice: IMacroChoice) {
Expand Down

0 comments on commit 83c3a7a

Please sign in to comment.