Skip to content

Commit

Permalink
When creating a new file with a template, and the file already exists…
Browse files Browse the repository at this point in the history
…, you will be asked what you want to do.
  • Loading branch information
chhoumann committed Jul 4, 2021
1 parent 808c092 commit cf70549
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 11 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Quickly add new pages or content to your vault.
You can also do a [manual installation](docs/ManualInstallation.md).

## What's new?
### 0.3.4
- When creating a new file with a template, and the file already exists, you will be asked what you want to do. You can append the template to the top, bottom, overwrite the file with the template, or do nothing.

### 0.3.3
- Fix 'undefined' error when cancelling template choices.
- Insert after in captures now allow format syntax for dynamic insertion.
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "quickadd",
"name": "QuickAdd",
"version": "0.3.3",
"version": "0.3.4",
"minAppVersion": "0.12.5",
"description": "Quickly add new pages or content to your vault.",
"author": "Christian B. B. Houmann",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quickadd",
"version": "0.3.3",
"version": "0.3.4",
"description": "Quickly add new pages or content to your vault.",
"main": "main.js",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import exp from "constants";

export const VALUE_SYNTAX: string = "{{VALUE}}";
export const DATE_SYNTAX = "{{DATE}}";
export const NAME_SYNTAX = "{{NAME}}";
Expand Down Expand Up @@ -48,3 +50,9 @@ export const TEMPLATE_SYNTAX_SUGGEST_REGEX: RegExp = new RegExp(/{{[T]?[E]?[M]?[
export const MACRO_SYNTAX_SUGGEST_REGEX: RegExp = new RegExp(/{{[M]?[A]?[C]?[R]?[O]?[:]?$|{{MACRO:[^\n\r}]*}}$/i);


// == File Exists (Template Choice) == //
export const fileExistsAppendToBottom: string = "Append to the bottom of the file";
export const fileExistsAppendToTop: string = "Append to the top of the file";
export const fileExistsOverwriteFile: string = "Overwrite the file";
export const fileExistsDoNothing: string = "Nothing";
export const fileExistsChoices: string[] = [fileExistsAppendToBottom, fileExistsAppendToTop, fileExistsOverwriteFile, fileExistsDoNothing];
49 changes: 43 additions & 6 deletions src/engine/TemplateChoiceEngine.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import type ITemplateChoice from "../types/choices/ITemplateChoice";
import type {App, TFile} from "obsidian";
import type {App} from "obsidian";
import {TFile} from "obsidian";
import {appendToCurrentLine, getAllFolders} from "../utility";
import {VALUE_SYNTAX} from "../constants";
import {
fileExistsAppendToBottom,
fileExistsAppendToTop,
fileExistsDoNothing,
fileExistsChoices,
fileExistsOverwriteFile,
VALUE_SYNTAX
} from "../constants";
import {log} from "../logger/logManager";
import type QuickAdd from "../main";
import {TemplateEngine} from "./TemplateEngine";
import type {IChoiceExecutor} from "../IChoiceExecutor";
import GenericSuggester from "../gui/GenericSuggester/genericSuggester";

export class TemplateChoiceEngine extends TemplateEngine {
public choice: ITemplateChoice;
Expand Down Expand Up @@ -39,10 +48,38 @@ export class TemplateChoiceEngine extends TemplateEngine {
if (this.choice.incrementFileName)
filePath = await this.incrementFileName(filePath);

const createdFile: TFile = await this.createFileWithTemplate(filePath, this.choice.templatePath);
if (!createdFile) {
log.logWarning(`Could not create file '${filePath}'.`);
return;
let createdFile: TFile;
if (await this.app.vault.adapter.exists(filePath)) {
const file = this.app.vault.getAbstractFileByPath(filePath);
if (!(file instanceof TFile && file.extension === 'md')) {
log.logError(`'${filePath}' already exists and is not a valid markdown file.`);
return;
}

await this.app.workspace.splitActiveLeaf('vertical').openFile(file);
const userChoice: string = await GenericSuggester.Suggest(this.app, fileExistsChoices, fileExistsChoices);

switch (userChoice) {
case fileExistsAppendToTop:
createdFile = await this.appendToFileWithTemplate(file, this.choice.templatePath, 'top');
break;
case fileExistsAppendToBottom:
createdFile = await this.appendToFileWithTemplate(file, this.choice.templatePath, 'bottom');
break;
case fileExistsOverwriteFile:
createdFile = await this.overwriteFileWithTemplate(file, this.choice.templatePath);
break;
case fileExistsDoNothing:
default:
log.logWarning("File not written to.");
return;
}
} else {
createdFile = await this.createFileWithTemplate(filePath, this.choice.templatePath);
if (!createdFile) {
log.logWarning(`Could not create file '${filePath}'.`);
return;
}
}

if (this.choice.appendLink) {
Expand Down
38 changes: 38 additions & 0 deletions src/engine/TemplateEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,44 @@ export abstract class TemplateEngine extends QuickAddEngine {
}
}

protected async overwriteFileWithTemplate(file: TFile, templatePath: string) {
try {
const templateContent: string = await this.getTemplateContent(templatePath);

const formattedTemplateContent: string = await this.formatter.formatFileContent(templateContent);
await this.app.vault.modify(file, formattedTemplateContent);

await replaceTemplaterTemplatesInCreatedFile(this.app, file, true);

return file;
}
catch (e) {
log.logError(e);
return null;
}
}

protected async appendToFileWithTemplate(file: TFile, templatePath: string, section: 'top' | 'bottom') {
try {
const templateContent: string = await this.getTemplateContent(templatePath);

const formattedTemplateContent: string = await this.formatter.formatFileContent(templateContent);
const fileContent: string = await this.app.vault.cachedRead(file);
const newFileContent: string = section === "top" ?
`${formattedTemplateContent}\n${fileContent}` :
`${fileContent}\n${formattedTemplateContent}`
await this.app.vault.modify(file, newFileContent);

await replaceTemplaterTemplatesInCreatedFile(this.app, file, true);

return file;
}
catch (e) {
log.logError(e);
return null;
}
}

protected async getTemplateContent(templatePath: string): Promise<string> {
const templateFile: TAbstractFile = this.app.vault.getAbstractFileByPath(templatePath);
if (!(templateFile instanceof TFile)) return;
Expand Down
4 changes: 2 additions & 2 deletions src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export function getTemplater(app: App) {
return app.plugins.plugins["templater-obsidian"]
}

export async function replaceTemplaterTemplatesInCreatedFile(app: App, file: TFile) {
export async function replaceTemplaterTemplatesInCreatedFile(app: App, file: TFile, force: boolean = false) {
const templater = getTemplater(app);

if (templater && !templater?.settings["trigger_on_file_creation"]) {
if (templater && (force || !templater?.settings["trigger_on_file_creation"])) {
await templater.templater.overwrite_file_templates(file);
}
}
Expand Down
2 changes: 1 addition & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"0.2.16": "0.12.4",
"0.3.3": "0.12.5"
"0.3.4": "0.12.5"
}

0 comments on commit cf70549

Please sign in to comment.