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:


// 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 =
  if (page != undefined) {
    let backlinks = page.file.inlinks
    let links = page.file.outlinks

// Got lost here...
      let content = await
      let referenceText = content.match(/^[^\.!?]*([^\.!?]*[\.!?]/i)

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

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

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


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