diff --git a/.gitignore b/.gitignore index 2fd7c5e..42e3ef8 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,6 @@ main.js # obsidian data.json + +**/*.map +src/**/*.js diff --git a/src/constants.ts b/src/constants.ts index 2861fae..39252a9 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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}]*)}}/); diff --git a/src/engine/CaptureChoiceEngine.ts b/src/engine/CaptureChoiceEngine.ts index b993768..49ea884 100644 --- a/src/engine/CaptureChoiceEngine.ts +++ b/src/engine/CaptureChoiceEngine.ts @@ -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 { diff --git a/src/engine/MacroChoiceEngine.ts b/src/engine/MacroChoiceEngine.ts index c6de9df..86412fb 100644 --- a/src/engine/MacroChoiceEngine.ts +++ b/src/engine/MacroChoiceEngine.ts @@ -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); @@ -55,7 +56,7 @@ 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)}); } } @@ -63,4 +64,5 @@ export class MacroChoiceEngine extends QuickAddEngine { // @ts-ignore this.app.commands.executeCommandById(command.id); } -} \ No newline at end of file +} + diff --git a/src/engine/SingleMacroEngine.ts b/src/engine/SingleMacroEngine.ts new file mode 100644 index 0000000..67d3ee0 --- /dev/null +++ b/src/engine/SingleMacroEngine.ts @@ -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 { + const macro = this.macros.find(macro => macro.name === macroName); + if (!macro) return; + + await this.executeCommands(macro.commands) + return this.output; + } +} \ No newline at end of file diff --git a/src/engine/TemplateChoiceEngine.ts b/src/engine/TemplateChoiceEngine.ts index ddf9484..fe6a65c 100644 --- a/src/engine/TemplateChoiceEngine.ts +++ b/src/engine/TemplateChoiceEngine.ts @@ -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 { diff --git a/src/formatters/captureChoiceFormatter.ts b/src/formatters/captureChoiceFormatter.ts index a9c85ad..2ac249b 100644 --- a/src/formatters/captureChoiceFormatter.ts +++ b/src/formatters/captureChoiceFormatter.ts @@ -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 { diff --git a/src/formatters/completeFormatter.ts b/src/formatters/completeFormatter.ts index ec54839..d69eb09 100644 --- a/src/formatters/completeFormatter.ts +++ b/src/formatters/completeFormatter.ts @@ -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(); } @@ -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; } @@ -29,6 +32,20 @@ export class CompleteFormatter extends Formatter { } } + private async replaceMacrosInString(input: string): Promise { + 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 { this.valueHeader = valueHeader; return await this.format(input); @@ -68,5 +85,4 @@ export class CompleteFormatter extends Formatter { protected async suggestForValue(suggestedValues: string[]) { return await GenericSuggester.Suggest(this.app, suggestedValues, suggestedValues); } - } \ No newline at end of file diff --git a/src/gui/choiceSuggester.ts b/src/gui/choiceSuggester.ts index 988adf4..6636e09 100644 --- a/src/gui/choiceSuggester.ts +++ b/src/gui/choiceSuggester.ts @@ -67,7 +67,7 @@ export default class ChoiceSuggester extends FuzzySuggestModal { return; } - await new TemplateChoiceEngine(this.app, templateChoice).run(); + await new TemplateChoiceEngine(this.app, this.plugin, templateChoice).run(); } private async onChooseCaptureType(captureChoice: ICaptureChoice) { @@ -76,7 +76,7 @@ export default class ChoiceSuggester extends FuzzySuggestModal { return; } - await new CaptureChoiceEngine(this.app, captureChoice).run(); + await new CaptureChoiceEngine(this.app, this.plugin, captureChoice).run(); } private async onChooseMacroType(macroChoice: IMacroChoice) {