想把日记中,4级标题下的内容,用dataviewjs,汇总起来。
请问代码要怎么写呢?
翻了翻论坛的帖子,发现很多人有这个需求,这是一个老生常谈的问题了,却从未被解决!
是不是这个问题很难啊,居然没有人回复耶
我也想知道 哈哈 一直没解决
这个问题会写点代码很简单,这是我的工作流:quickadd读取日记里的标题然后汇总后写入一个新文件。但请不要问我下列问题:
module.exports = async (params) => {
const allFolders = app.vault.getAllLoadedFiles()
.filter(f => f instanceof params.obsidian.TFolder)
.map(folder => folder.path);
const targetFolder = await params.quickAddApi.suggester(allFolders, allFolders)
const targetHeading = await params.quickAddApi.inputPrompt("请输入待合并的标题:", placeholder = "日志", value = "日志")
const files = app.vault.getAbstractFileByPath(targetFolder).children.filter((file)=>{return file.extension === "md"})
//console.log(files)
let resultContent = ""
files.forEach(async (file) => {
const headingName = file.name.split(".")[0]
const headings = app.metadataCache.getFileCache(file).headings
const contents = await app.vault.cachedRead(file)
//console.log(headings, contents)
let startLine
let endLine
let headingIndex
headings.some((heading, index)=>{
if(heading.heading === targetHeading){
startLine = heading.position.start.line
headingIndex = index
return true
}else{
headingIndex = -1
}
})
if(headingIndex + 1 != headings.length){
endLine = headings[headingIndex + 1].position.start.line
}else{
endLine = contents.split("\n").length
}
const headingList = contents.split("\n").slice(startLine + 1, endLine)
const resultHeading = `## ${headingName}\n` + headingList.join("\n")
resultContent += resultHeading
params.variables["resultContent"] = resultContent
params.variables["resultFilePath"] = "000-Plan/mergedHeading.md"
})
}
module.exports = async (params) => {
let resultContent = []
const files = app.vault.getMarkdownFiles()
files.forEach(async (file)=>{
const content = await app.vault.cachedRead(file)
content.split("\n").forEach((line)=>{
if(/-\s\[[\s\S]*?\]\(http[\s\S]*?\)/m.test(line)){
const href = line.match(/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/)[0]
let flag = false
//console.log(href)
resultContent.some(el=>{
if(el.includes(href)){
flag = true
return true
}
})
if(!flag){
resultContent.push(line)
}
//console.log(resultContent)
params.variables["resultContent"] = resultContent.join("\n")
params.variables["resultFilePath"] = "Resource/mergedLinkResult.md"
}
})
})
}
module.exports = async (params) => {
let content
if(!params.variables["resultContent"]){
content = await params.quickAddApi.inputPrompt("待写入的文件内容:")
}else{
content = params.variables["resultContent"]
}
try{
await app.vault.adapter.write(params.variables["resultFilePath"], content)
console.log("文件写入成功")
}catch{
console.log("文件写入失败!")
}
}