Dynamically adding priority tag to tasks for visual information based on due date

What I’m trying to do

I’m trying to write a dataviewjs query that uses due date metadata in tasks to show visual information based on a priority level. I opted for the Colored Tags Wrangler plugin to give specific colors to the priority tags in my vault. I wanted to add dynamic tags in this format:

  • Due this week: #P1-critical
  • Due next week: #P2-high
  • Due in three weeks: #P3-medium

Things I have tried

The issue that I’m facing is that as soon as text of the task is changed i.e. adding the priority tags, the reference to the task is lost and it cannot be completed anymore. The following dataviewjs code dynamically adds the tags based on due date, but cannot check the original tasks from the query. As soon as if (priorityTag) is commented out, the dynamically added tag is removed and the original task can be checked again.

Tasks

  • Test1 [due:: 29-05-2024]
  • Test2 [due:: 05-06-2024]
  • Test3 [due:: 12-06-2024]

Code

// Helper function to parse date in DD-MM-YYYY format
function parseDate(dateString) {
    let [day, month, year] = dateString.split('-').map(Number);
    return new Date(year, month - 1, day);
}

// Get the current date and calculate the start of this week, next week, and three weeks from now
let now = new Date();
let dayOfWeek = now.getDay();
let startOfThisWeek = new Date(now);
startOfThisWeek.setDate(now.getDate() - dayOfWeek);
let endOfThisWeek = new Date(startOfThisWeek);
endOfThisWeek.setDate(startOfThisWeek.getDate() + 6);
let startOfNextWeek = new Date(endOfThisWeek);
startOfNextWeek.setDate(endOfThisWeek.getDate() + 1);
let endOfNextWeek = new Date(startOfNextWeek);
endOfNextWeek.setDate(startOfNextWeek.getDate() + 6);
let startOfThreeWeeks = new Date(endOfNextWeek);
startOfThreeWeeks.setDate(endOfNextWeek.getDate() + 1);
let endOfThreeWeeks = new Date(startOfThreeWeeks);
endOfThreeWeeks.setDate(startOfThreeWeeks.getDate() + 13);

// Function to determine the appropriate priority tag based on the due date
function getPriorityTag(dueDate) {
    if (dueDate >= startOfThisWeek && dueDate <= endOfThisWeek) {
        return "#P1-critical";
    } else if (dueDate >= startOfNextWeek && dueDate <= endOfNextWeek) {
        return "#P2-high";
    } else if (dueDate >= startOfThreeWeeks && dueDate <= endOfThreeWeeks) {
        return "#P3-medium";
    } else {
        return "";
    }
}

// Process the tasks to add display tags without affecting the original tasks
let tasks = dv.current().file.tasks.map(task => {
    // Extract the due date from the text but do not remove it
    let dateMatch = task.text.match(/\[due:: (\d{2}-\d{2}-\d{4})\]/);
    let displayText = task.text;
    if (dateMatch) {
        let dueDate = parseDate(dateMatch[1]);

        // Add the appropriate priority tag based on the due date
        let priorityTag = getPriorityTag(dueDate);
        
        
        //Commenting out this enables checking the original tasks
        if (priorityTag) { displayText = `${displayText} ${priorityTag}`;}
    }

    // Return a new object that combines the original task with the modified display text
    return {
        ...task,
        text: displayText,
        checked: task.completed
    };
});

console.log(tasks);

// Display the tasks with the modified text for display purposes
dv.taskList(tasks, false);

Result

Checking the original task does check the query, but checking the query doesn’t result in original task being checked.

Perhaps my approach of using tags isn’t the most practical. If anyone has suggestion in order to show visual information based on task due dates, please let me know.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.