tasks by page

Hi - I am wonder if its possible to have a query of all tasks and render with a template that groups them by page. Something like:

Example query:

${template.each(
query[[
  from index.tag 'task' 
  where not done 
  and dueDate != nil
  order by dueDate
]],
templates.tasksByPage
)}

Expected results:

[[Example page 1]]
- [ ] Some task on example page 1
- [ ] Some other task on example page 1  

[[Example page 2]]
- [ ] Some task on example page 2
- [x] Some other task on example page 2
- [ ] Some other task on example page 2

[[Example page 3]]
- [ ] Some task on example page 3
- [x] Some other task on example page 3
- [x] Some other task on example page 3

The query language doesn’t have any aggregation or group by functionality at the moment. This seems to work, though, but is a bit more manual:


```space-lua
local pageTasksTemplate = template.new[==[
# [[${pageName}]]
${template.each(tasks, templates.taskItem)}
]==]

function tasksByPage()
  -- Aggregate tasks per page
  local pageTasks = {}
  for task in query[[
      from index.tag 'task' 
      where not done 
      and dueDate != nil
      order by dueDate
    ]] do
    if not pageTasks[task.page] then
      pageTasks[task.page] = {}
    end
    table.insert(pageTasks[task.page], task)
  end

  -- Build up the markdown
  local md = ""
  for pageName, tasks in pairs(pageTasks) do
    md = md .. pageTasksTemplate({
      pageName = pageName,
      tasks = tasks
    })
  end
  return md
end
```

${tasksByPage()}

1 Like