fix: Context detection in instant task conversion (Issue #320)

Fixed bug where @context syntax wasn't being detected during instant task
conversion. The NaturalLanguageParser was correctly parsing contexts, but
they were being lost during conversion to TasksPlugin format.

Changes:
- Added contexts field to TasksPluginParser.ParsedTaskData interface
- Updated InstantTaskConvertService to preserve contexts from NLP parsing
- Enhanced context processing logic to handle both parsed and default contexts
- Ensures contexts are properly extracted and stored in task metadata

Now "Convert task to TaskNote" properly detects @context syntax and populates
the contexts field in the created task note.

Fixes #320
This commit is contained in:
Callum Alpass 2025-07-29 07:14:02 +10:00
parent 65a3bb024c
commit 334ae6ac9e
2 changed files with 19 additions and 4 deletions

View file

@ -379,8 +379,9 @@ export class InstantTaskConvertService {
let timeEstimate: number | undefined;
let recurrence: import('../types').RecurrenceInfo | undefined;
// Extract parsed tags and projects
// Extract parsed tags, contexts, and projects
const parsedTags = parsedData.tags || [];
const parsedContexts = parsedData.contexts || [];
const parsedProjects = parsedData.projects || [];
if (this.plugin.settings.useDefaultsOnInstantConvert) {
@ -404,10 +405,17 @@ export class InstantTaskConvertService {
scheduledDate = calculateDefaultDate(defaults.defaultScheduledDate);
}
// Apply default contexts
if (defaults.defaultContexts) {
contextsArray = defaults.defaultContexts.split(',').map(s => s.trim()).filter(s => s);
// Apply contexts: start with parsed contexts, then add default contexts
contextsArray = [];
if (parsedContexts.length > 0) {
contextsArray.push(...parsedContexts);
}
if (defaults.defaultContexts) {
const defaultContextsArray = defaults.defaultContexts.split(',').map(s => s.trim()).filter(s => s);
contextsArray.push(...defaultContextsArray);
}
// Remove duplicates
contextsArray = [...new Set(contextsArray)];
// Apply tags: start with task tag, add parsed tags, then add default tags
tagsArray = [this.plugin.settings.taskTag];
@ -438,6 +446,11 @@ export class InstantTaskConvertService {
status = (parsedData.status ? this.sanitizeStatus(parsedData.status) : '') || 'none';
dueDate = parsedDueDate || undefined;
scheduledDate = parsedScheduledDate || undefined;
// Apply contexts: only use parsed contexts
contextsArray = [];
if (parsedContexts.length > 0) {
contextsArray.push(...parsedContexts);
}
// Apply tags: start with task tag, add parsed tags
tagsArray = [this.plugin.settings.taskTag];
if (parsedTags.length > 0) {
@ -746,6 +759,7 @@ export class InstantTaskConvertService {
recurrence: nlpResult.recurrence,
tags: nlpResult.tags && nlpResult.tags.length > 0 ? nlpResult.tags : undefined,
projects: nlpResult.projects && nlpResult.projects.length > 0 ? nlpResult.projects : undefined,
contexts: nlpResult.contexts && nlpResult.contexts.length > 0 ? nlpResult.contexts : undefined,
// TasksPlugin specific fields that NLP doesn't have
startDate: undefined,
createdDate: undefined,

View file

@ -17,6 +17,7 @@ export interface ParsedTaskData {
month_of_year?: number;
};
tags?: string[];
contexts?: string[];
projects?: string[];
isCompleted: boolean;
}