QuickAdd 读取 data.json 到文档以管理 QuickAdd Capture

效果 GIF,点击展开

20240401_163608

  • 根据情况自行调整代码。

  • 楼主的 Capture 存放在以 C: 开头的 Multi 内,且 Multi 内只有 Capture,体现为代码中的 pre = 'C:'。混放的根据情况自行添加 .filter()

    顺序是所有 Capture 在 Macro 上方。

    示意

    image

  • Macro 中的 Capture 以 C 开头,体现为代码中的 const caps....startsWith('C ')

    示意

    image

  • file = 后面写你想输出到的 MD 文档路径。这个文档不要再存别的信息。

  • 不宜操作太快。

  • 使用前备份 data.json。

    image

基本配置参 QuickAdd JS & Templater JS 简介及相互修改“QuickAdd Capture 加载内部代码”。

js quickadd 代码
const fs = require('fs'), pre = 'C:', file = app.vault.getAbstractFileByPath('文件目录/QuickAddson.md')
, id = 'quickadd', f = `${id}/data.json`, A = (await app.vault.read(file)).split('\n'), fc = app.metadataCache.getFileCache(file)
, I = (c, arr, end)=> {
  const i = arr.findIndex(i=> i.heading == c.name), pos = arr[i].position.end.line+2
  c.format.format = (arr[i+1] ? A.slice(pos, arr[i+1].position.end.line-1) : A.slice(pos, end)).join('\n')
}
, IO = (id, f, fun)=> { const path = `${app.vault.adapter.basePath}/.obsidian/plugins/${f}`
  , func = f.endsWith('.json') ? data=> { const obj = JSON.parse(data); fun(obj); return JSON.stringify(obj, null, 2) } : fun
  fs.readFile(path, 'utf8', (err, data)=> { if (err) throw err; const now = func(data); if (!id) return
  fs.writeFile(path, now, 'utf8', err=> { if (err) throw err }); app.plugins.disablePlugin(id); app.plugins.enablePlugin(id) })
}
switch (await this.quickAddApi.yesNoPrompt('读 (no) or 写 (yes)')) {
case false:
IO(null, f, async obj=> { let str = ''
  obj.choices.map(p=> (p.type == 'Multi' && p.name.startsWith(pre)) && p.choices.map(c=> str += `## ${c.name}\n\n${c.format.format}\n\n`))
  obj.macros.map(p=> { const comms = p.commands.filter(c=> c.type == 'NestedChoice'); if (!comms[0]) return
    str += comms.map(c=> `## ${c.choice.name}\n\n${c.choice.format.format}\n`).join('\n')
  }); await app.vault.modify(file, str)
}); break
case true:
IO(id, f, obj=> { const caps = fc.headings.filter(p=> !p.heading.startsWith('C ')), macs = fc.headings.slice(caps.length)
  obj.choices.map(p=> (p.type == 'Multi' && p.name.startsWith(pre)) && p.choices.map(c=> I(c, caps, macs[0].position.end.line-1)))
  obj.macros.map(p=> p.commands.filter(c=> c.type == 'NestedChoice').map(c=> I(c.choice, macs, -1)))
}); break }
1 个赞

这样是不是方便分享脚本了,导出和导入