Trouble calling a CustomJS script

I slight upgrade would be to also give context on those files:

// Define folderstoCheck array (Add your own folders)
const folderstoCheck = ["Placeholder1", "Placeholder2"];

let currentPage = dv.current().file.path;
let pages = new Set();
let stack = [currentPage];
while (stack.length > 0) {
    let elem = stack.pop();
    let meta = dv.page(elem);
    if (!meta) continue;

    for (let inlink of meta.file.inlinks.array()) {
        if (pages.has(inlink.path)) continue;

        // Check if any folder is included in the inlink path
        if (folderstoCheck.some(folder => inlink.path.includes(folder))) {
            pages.add(inlink.path);
            stack.push(inlink.path);
        }
    }
}

const processedData = [];

for (let page of pages) {
    const content = await dv.io.load(page);
    const inlinkedFileName = dv.current().file.name.replace(/\.[^/.]+$/, ""); // Extract the file name without extension
    const inlinkRegex = new RegExp(`\\[\\[${inlinkedFileName}.*?\\]\\]`, "g");
    const paragraphs = content.split(/\n\s*\n/);

    for (let paragraph of paragraphs) {
        if (inlinkRegex.test(paragraph)) {
            // Remove the path and format the file name with double square brackets
            const fileName = `[[${page.split('/').pop().replace(/\.[^/.]+$/, "")}]]`;

            processedData.push({
                link: fileName,
                content: paragraph
            });
        }
    }
}

dv.table(
    ["Source File", "Context"],
    processedData.map(p => [p.link, p.content])
);

For anyone wanting to do what OP wanted:
Save the file as a javascript file in a folder that is not the Templater user scripts folder, but some other folder.

Put in the foldertoCheck array the name of the folders that contain your Zotero annotations or whatever.

Then you can put the query call in a callout and even in a default template, like so:

> [!warning]+ Remember to incorporate this
> ```dataviewjs
> dv.view('javascriptfilenamewithnoextension')
> ```
1 Like