Return a string of text and the backlink via dataview

I was stuck in the chimney…but here we go:

```dataviewjs
// Query all pages within the specified folder
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) {
            // Apply replacements to each matched content before storing in pages
            const processedContent = regexMatches
                .map(match => {
                    let processed = match.replace(/>/g, '>'); // Replace > with >
                    processed = processed.replace(/^>\s*/gm, ''); // Remove > and spaces at the start of each line
                    return processed;
                })
                .join('<br><br>'); // Separate each processed match with two line breaks

            return {
                link: page.file.link,
                content: processedContent
            };
        }

        return null;
    })
);

// Filter out null entries and build the list string with proper formatting
const listOutput = pages
    .filter(p => p !== null)
    .map(p => `${p.link}\n${p.content}` + `<br><br>`); // Include breaks between list items as well

// Output the formatted list
dv.list(listOutput);
```
  • Using <br><br> instead of \n\n for line breaks as the latter didn’t cut it for me.
  • You can use processed = processed.replace(/(^>\s*)|(^-\s*)/gm, ''); to remove list dashes as well.
1 Like