Dataview query to extract a list item and all its sublist items (Updated)

I came across this thread, which happened to be a problem I too was experiencing. Here’s the thread on which I’m expanding: Dataview query to extract a list item and all its sublist items

To get right to the point, first, thank you @holroy for the base code. Second, here’s a safe dataviewjs solution that, in my case, just filters by tag and only checks the current file path:

// Limits to 1.) current file, and 2.) only lists within it
let page = dv.current().file.path
let lists = dv.page(page).file.lists

// Filters list items by those that have "#key-idea"
let keyIdeas = lists.filter(l => l.tags && l.tags.includes("#key-idea"))

function listChildren(children, offset = "   ") {
	let result = "\n"
	
	// Checks if there are any children
	for (const child of children) {
		// Appends text to "return value"
		result += offset + "- " +
	        (child.task ? `[${ child.status ?? " "}] ` : "" ) +
	        child.text + "\n"
	    
	    // If child has another child, then nested-call listChildren()
	    if (child.children.length) {
		    result += listChildren(child.children, offset + "  ") 
	    }
	}
	return result
}

// Displays a MARKDOWN LIST
dv.paragraph(dv.markdownList(keyIdeas.map(l => l.text + listChildren(l.children))))
1 Like