Creating a Dataview Backlinks Panel with Context

What I’m Trying to Do

I am trying to make some Dataviewjs code that will show the backlinks and outlinks of the active note from the sidebar and show the context of each link. By context, what I mean is the line or sentence that the link can be found in. It would look sort of like this:

Except, of course, “Some description” would be replaced by the sentence the link apears in.

What I’ve Tried

Here is the code used in the image above:

```dataviewjs

// This gets info from the active pane. 

const getFirstSplit = (split) => { if (split.type == "tabs") { return split } else if (split.type == "split") { return getFirstSplit(split.children[0]) } }
let firstSplit = getFirstSplit(app.workspace.rootSplit)
let current = firstSplit.children[firstSplit.currentTab].view.file
let page = dv.page(current.path)
  
  if (page != undefined) {
    let backlinks = page.file.inlinks
    let links = page.file.outlinks

// Got lost here...
      let content = await dv.io.load(current.path)
      let referenceText = content.match(/^[^\.!?]*(page.file.link)[^\.!?]*[\.!?]/i)


referenceText = "Some description" // demo purposes; remove later

// Backlinks Section
    if (backlinks != undefined) {
		backlinks = backlinks.map(p => p + "<br> " + referenceText)
      dv.header(1, "==" + backlinks.length + "== backlinks for *" + page.file.name + "*")
      dv.list(backlinks)
    }

// Outlinks Section
 if (links != undefined) {
		links = links.map(p => p + "<br> " + referenceText)
      dv.header(1, "==" + links.length + "== pages linked in *" + page.file.name + "*")
      dv.list(links)
    }


}
```

It’s based on this Novel info panel code I saw earlier, so that’s where most of the code comes from, and I don’t really know what I’m doing—mostly just made this through trial and error. I feel like I’m pretty close, but now I’m stuck.

Does anyone know how I could change this so that the referenceText for each backlink and outlink is retrieved and displayed like above?

1 Like

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