实例一
- 空文档包括:
- 大小为 0 的文件(全部后缀)。
- 仅有空白字符的 MD 文档。
- 复选框控制是否查询仅有 YAML 的 MD 文档。默认前加“Y”标识,可自行更换标识。
- 删除按钮默认至系统回收站,方便原路径还原反悔。任何疑问请查看系统回收站。
代码
const check = dv.container.createEl('input', {type: 'checkbox'})
const items = []; let isFile = !0
dv.el('button', '切换').onclick = ()=> { isFile = !isFile; lookup() }
/**
dv.el('button', '清空').onclick = ()=> items.filter(
dFile=> isFile ? !dFile.extension : dFile.extension
).map(dFile=> trash(dFile))*/
const trash = async dFile=> await app.vault.trash(dFile, !0)
const form = (dFile, note)=> {
items.push(dFile)
const btn = dv.el('button', '删除')
btn.onclick = ()=> { trash(dFile); btn.textContent = '已删' }
const emptyFile = `${note||''}[[${dFile.name}]]`
return isFile ? [dFile.parent.path, emptyFile, btn] : [dFile.path, btn]
}
const table = dv.el('div')
const lookup = ()=> {
table.empty(); const tdata = []
app.vault.getAllLoadedFiles().map(dFile=> {
if (isFile) {
const fc = app.metadataCache.getFileCache(dFile); if (!fc) return
const { sections: secs, frontmatter } = fc
if (
dFile.stat?.size == 0 || dFile.extension == 'md' && !secs
) tdata.push(form(dFile))
if (
check.checked && dFile.extension && frontmatter && !secs[1]
) tdata.push(form(dFile, 'Y'))
}
else if (!dFile.extension && !dFile.children[0]) tdata.push(form(dFile))
})
const thead = isFile ? ['路径', '空文档', ''] : ['空文件夹', '']
dv.api.table(thead, tdata, table, dv.component)
}; lookup(); check.onchange = lookup
注释掉的一项为清空空档按钮,按需启用。
实例二
得益于 交流向实验性 QuickAdd 批量重命名小标题防止双链断裂 发现的 API。
-
始终查找全库断裂双链(灰链、未创建链…),如示意图左上角。默认前加“
︎”标识。
排除 Templater 模板使用语法的双链。 -
未选择时默认查找当前活动文档所在文件目录未被引文件(全部后缀)。
故可把代码放在附件文件夹;
也可把代码所在文档开在旁边,迅速查看多个文件夹。示例 GIF,点击展开
-
240418 更新查找框
代码
const genSele = (inpu, items)=> {
const sele = createEl('datalist', {attr: {id: 'demo'}})
inpu.setAttribute('list', 'demo')
for (const item of items)
sele.createEl('option', {value: item.path})
return sele
}
const inpu = createEl('input', {attr: {style: 'width: 120px;'}})
const folders = app.vault.getAllLoadedFiles().filter(p=> !p.extension)
const sele = genSele(inpu, folders)
dv.container.append(inpu, sele)
let whole = !1, folder
inpu.onchange = ()=> {
folder = folders.find(p=> p.path == inpu.value)
if (folder) lookup()
}
/**
dv.el('button', '全库').onclick = ()=> {
whole = !0; lookup(); whole = !1
inpu.placeholder = `来源: 全库`
}*/
const table = dv.el('div')
const lookup = ()=> {
table.empty()
if (!folder || folder == folders[0])
folder = app.workspace.getActiveFile().parent
const fdPath = folder.path
inpu.placeholder = `来源: ${fdPath}`; inpu.value = ''
const unlinked = Object.entries(
app.metadataCache.unresolvedLinks
).flatMap(([rPath, unlinks])=> {
const aoa = Object.keys(unlinks).filter(link=> !link.startsWith('<%'))
return aoa[0] ? [rPath, aoa] : []
})
const linked = Object.values(
app.metadataCache.resolvedLinks
).reduce((acc, obj)=> Object.assign(acc, obj), {})
const tdata = app.vault.getFiles().filter(p=> {
if (whole) return p
else {
const sameLevel = p.path.includes('/')
? p.path.slice(0, fdPath.length) == fdPath
: fdPath == '/' // the first level
return sameLevel && !linked[p.path]
}
}).map(p=> [p.parent.path, `[[${p.path}|${p.name}]]`])
dv.api.table(
['路径', '未被引'], [unlinked, ...tdata], table, dv.component
)
}; lookup()
注释掉的一项为查找全库未被引文件按钮,按需启用。
彩蛋
使用 QuickAdd 正则保值替换选单 便捷格式化双链,主要适用于非嵌入的 Wiki 链接。
会,可在原帖示例代码上继续添加新项;
不会,将下面给出的 rgx
和 form
组合分别覆盖替换原帖代码 sus[0]
和 sus[1]
后面的内容。
1、 →
[[文件路径/Wiki]]
→ [[文件路径/Wiki|Wiki]]
rgx = /(?<!!)\[\[([^\|]+?)]]/gm; form = (m, p1)=> `[[${p1}|${p1.split('/').slice(-1)}]]`
2、 →
rgx = /(?<!!)\[\[(.+?)]]/gm; form = (m, p1)=> p1.split('|').slice(-1)