Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions exampleVault/Buttons/Button Example.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ actions:

```

```meta-bind-button
label: Daily Note (with date)
hidden: false
id: ""
style: primary
actions:
- type: templaterCreateNote
templateFile: "templates/templater/Templater Template.md"
folderPath: "Daily/{YYYY}/{MM}"
fileName: "{YYYY-MM-DD}"
openIfAlreadyExists: true

```

```meta-bind-button
label: Sleep
hidden: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
} from 'packages/core/src/config/ButtonConfig';
import { ButtonActionType } from 'packages/core/src/config/ButtonConfig';
import { AbstractButtonActionConfig } from 'packages/core/src/fields/button/AbstractButtonActionConfig';
import { ensureFileExtension, joinPath } from 'packages/core/src/utils/Utils';
import { ensureFileExtension, joinPath, processDateFormatPlaceholders } from 'packages/core/src/utils/Utils';

export class CreateNoteButtonActionConfig extends AbstractButtonActionConfig<CreateNoteButtonAction> {
constructor(mb: MetaBind) {
Expand All @@ -21,8 +21,11 @@ export class CreateNoteButtonActionConfig extends AbstractButtonActionConfig<Cre
_context: ButtonContext,
click: ButtonClickContext,
): Promise<void> {
if (action.openIfAlreadyExists) {
const filePath = ensureFileExtension(joinPath(action.folderPath ?? '', action.fileName), 'md');
const processedFileName = processDateFormatPlaceholders(action.fileName) ?? action.fileName;
Comment thread
hsayed21 marked this conversation as resolved.
Outdated
const processedFolderPath = processDateFormatPlaceholders(action.folderPath);

if (action.openIfAlreadyExists && action.fileName) {
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition checks action.fileName instead of processedFileName. If date placeholder processing results in an empty string or the original action.fileName is just a placeholder that becomes empty, the openIfAlreadyExists logic may not work as intended. Consider checking processedFileName instead, or ensure the processed value is truthy before attempting to open the existing file.

Copilot uses AI. Check for mistakes.
const filePath = ensureFileExtension(joinPath(processedFolderPath ?? '', processedFileName ?? ''), 'md');
// if the file already exists, open it in the same tab
if (await this.mb.file.exists(filePath)) {
await this.mb.file.open(filePath, '', false);
Expand All @@ -31,8 +34,8 @@ export class CreateNoteButtonActionConfig extends AbstractButtonActionConfig<Cre
}

await this.mb.file.create(
action.folderPath ?? '',
action.fileName,
processedFolderPath ?? '',
processedFileName ?? 'Untitled',
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If processedFileName is an empty string (which can happen if action.fileName is an empty string), the nullish coalescing operator won't trigger and an empty string will be passed to mb.file.create(). Consider using a falsy check instead: processedFileName || 'Untitled' to handle empty strings properly.

Copilot uses AI. Check for mistakes.
'md',
action.openNote ?? false,
click.openInNewTab(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
} from 'packages/core/src/config/ButtonConfig';
import { ButtonActionType } from 'packages/core/src/config/ButtonConfig';
import { AbstractButtonActionConfig } from 'packages/core/src/fields/button/AbstractButtonActionConfig';
import { ensureFileExtension, joinPath } from 'packages/core/src/utils/Utils';
import { ensureFileExtension, joinPath, processDateFormatPlaceholders } from 'packages/core/src/utils/Utils';

export class TemplaterCreateNoteButtonActionConfig extends AbstractButtonActionConfig<TemplaterCreateNoteButtonAction> {
constructor(mb: MetaBind) {
Expand All @@ -21,8 +21,11 @@ export class TemplaterCreateNoteButtonActionConfig extends AbstractButtonActionC
_context: ButtonContext,
click: ButtonClickContext,
): Promise<void> {
const processedFileName = processDateFormatPlaceholders(action.fileName);
const processedFolderPath = processDateFormatPlaceholders(action.folderPath);

if (action.openIfAlreadyExists && action.fileName) {
Copy link

Copilot AI Dec 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition checks action.fileName instead of processedFileName. If date placeholder processing results in an empty string or the original action.fileName is just a placeholder that becomes empty, the openIfAlreadyExists logic may not work as intended. Consider checking processedFileName instead, or ensure the processed value is truthy before attempting to open the existing file.

Copilot uses AI. Check for mistakes.
const filePath = ensureFileExtension(joinPath(action.folderPath ?? '', action.fileName), 'md');
const filePath = ensureFileExtension(joinPath(processedFolderPath ?? '', processedFileName ?? ''), 'md');
// if the file already exists, open it in the same tab
if (await this.mb.file.exists(filePath)) {
await this.mb.file.open(filePath, '', false);
Expand All @@ -32,8 +35,8 @@ export class TemplaterCreateNoteButtonActionConfig extends AbstractButtonActionC

await this.mb.internal.createNoteWithTemplater(
action.templateFile,
action.folderPath,
action.fileName,
processedFolderPath,
processedFileName,
action.openNote,
click.openInNewTab(),
);
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/utils/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import moment from 'moment';

/**
* Clamp
*
Expand Down Expand Up @@ -371,6 +373,22 @@ export function ensureFileExtension(filePath: string, extension: string): string
return filePath + extension;
}

/**
* Processes date format placeholders in a string.
* Replaces patterns like {YYYY-MM-DD} with formatted dates using moment.js.
*/
export function processDateFormatPlaceholders(value: string | undefined): string | undefined {
if (!value) {
return value;
}

const placeholderRegex = /\{([^}]+)\}/g;

return value.replace(placeholderRegex, (_match, format: string) => {
return moment().format(format);
Comment thread
hsayed21 marked this conversation as resolved.
Outdated
});
Comment thread
hsayed21 marked this conversation as resolved.
}

export function toArray<T>(value: T | T[] | undefined): T[] {
if (value === undefined) {
return [];
Expand Down