Search in a page and all its linked notes?

What I’m trying to do

Is it possible to limit a text search to a page as well as all of the pages that it links to or that link to it? This would be a gamechanging feature for me, but I haven’t figured it out yet.

1 Like

I don’t know if there’s a better way to do this, but it’s easy to do with Dataview:

```dataviewjs
const searchString = 'asdf'

const me = dv.current().file
const matches = [...me.inlinks.array(), ...me.outlinks.array()]
  .filter(async (page) => {
    const file = app.vault.getAbstractFileByPath(page.path)
    return (await app.vault.read(file)).match(new RegExp(searchString))
  })
  .map(x => dv.page(x.path).file.link)

dv.list(matches)
```

Paste this into any page, and it will search the inlinks and outlinks for that page for the string asdf (or whatever you specify for the searchString variable).

3 Likes

Using the sidebar search, where “term” is your search term and “note” is your note name: term (file:/^note$/ OR /\[\[(.+/)?note(\||\[)/)

(This assumes no duplicate note names.)

Not something you want to type every time, but you can save it (using the core Starred plugin or as an embedded search) and replace the term and note when you use it.

Details:

file:/^note$/ uses a regular expression to match the note name exactly (^ means the start of the string and $ the end).

The regular expression /\[\[(.+/)?note(\||\[)/ matches

  • 2 left square brackets,
  • optionally followed by any number of characters ending with a slash (in case the link includes a folder),
  • followed by the note name,
  • follows by either a pipe or a right square bracket.

Square brackets and pipes both mean things in regular expressions, so most of them are preceded by backslashes to mark them as just plain text, making the whole thing harder to read.

1 Like

It was hard to pick just one solution. Thank you both!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.