Skip to content

Commit

Permalink
Add {{TEMPLATE}} to format syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Jun 12, 2021
1 parent 07667a8 commit 1a7d152
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const FORMAT_SYNTAX: string[] = [
"{{DATE}}", "{{DATE:<DATEFORMAT>}}", "{{VDATE:<VARIABLE NAME>, <DATE FORMAT>}}",
"{{VALUE}}", "{{NAME}}", "{{VALUE:<VARIABLE NAME>}}", "{{LINKCURRENT}}", "{{MACRO:<MACRONAME>}}"
"{{VALUE}}", "{{NAME}}", "{{VALUE:<VARIABLE NAME>}}", "{{LINKCURRENT}}", "{{MACRO:<MACRONAME>}}",
"{{TEMPLATE:<TEMPLATEPATH>}}"
];

export const FILE_NAME_FORMAT_SYNTAX: string[] = [
Expand All @@ -18,4 +19,4 @@ 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}]*)}}/);
export const TEMPLATE_REGEX: RegExp = new RegExp(/{{MACRO:([^\n\r}]*.md$)}}/);
export const TEMPLATE_REGEX: RegExp = new RegExp(/{{TEMPLATE:([^\n\r}]*.md)}}/);
6 changes: 5 additions & 1 deletion src/engine/SingleTemplateEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ export class SingleTemplateEngine extends TemplateEngine {
super(app, plugin);
}
public async run(): Promise<string> {
const templateContent: string = await this.getTemplateContent(this.templatePath);
let templateContent: string = await this.getTemplateContent(this.templatePath);
if (!templateContent) {
throw new Error(`Template ${this.templatePath} not found.`);
}

if (this.templater) {
templateContent = this.templater.templater.parser.parseTemplates(templateContent);
}

return await this.formatter.formatFileContent(templateContent);
}
}
11 changes: 7 additions & 4 deletions src/formatters/formatDisplayFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type {App} from "obsidian";
import {getNaturalLanguageDates} from "../utility";
import type QuickAdd from "../main";
import {SingleTemplateEngine} from "../engine/SingleTemplateEngine";
import {template} from "@babel/core";

export class FormatDisplayFormatter extends Formatter {
constructor(private app: App, private plugin: QuickAdd) {
Expand All @@ -19,6 +18,7 @@ export class FormatDisplayFormatter extends Formatter {
output = await this.replaceVariableInString(output);
output = await this.replaceLinkToCurrentFileInString(output);
output = await this.replaceMacrosInString(output);
output = await this.replaceTemplateInString(output);

return output;
}
Expand Down Expand Up @@ -51,8 +51,11 @@ export class FormatDisplayFormatter extends Formatter {
}

protected async getTemplateContent(templatePath: string): Promise<string> {
const templateContent: string = await new SingleTemplateEngine(this.app, this.plugin, templatePath).run();
if (!templateContent) return `Template (not found): ${templatePath}`;
return templateContent;
try {
return await new SingleTemplateEngine(this.app, this.plugin, templatePath).run();
}
catch (e) {
return `Template (not found): ${templatePath}`;
}
}
}
17 changes: 8 additions & 9 deletions src/formatters/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
VARIABLE_REGEX
} from "../constants";
import {getDate} from "../utility";
import {template} from "@babel/core";

export abstract class Formatter {
protected value: string;
Expand Down Expand Up @@ -140,14 +139,6 @@ export abstract class Formatter {
return output;
}

protected abstract getNaturalLanguageDates();

protected abstract getMacroValue(macroName: string): Promise<string> | string;

protected abstract promptForVariable(variableName: string): Promise<string>;

protected abstract getTemplateContent(templatePath: string): Promise<string>;

protected async replaceTemplateInString(input: string): Promise<string> {
let output: string = input;

Expand All @@ -160,4 +151,12 @@ export abstract class Formatter {

return output;
}

protected abstract getNaturalLanguageDates();

protected abstract getMacroValue(macroName: string): Promise<string> | string;

protected abstract promptForVariable(variableName: string): Promise<string>;

protected abstract getTemplateContent(templatePath: string): Promise<string>;
}
2 changes: 0 additions & 2 deletions src/gui/GenericInputPrompt/GenericInputPromptContent.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<script lang="ts">
import type {App} from "obsidian";
export let app: App;
export let header: string = "";
export let placeholder: string = "";
export let value: string = "";
Expand Down
1 change: 0 additions & 1 deletion src/gui/GenericInputPrompt/genericInputPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export default class GenericInputPrompt extends Modal {
this.modalContent = new GenericInputPromptContent({
target: this.contentEl,
props: {
app,
header,
placeholder,
value,
Expand Down

0 comments on commit 1a7d152

Please sign in to comment.