const currentFolder = dv.current().file.folder;
const currentFile = dv.current().file.path;
 
// Find all notes in this folder and its subfolders
// excluding the current note and Master files
const pages = dv.pages(`"${currentFolder}"`)
    .where(p =>
        p.file.path !== currentFile &&
        !p.file.name.toLowerCase().includes("master")
    )
    .sort(p => p.file.path);
 
// Group the notes by folder
const groups = pages.groupBy(p => p.file.folder);
 
// Display each folder once
for (const group of groups) {
 
    const folderParts = group.key.split("/");
    const folderName = folderParts[folderParts.length - 1] || "Root";
 
    dv.header(2, folderName);
 
    for (const page of group.rows) {
 
        // Link directly to the original note
        dv.el("div", dv.fileLink(
            page.file.path,
            false,
            "↗ Open original note"
        ));
 
        // Embed the original note
        dv.el("div", dv.fileLink(
            page.file.path,
            true
        ));
 
        // Space between notes
        dv.el("div", "", {
            attr: { style: "height: 20px;" }
        });
    }
}