Dataviewjs的奇技淫巧

dataviewjs确实可以实现,要放在以前,我肯定会用正则硬去匹配标题,但是现在我发现ob其实可以直接获取文件对应的标题 :joy:

let files = dv.pages(`"02-Daily Notes"`)
let targetHeader = '[[Project 1]]'
let headers = files.map(p => {
		let tf = app.vault.getAbstractFileByPath(p.file.path)
		let header = app.metadataCache.getFileCache(tf)
			.headings
		if (!header) return
		let h = []
		let b = false
		for (let i of header) {
			if (b && i.level == 3) h.push(i.heading)
			if (b && i.level == 2) b = false
			if (i.heading == targetHeader && i.level == 2) b = true
		}
		return h.length == 0 ? false : [p.file.link, h]
	})
	.filter(p => p)
dv.table(['file', 'headers'], headers)
1 个赞