Obsidian Editor help to navigate to line based on match from DataViewJS table

I am running the following on my longer files:

```dataviewjs
// Define the regular expression
const regex = /\[\[(?!.*\|)([^[\]]+#[^\]]+)\]\]/g;

(async () => {
  const content = await dv.io.load(dv.current().file.path);

  // Perform the regex match on the current page's content
  const matches = content.matchAll(regex);

  const matchedLines = [];

  for (const match of matches) {
    matchedLines.push(match[0]); // Push the matched line to the array
  }

  if (matchedLines.length > 0) {
    const tableData = matchedLines.map((line) => {
      return [dv.current().file.link, line];
    });

    dv.table(
      ["Note", "Matching Line"],
      tableData
    );
  } else {
    console.log("No matches found on the current page.");
  }
})();
```

What it does is gives me the links where I am yet to put in aliases, e.g.
[[London#Big Ben]] needs to be changed to [[London#Big Ben|Big Ben]].

  • The reason I cannot run a regex replace on all my files because sometimes the alias must be lowercase. So I need to do it manually.

Currently the table sort of works (anybody can try it who has DataView installed and a link with a heading like mine), but what I’d like to do is when I click the match, I don’t want to be taken to the file with the link (as it does now), but be navigated to the line in the current file where the change must be made. After a few tries with ChatGPT, I gave up (because Obsidian has a new editor).

It seems like a niche case but maybe other people would be interested as well.

Thanks

AFAIK, there is no go to line links available within Obsidian, except when you’re doing searches either in the side panel or using the query searches. So it could be an option for you to change your regex to something accepted in the search field of Obsidian, and then use the search result for your base list for where to change.

Another option would be to switch to something like VS Code, and do a search-and-replace on your vault, and then use its result navigation to semi-automagically replace those you want replaced. That is, hit replace for those you want, and skip to next for those you don’t want replaced.

1 Like

Yeah, in the meantime I thought of trying to get an intent or hook to the search panel with the current file name plus the regex, using it as a workaround. But I cannot feed what I don’t know to my robot (cheap guy).

Then I was thinking I could get the list of my Capital letter filenames and use that somehow with Notepad++.

Then, just as you answered, it hit me, on the heels of the first idea:
Inline query (I need to write this down fast, before Cawlin comes):

```query
file: <filename>.md /\[\[(?!.*\|)([^[\]]+#[^\]]+)\]\]/
```

I will take the current filename from my YAML key títle and do it with Notepad++ on every file.
That way, when you click on the match, you’ll be taken to the line that needs changes.
Downside is that in a large vault, the query is slower than doing a load of dv.curent on the current file.

Why can’t you just do that search in the side panel, without limiting it to the current file? If your vault is too large, you could potentially limit it to only do certain folders or so, and keep tidying it up bit by bit.

I have 1045 items to go through and many other jobs down the road.
And even if I were to up and do the job, I’d still need something of a reminder that the next time I make a similar link, I’d need to alias it because they look ugly when published.
Like this, I can get some feedback and quickly go to the line to insert the alias manually, depending on whether it is e.g. a common noun or not.
I also have a Dashboard canvas where I run 4-5 full-vault DVJs queries with similar reminders (like if there was an update on a file that had been published, I need to go into the file and run the publish again on it to update the online copy).

  • Previously, I just kept some regex searches among my Bookmarks; now I convert these to DVJs queries.

By the way, if anyone is interested, how I got along with this project:

  • For some reason, Notepad++ didn’t find as many files to update with the code as Sublime Text, which has other known drawbacks…

At the same time, I’m not that happy to have polluted the files, because I could have cleaned up the files and run a full-vault DVJs query with the regex and put the results on the Dashboard with the others.
But at the same time, it’s good to have some reminder for oneself that one can put in something like this next time around. Then it would be easy to exchange the current regex with something else.
But now one must remember to put in the query in one’s Templater templates as well…

In the meantime, I made 2 more queries for each note to help with review of notes before publishing them.

By the way, the Obsidian query file: "filename.md" /regex query pattern/ doesn’t give good results.
I thought of putting up a bug report again, but it may come back as no bug. So, instead, I was using regex for the file names too:
file: /^filename.md/ /regex query pattern/

  • This will give proper results for the current file. Using the double quote method, sometimes I would get results for Def abc.md as well as Abc.md.

See also this thread:

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