如何动态设置dataview显示当前目录下文件目录树

初学dataview
目前找到的帖子展示子文件夹都是用的手写路径

尝试着将别人写的dataviewjs中展示md文件的片段截取转换为显示子文件夹,但都不行。

dv.current().file.folder 可以取得当前笔记的文件夹的路径

以下基本全都抄自 Hierarchical list of files with the dataview plugin - Help


```dataviewjs
let dir = dv.current().file.folder;

let processed = [];

function listRecursive(folder, depth) {
	let files = [];
	// All pages in the scope of the current path
	let pages = dv.pages('"' + folder + '"')
	// Collect files in the current folder here
	let currentFiles = "";
	pages.forEach(page => {
		if (page.file.folder === folder) {
			// Page is in current folder
			currentFiles += page.file.link + " | ";
		}
		else {
			// Page is in subfolder
			let nestedFolder = page.file.folder;
			// Make sure nested folder is direct child, not any other descendant from current folder
			let isChild = folder.split('/').length + 1 == nestedFolder.split('/').length;
			// Make sure we dont process sub-directories multiple times
			if (!processed.includes(nestedFolder) && isChild) {
				processed.push(nestedFolder);
				// Result of recursive call is a list, by adding it to the current list we recursively build a tree
				files.push(listRecursive(nestedFolder, depth + 1));
			}
		}
	});
	
	if (currentFiles.endsWith(" | "))
		currentFiles = currentFiles.slice(0, -3);
	
	// Add files in current folder at the start
	if (currentFiles !== "") files.unshift(currentFiles);
	
	// Add current folder name at the start
	let path = folder.split('/');
	path = path[path.length - 1];
	
	if (depth == 0) path = path;
	
	files.unshift("<h3>" + path + "/ </h3>");
	return files;
}

let files = listRecursive(dir, 0);
dv.list(files);
```

效果大致是

1 个赞