Generate Tasks in Daily Note Template according to contents of hashtag

First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.

What I’m trying to do

So I’m trying to set up a code in my daily note template that will automatically generate a list of tasks based on any instances of a certain hashtag in my vault, along with the line of text that accompanies said hashtag. So ideally I’d get:

- [ ] #Tag lorem ipsum blah blah blah
- [ ] #Tag More lorem ipsum

I don’t want it in a Dataview/Query style list, unless the tasks can be added to my “Tasks Completed Today” list at the end automatically, I use a very simple code there like so:

done on 2023-07-12

Things I have tried

Got some help on the Discord, I’ve tried reading about Templater and Dataview, and the code I have so far should be able to find instances of my hashtag, #Next, throughout my vault:

<%* app.vault.getMarkdownFiles().forEach(file => {
if (tp.file.include("#Next")) {

}) %>

… But I cannot figure out how on Earth to retrieve the line of text AFTER these tags, and then to add that to the document in the format of a task. Extremely frustrated after trying to figure this out a couple days.

Thanks in advance, all help appreciated.

The normal way to do this is to simply tag the task directly with #next. This way the task is simple to fetch, and you don’t need to mess around with reading the next line in a file and so on.

Hey holroy, thanks for taking the time to reply,
That wasn’t exactly what I was looking for-- I tried using recurring tasks but that was cluttering up the files that contained them with repeats for every day-- hence what I wanted was something that could grab normal lines of text with the hashtag and create tasks from them.

Someone in the Discord was very kind and helped me figure it out, I’ll post the sum of the code they put together in case it helps someone else:

const tag = "#<Your hashtag here>"
const excludeFolders = ["<Any folders you want to exclude>"]
const taggedFiles = app.vault.getMarkdownFiles()
	.filter(file => !excludeFolders.some(folder => (file.parent?.path ?? "/").startsWith(folder)))
	.map(file => [file, app.metadataCache.getFileCache(file)])
	.map(([file, cache,]) => [file, cache ? (tp.obsidian.getAllTags(cache) ?? []) : []])
	.filter(([, tags,]) => tags.includes(tag))
	.map(([file,]) => file)
const tasks = (await Promise.all(taggedFiles
	.map(file => app.vault.cachedRead(file))))
	.flatMap(content => content.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/u))
	.filter(line => line.includes(tag))
	.map(line => `- [ ] ${line}`)
for (item in tasks) {
	item = "- [ ]" + item
tR += tasks.join("\n")

All credits to “quote” from the Discord for that :slight_smile:

removed - I misunderstood the request.

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