Space Script: Task attributes and priority emojis

Here’s a space script using the awesome new attributeExtractor function.

It is based on the example in Space Script :

  • Convert emojis like 📅 2024-01-01 into attributes like [deadline: 2024-01-01]
  • Convert different emojis into specific priority attributes like :fire: to priority: critical
  • Adds a default priority of medium to all tasks that don’t have an emoji

Feel free to change it as needed

function processEmojiAttributes(text, attributes) {
    // These can be customized if needed
    const taskEmojis = {
        "➕": "created",
        "📅": "deadline",
        "✅": "completed",
        "⌛": "scheduled"
    };

    // Strip the checkbox
    let taskName = text.replace(/\[[^\]]+\]\s*/, "").trim();

    // Loop through the symbols to extract their corresponding dates
    Object.entries(taskEmojis).forEach(([symbol, meaning]) => {
        const regex = new RegExp(`\\${symbol}\\s*(\\d{4}-\\d{2}-\\d{2})`);
        const match = regex.exec(text);
        if (match) {
            attributes[meaning] = match[1];
            taskName = taskName.replace(new RegExp(`\\${symbol}\\s*\\d{4}-\\d{2}-\\d{2}`), "").trim();
        }
    });

    attributes['name'] = taskName;
    return attributes;
}


function processEmojiPriorities(text, attributes) {
    // These can be changed as needed
    const priorityEmojis = {
        "❗": "critical",
        "🔥": "critical",
        "🔼": "high",
        "➖": "medium",
        "🔽": "low",
    };

    for (const [emoji, priority] of Object.entries(priorityEmojis)) {
        if (text.includes(emoji)) {
            attributes['priority'] = priority;
            break;
        }
    }

    // Default to medium if there's no match
    if (!attributes['priority']) {
      attributes['priority'] = "medium";
    }

    // Update name to remove these emojis too
    attributes['name'] = attributes['name'].replace(/(🔥|❗|🔼|➖|🔽)/g, "").trim();

    return attributes;
}

silverbullet.registerAttributeExtractor({tags: ["task"]}, (text) => {
    let attributes = {}
    // Convert task emojis to attributes
    attributes = processEmojiAttributes(text, attributes);
    
    // Convert emojis to priority values
    attributes = processEmojiPriorities(text, attributes);

    return attributes;
});
5 Likes

Thanks for this script, looks really nice.
I guess I will try to implement it in my workflow, seems super useful.

I need though to work now on the side effects, which is what all these emojis is doing to other templates when filtering/processing the task name.
Below you can see how the tasks are treated in the queries, some emojis and dates disappear, others not.

  • 1st query
```query
task where page = @page.name render [[Library/Personal/Query/Task]]
```

Where Library/Personal/Query/Task

* [{{#if done}}x{{else}} {{/if}}] [[{{page}}@{{pos}}]] {{replace(name, /#[^#\d\s\[\]]+\w+/, "")}} {{#if completed}}{{replace(completed, /(.+)/, "✅$1")}}{{/if}} {{#if deadline}}📅 {{deadline}}{{/if}}

As you can see, I am replacing Completed and Deadline. Either i need to do the same for each emoji, or find a generic regex query.
Or maybe not, need to think which dates are useful to show in the task or not

  • The last table is a template
```template
{{{task where page = @page.name select name, priority, created, scheduled, deadline, completed}}}
```

You can see that i don’t use any template or anything special but certain emojis and dates don’t appear. For example the priority emojis :arrow_up_small: and :arrow_down_small: .
This is an interesting case because I don’t know what could be interfering here.
Will need to debug a bit more

Playing a bit with the new fields :slight_smile:

Testing the following:

  • Add an emoji denoting:
    • :heavy_check_mark:: Task done
    • :green_circle:: Task not done with a due date further than 10 days from today
    • :yellow_circle:: Task not done with a due date in less than 4 days
    • :red_circle:: Task not done with a past due date
  • Adding an emoji denoting the priority
  • Adding dates back (still need to think how i want to represent this, or which dates)

Super “hacky” the template :sweat_smile: :

* [{{#if done}}x{{else}} {{/if}}] [[{{page}}@{{pos}}]]{{#if completed}} ✔️{{else}}{{#if deadline}}{{#if dateDiff(today(),deadline) < 0}} 🔴{{else}}{{#if dateDiff(today(),deadline) < 4}} 🟡{{else}} 🟢{{/if}}{{/if}}{{else}} {{/if}}{{/if}}{{#if priority = "critical"}} ❗{{/if}}{{#if priority = "high"}} 🔼{{/if}}{{#if priority = "medium"}} {{/if}}{{#if priority = "low"}} 🔽{{/if}} {{replace(name, /#[^#\d\s\[\]]+\w+/, "")}}{{#if created}}➕ {{created}}{{/if}} {{#if scheduled}}⌛ {{scheduled}}{{/if}} {{#if deadline}}📅 {{deadline}}{{/if}} {{#if completed}}{{replace(completed, /(.+)/, "✅$1")}}{{/if}}

2 Likes

Very cool, thanks! Looking forward to seeing what other changes you come up with. I really like the indicators for overdue/almost due.

Did you figure out the task name issue? I tried testing a few of the examples in your screenshot and they seem to work for me

I am trying to build a useful/productive TODO page for my work instance, but it is still Work in Progress:

I am still trying to come up with ways where i can see priorities like this, but also deadlines/due dates easily, without cluttering much the page with multiple tables

Not yet, I realized that:

  • A task with priority critical (so with :exclamation:in their task name) which is undone appears without the emoji in the name

  • Once that task is marked as done, the emoji appears back in the name.

So maybe your script is only addressing not-done tasks… or maybe something else in my instance… I haven’t have time to dig into the details so far (busy week with kids).

I did some cleaning with my scripts and some more cache flushing and playing a bit with online/sync mode and now it seems that emojis are not back in the same after completing the tasks

[UPDATE} No, they still seem to appear after marking them done. Needs more debugging

I will also point to this:

2 Likes