将下面这段统计内容的代码改成统计行数。dataviewjslet files = dvpages工作工作日报HeaderAggregationfiles标题1标题2async function HeaderAggregationfilesheaders let d = await Promiseallfilesmapasync functionfile let contents =
修改后的代码如下所示:
let files = dv.pages(`"工作/工作日报"`)
HeaderAggregation(files,['标题1','标题2'])
async function HeaderAggregation(files,headers) {
let d = await Promise.all(files.map(async function(file) {
let contents = []
for(let i in headers) {
let content = await app.vault.readRaw(file.file.path)
let header = content.match(/^#+ .*$/mg)
if(!header) return
header = header.map(p=>[p.split(' ')[0].length,p.slice(p.split(' ')[0].length+1)])
content = content.split(/^#+ /m)
let index = header.indexOf(header.find(p=>p[1]==headers[i]))
if(index==-1) continue
let num = []
for(let j =index+1;header[j]&&header[j][0]>header[index][0];j++) num.push(header[j]);
let c = content.find(p=>p.startsWith(headers[i]))
for(let j=0;header[index+1+j] && j<num.length;j++)
c+='#'.repeat(header[index+1+j][0])+' '+content.find(p=>p.startsWith(header[index+1+j][1]))
contents.push(c?.slice(headers[i].length)??'')
}
if(contents.every(p=>p.replace(/\s+/,'')=='')) return
return [file.file.link,...contents]
}))
d=d.filter(p=>p)
dv.header(`行数: ${d.length}`)
}
修改后的代码将统计结果输出为行数,使用dv.header()函数将行数显示在Dataview的头部
原文地址: https://www.cveoy.top/t/topic/h5yi 著作权归作者所有。请勿转载和采集!