-
-
Notifications
You must be signed in to change notification settings - Fork 73
feat(buttons): Add date format placeholders for templaterCreateNote and createNote actions #643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
|
|
@@ -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); | ||
| const processedFolderPath = processDateFormatPlaceholders(action.folderPath); | ||
|
|
||
| if (action.openIfAlreadyExists && action.fileName) { | ||
| 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); | ||
|
|
@@ -31,8 +34,8 @@ export class CreateNoteButtonActionConfig extends AbstractButtonActionConfig<Cre | |
| } | ||
|
|
||
| await this.mb.file.create( | ||
| action.folderPath ?? '', | ||
| action.fileName, | ||
| processedFolderPath ?? '', | ||
| processedFileName ?? 'Untitled', | ||
|
||
| 'md', | ||
| action.openNote ?? false, | ||
| click.openInNewTab(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
|
|
@@ -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) { | ||
|
||
| 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); | ||
|
|
@@ -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(), | ||
| ); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import Moment from 'moment/moment'; | ||
|
|
||
| /** | ||
| * Clamp | ||
| * | ||
|
|
@@ -371,6 +373,32 @@ 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 === undefined || value === '') { | ||
| return value; | ||
| } | ||
|
|
||
| const placeholderRegex = /\{([^}]+)\}/g; | ||
|
|
||
| return value.replace(placeholderRegex, (match, format: string) => { | ||
| // Validate that the format string only contains valid moment.js tokens and delimiters | ||
| // Moment.js tokens: Y M D d H h m s S a A Q W w X x Z z G g E e o k l | ||
| // Common delimiters: : / - space . , [ ] | ||
| const validMomentFormat = /^[YMDdHhmsaAQWwXxZzGgEeSsokl:/\-\s.,[\]]+$/.test(format); | ||
|
||
|
|
||
| if (!validMomentFormat) { | ||
| // Leave unknown/invalid formats unchanged | ||
| return match; | ||
| } | ||
|
|
||
| return Moment().format(format); | ||
| }); | ||
| } | ||
|
|
||
| export function toArray<T>(value: T | T[] | undefined): T[] { | ||
| if (value === undefined) { | ||
| return []; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition checks
action.fileNameinstead ofprocessedFileName. If date placeholder processing results in an empty string or the originalaction.fileNameis just a placeholder that becomes empty, theopenIfAlreadyExistslogic may not work as intended. Consider checkingprocessedFileNameinstead, or ensure the processed value is truthy before attempting to open the existing file.