diff --git a/PR.md b/PR.md new file mode 100644 index 00000000..268e1c50 --- /dev/null +++ b/PR.md @@ -0,0 +1,19 @@ +# fix-task-prop-based-ident + +## Property-based task identification preserves tags + +This change prevents property-based identification from mutating tags automatically. Tags are only written when the user explicitly sets them (including default tags), while the identifying property is still applied. + +Examples (illustrative): + +- Creating a task with property identification and tags like `client, urgent` keeps those tags; no extra task tag is injected. +- Updating priority/status without touching tags leaves existing tags unchanged. + +## Changelog + +### Task Identification + +- Only add the identifying task tag to frontmatter when tag-based identification is enabled. +- In property-based mode, preserve user/default tags without filtering them out. +- Only write `frontmatter.tags` during updates when tags are explicitly provided; otherwise leave tags untouched. +- Fixes callumalpass/tasknotes#1391. diff --git a/src/services/TaskService.ts b/src/services/TaskService.ts index 80b6a347..0560df66 100644 --- a/src/services/TaskService.ts +++ b/src/services/TaskService.ts @@ -343,10 +343,15 @@ export class TaskService { icsEventId: taskData.icsEventId || undefined, }; + const shouldAddTaskTag = this.plugin.settings.taskIdentificationMethod === "tag"; + const taskTagForFrontmatter = shouldAddTaskTag + ? this.plugin.settings.taskTag + : undefined; + // Use field mapper to convert to frontmatter with proper field mapping const frontmatter = this.plugin.fieldMapper.mapToFrontmatter( completeTaskData, - this.plugin.settings.taskTag, + taskTagForFrontmatter, this.plugin.settings.storeTitleInFilename ); @@ -361,12 +366,8 @@ export class TaskService { lower === "true" || lower === "false" ? lower === "true" : propValue; frontmatter[propName] = coercedValue as any; } - // Remove task tag from tags array if using property identification - const filteredTags = tagsArray.filter( - (tag: string) => tag !== this.plugin.settings.taskTag - ); - if (filteredTags.length > 0) { - frontmatter.tags = filteredTags; + if (tagsArray.length > 0) { + frontmatter.tags = tagsArray; } } else { // Tags are handled separately (not via field mapper) @@ -1374,23 +1375,12 @@ export class TaskService { } if (updates.hasOwnProperty("tags")) { - let tagsToSet = updates.tags; - // Remove task tag if using property identification - if (this.plugin.settings.taskIdentificationMethod === "property" && tagsToSet) { - tagsToSet = tagsToSet.filter( - (tag: string) => tag !== this.plugin.settings.taskTag - ); - } - frontmatter.tags = tagsToSet; - } else if (originalTask.tags) { - let tagsToSet = originalTask.tags; - // Remove task tag if using property identification - if (this.plugin.settings.taskIdentificationMethod === "property") { - tagsToSet = tagsToSet.filter( - (tag: string) => tag !== this.plugin.settings.taskTag - ); + const tagsToSet = Array.isArray(updates.tags) ? [...updates.tags] : []; + if (tagsToSet.length > 0) { + frontmatter.tags = tagsToSet; + } else { + delete frontmatter.tags; } - frontmatter.tags = tagsToSet; } });