Probably somewhat of an overkill (I’m not much on javascript myself), but you can use something like this:
```dataviewjs
// Query all pages
const allPages = dv.pages("");
// Crawl content and render list
const pages = await Promise.all(
allPages.map(async (page) => {
const content = await dv.io.load(page.file.path);
const regexPattern = /.*\bSEARCHTERM\b.*/gm;
const regexMatches = content.match(regexPattern);
if (regexMatches && regexMatches.length > 0) {
return {
link: page.file.link,
content: regexMatches.join('\n\n')
};
}
return null;
})
);
// Filter out null entries and render the result list
const processedPages = pages.filter(p => p !== null);
dv.list(
processedPages.map(p => `${p.link}\n${p.content}`)
);
```
- If you want to specify a folder, change
const allPages = dv.pages("");
toconst allPages = dv.pages('"FOLDERNAME"');
- Change SEARCHTERM to your own string you want returned. Spaces between strings should be allowed but you may do well to use
\s
for space, like so:cats\sand\sdogs
. - You can remove the
\b
’s fromconst regexPattern = /.*\bSEARCHTERM\b.*/gm;
like so:const regexPattern = /.*SEARCHTERM.*/gm;
to get more – sometimes unwanted – results.
Script returns the paragraph(s) containing searchterm specified.
Glædelig jul!