Skip to content

Commit

Permalink
Capture: Add option to create 'Insert After' line if it isn't found
Browse files Browse the repository at this point in the history
  • Loading branch information
chhoumann committed Jul 15, 2021
1 parent 267be76 commit 3e71023
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 7 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.6
- Added setting to create the 'Insert After' line if it isn't found.

### 0.3.5
- You can now execute [inline JavaScript](docs/InlineScripts.md) in templates or captures.

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.5",
"version": "0.3.6",
"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.5",
"version": "0.3.6",
"description": "Quickly add new pages or content to your vault.",
"main": "main.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export const FILE_NAME_FORMAT_SYNTAX: string[] = [
export const FILE_NUMBER_REGEX: RegExp = new RegExp(/([0-9]*)\.md$/);
export const NUMBER_REGEX: RegExp = new RegExp(/^-?[0-9]*$/);

export const CREATE_IF_NOT_FOUND_TOP: string = "top";
export const CREATE_IF_NOT_FOUND_BOTTOM: string = "bottom";

// == Format Syntax == //
export const DATE_REGEX: RegExp = new RegExp(/{{DATE(\+-?[0-9]+)?}}/);
export const DATE_REGEX_FORMATTED: RegExp = new RegExp(/{{DATE:([^}\n\r+]*)(\+-?[0-9]+)?}}/);
Expand Down
17 changes: 17 additions & 0 deletions src/formatters/captureChoiceFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {log} from "../logger/logManager";
import type QuickAdd from "../main";
import type {IChoiceExecutor} from "../IChoiceExecutor";
import {getLinesInString, templaterParseTemplate} from "../utility";
import {CREATE_IF_NOT_FOUND_TOP} from "../constants";

export class CaptureChoiceFormatter extends CompleteFormatter {
private choice: ICaptureChoice;
Expand Down Expand Up @@ -52,6 +53,18 @@ export class CaptureChoiceFormatter extends CompleteFormatter {

const targetPosition = fileContentLines.findIndex(line => targetRegex.test(line));
if (targetPosition === -1) {
if (this.choice.insertAfter?.createIfNotFound) {
const insertAfterLineAndFormatted: string = `${await this.format(this.choice.insertAfter.after)}\n${formatted}`;

if (this.choice.insertAfter?.createIfNotFoundLocation === CREATE_IF_NOT_FOUND_TOP) {
const frontmatterEndPosition = this.file ? await this.getFrontmatterEndPosition(this.file) : 0;
return this.insertTextAfterPositionInBody(insertAfterLineAndFormatted, this.fileContent, frontmatterEndPosition - 1);
}
else {
return this.insertTextAfterPositionInBody(insertAfterLineAndFormatted, this.fileContent, fileContentLines.length - 1);
}
}

log.logError("unable to find insert after line in file.")
}

Expand Down Expand Up @@ -104,6 +117,10 @@ export class CaptureChoiceFormatter extends CompleteFormatter {
}

private insertTextAfterPositionInBody(text: string, body: string, pos: number): string {
if (pos === -1) {
return `${text}\n${body}`;
}

const splitContent = body.split("\n");
const pre = splitContent.slice(0, pos + 1).join("\n");
const post = splitContent.slice(pos + 1).join("\n");
Expand Down
32 changes: 31 additions & 1 deletion src/gui/ChoiceBuilder/captureChoiceBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import type ICaptureChoice from "../../types/choices/ICaptureChoice";
import type {App} from "obsidian";
import {Setting, TextAreaComponent, TextComponent, ToggleComponent} from "obsidian";
import {FormatSyntaxSuggester} from "../formatSyntaxSuggester";
import {FILE_NAME_FORMAT_SYNTAX, FORMAT_SYNTAX} from "../../constants";
import {
CREATE_IF_NOT_FOUND_BOTTOM,
CREATE_IF_NOT_FOUND_TOP,
FILE_NAME_FORMAT_SYNTAX,
FORMAT_SYNTAX
} from "../../constants";
import {FormatDisplayFormatter} from "../../formatters/formatDisplayFormatter";
import type QuickAdd from "../../main";
import {FileNameDisplayFormatter} from "../../formatters/fileNameDisplayFormatter";
Expand Down Expand Up @@ -155,6 +160,31 @@ export class CaptureChoiceBuilder extends ChoiceBuilder {
.setValue(this.choice.insertAfter?.insertAtEnd)
.onChange(value => this.choice.insertAfter.insertAtEnd = value)
);

const createLineIfNotFound: Setting = new Setting(this.contentEl);
createLineIfNotFound.setName("Create line if not found")
.setDesc("Creates the 'insert after' line if it is not found.")
.addToggle(toggle => {
if (!this.choice.insertAfter?.createIfNotFound)
this.choice.insertAfter.createIfNotFound = false; // Set to default

toggle
.setValue(this.choice.insertAfter?.createIfNotFound)
.onChange(value => this.choice.insertAfter.createIfNotFound = value)
.toggleEl.style.marginRight = "1em"
}
)
.addDropdown(dropdown => {
if (!this.choice.insertAfter?.createIfNotFoundLocation)
this.choice.insertAfter.createIfNotFoundLocation = CREATE_IF_NOT_FOUND_TOP; // Set to default

dropdown
.setValue(this.choice.insertAfter?.createIfNotFoundLocation)
.onChange(value => this.choice.insertAfter.createIfNotFoundLocation = value)
.addOption(CREATE_IF_NOT_FOUND_TOP, "Top")
.addOption(CREATE_IF_NOT_FOUND_BOTTOM, "Bottom")
}
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/types/choices/CaptureChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class CaptureChoice extends Choice implements ICaptureChoice {
captureToActiveFile: boolean;
createFileIfItDoesntExist: { enabled: boolean, createWithTemplate: boolean, template: string };
format: { enabled: boolean; format: string };
insertAfter: { enabled: boolean; after: string, insertAtEnd: boolean };
insertAfter: { enabled: boolean; after: string, insertAtEnd: boolean, createIfNotFound: boolean, createIfNotFoundLocation: string };
prepend: boolean;
task: boolean;

Expand All @@ -20,7 +20,7 @@ export class CaptureChoice extends Choice implements ICaptureChoice {
this.captureToActiveFile = false;
this.createFileIfItDoesntExist = {enabled: false, createWithTemplate: false, template: ""};
this.format = {enabled: false, format: ""};
this.insertAfter = {enabled: false, after: "", insertAtEnd: false};
this.insertAfter = {enabled: false, after: "", insertAtEnd: false, createIfNotFound: false, createIfNotFoundLocation: "top"};
this.prepend = false;
this.task = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/choices/ICaptureChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export default interface ICaptureChoice extends IChoice {
prepend: boolean;
appendLink: boolean;
task: boolean;
insertAfter: { enabled: boolean, after: string, insertAtEnd: boolean };
insertAfter: { enabled: boolean; after: string, insertAtEnd: boolean, createIfNotFound: boolean, createIfNotFoundLocation: string };
}

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.5": "0.12.5"
"0.3.6": "0.12.5"
}

0 comments on commit 3e71023

Please sign in to comment.