Creating hierarchical outline using Dataviewjs

I’m trying to create a hierarchical outline of my notes using Dataviewjs (essentially, I want a representation of a folgezettel sequence using a file property instead of embedding the folgezettel label in the filename). A similar question went unanswered before.

The main problem I’m running into is that I don’t know how to indent a bullet point. Here’s a first attempt:

const pages = dv.pages('"notes"').sort(b=>b.sequence)
for (let page of pages){
	let seq = page.sequence;
	const regex = /[\d]+|[a-z]+/g;
	let level = seq.match(regex)
	let indent = level.length
	let pre = ""
	if (indent == 1){
		pre="-"
	}
	else{
		for (var i = 0; i < indent; i++){
			pre+="\t"
		}
		pre = pre + "-"
	}
	dv.el("p",pre+" "+seq + " " + dv.fileLink(page.file.path))	
}

However, this leaves many lines indented so that they appear as code in the editor window:

My second attempt basically achieves the look I’m going for, but it has no affordances of outlines (e.g., folding)

const pages = dv.pages('"notes"').sort(b=>b.sequence)
for (let page of pages){
	let seq = page.sequence;
	const regex = /[\d]+|[a-z]+/g;
	let level = seq.match(regex)
	let indent = level.length
	let pre = ""
	for (var i = 0; i < indent; i++){
		pre += "--"
	}
	dv.el("p",pre+" "+seq + " " + dv.fileLink(page.file.path))	
}

I also made an attempt with markdown headings, but the headings produced by Dataview aren’t reflected in the Outline panel, so they’re not too useful.

Does anyone have any ideas that could work here? Thanks!

1 Like

You can use dv.paragraph() to render a markdown source. For example,

```dataviewjs
dv.paragraph(`
- item 1
  - subitem 1
  - subitem 2
- item 2
`)
```

this will render a nested bullet list like this:

This means the following JS query does the job:

```dataviewjs
const pages = dv.pages('"notes"').sort(page => String(page.sequence));
const markdown = pages.map(page => {
	const seq = String(page.sequence);
	const regex = /[\d]+|[a-z]+/g; 
	const level = seq.match(regex);
	const indent = level.length;
	return `${"  ".repeat(indent-1)}- ${seq} ${page.file.link}`
}).join("\n");
dv.paragraph(markdown);
```

image

1 Like

Thanks! Really nice solution. But, it doesn’t fold unfortunately, which I suppose is a Dataview / code block issue.

it doesn’t fold unfortunately, which I suppose is a Dataview / code block issue.

Yeah, I think so. You can file an issue to the Dataview repo, if you want it to come true. It will be a nice feature.

Or it might make more sense to send a FR to the Obsidian API.

So I forgot that you could use Templater to basically “print” a Dataview result into a file. This achieves what I want, it just needs to be re-run manually (or via a trigger):

function returnFolgezettel (tp) {
    const dv = app.plugins.plugins.dataview.api
    const pages = dv.pages('"notes"').sort(page => String(page.sequence));
    const markdown = pages.map(page => {
        const seq = String(page.sequence);
        const regex = /[\d]+|[a-z]+/g; 
        const level = seq.match(regex);
        const indent = level.length;
        return `${"  ".repeat(indent-1)}- ${seq} ${page.file.link}`
    }).join("\n");
  
    return markdown
}
module.exports = returnFolgezettel;
1 Like

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