• 需要在dataview设置中打开js。

实例结果示意:

  1. 左图是一份md文档的内容概览(markdown是obsidian的基本储存形式),该文档内具有标题# Linked
    1. 【前提!】本实例需要在不同文档中,都已经**预先**写有相同的标题,如 “# 章节梗概/章节大纲”。
  2. 右图,则是通过dataviewjs搜索与筛选了目标文件夹内的文档,并显示了最终的结果页面。

以下为代码。

直接复制,替换目标文件夹# 标题名 后,即可使用。

```dataviewjs
function naturalCompare(a, b) {
    const removeLeadingZeros = s => s.replace(/(\d+)/g, n => n.padStart(10, '0'));
    return removeLeadingZeros(a).localeCompare(removeLeadingZeros(b));
}
const folderPath = "目标文件夹";
const pages = dv.pages(`"${folderPath}"`);
let results = [];
for (const page of pages) {
    const file = app.vault.getAbstractFileByPath(page.file.path);
    if (file) {
        const fileContent = await app.vault.read(file);
        const lines = fileContent.split('\n');
        let capture = false;
        let linkedContent = "";

        for (const line of lines) {
            if (line.startsWith("# 标题名")) {
                capture = true;
                continue;
            }
            if (capture && line.startsWith("#")) {
                capture = false;
            }
            if (capture) {
                linkedContent += line + "\n";
            }
        }
        if (linkedContent.trim()) {
            results.push({ name: page.file.name, path: page.file.path, content: linkedContent });
        }
    } else {
        console.log("未找到文件: " + page.file.path);
    }
}
results.sort((a, b) => naturalCompare(a.name, b.name));
for (const result of results) {
    const fileLink = `[${result.name}](obsidian://open?vault=${app.vault.getName()}&file=${encodeURIComponent(result.path)})`;
    dv.header(3, fileLink);
    dv.paragraph(result.content);
}
```