obsidian-dataview icon indicating copy to clipboard operation
obsidian-dataview copied to clipboard

Rename file links in queries when file is renamed

Open ghost opened this issue 3 years ago • 5 comments

As the title describes, auto-rename file links that are in queries. dataview already autocompletes links when typing them out in a query, however when the file is renamed the link isn't renamed in the query. Obsidian automatically renames the link everywhere else though.

Query example:

WHERE project = [[Projects/P - Sample project]]

ghost avatar May 10 '21 21:05 ghost

I'll have to see if I can hook into the existing renamer to also do codeblocks; as far as I'm aware, Obsidian intentionally skips frontmatter and codeblocks by default.

blacksmithgu avatar May 12 '21 21:05 blacksmithgu

Thanks! I can imagine some people may want a switch to enable / disable this feature in the settings menu

ghost avatar May 13 '21 06:05 ghost

Exactly what I need +1

stecydube avatar Dec 31 '21 12:12 stecydube

Automatically renaming things inside Dataview scripts/queries sounds risky (it would need actual understanding of code structure), and also sounds like something that Obsidian doesn't even support (plugins are seemingly not notified about renamed files). So sure, the feature would be nice but sounds impossible at the moment.

Arcitec avatar Jun 26 '22 02:06 Arcitec

Obsidian does change wikilinks if you rename the file or move it around but I agree that it's risky to do that in code. I wonder if Obs simply ignores wikilinks in code blocks.

AB1908 avatar Jun 26 '22 10:06 AB1908

Given that DV does parse the note contents, it does sound doable but then you'd need to support both wikilinks and markdown links and it honestly may be more trouble than it's worth. Perhaps a separate plugin could cover this and we could also request Obs to include this as a toggle-able setting?

AB1908 avatar Oct 29 '22 20:10 AB1908

As a possible workaround: You could extract the link to a inline field, so that the link is a proper content of the file and dataview will rename it for you while your query stays functional. Of course, that'd mean the link shows up in your graph.

queryKey:: [[Some project]]

```dataview
LIST 
WHERE project = this.queryKey
```

s-blu avatar Dec 14 '22 19:12 s-blu

As a possible workaround: You could extract the link to a inline field, so that the link is a proper content of the file and dataview will rename it for you while your query stays functional. Of course, that'd mean the link shows up in your graph.

queryKey:: [[Some project]]

```dataview
LIST 
WHERE project = this.queryKey

the field can be made invisible with: <span style="display:none">[ queryKey::[[Some project]] ]</span> wthout "inline fields bracket syntax" does not work

dany52 avatar Jan 03 '23 15:01 dany52

I encountered this problem aswell and found my own solutien. I use these inline file links because I don't want them to appear on the graph. The direction in which I link my notes is very important because when a note links to it's parent, it is a dependency of that note. In other words the direction of links is a hirarchical dependency structure.

Here is my solution:

  1. I use templater to assign a unique identifier (YYMMDDhhmmss) to my notes
  2. Then I query this unique identifier:
dv.fileLink(dv.pages().where(p => p.note_id == 20230925104141)[0].file.path, false, "display")

There are a few caveats tho: If for some reason the ID of a note breaks I have no way of figuring out whether that note is linked somewhere.

My feature request therefor would be to allow the use of custom javascript snippets such as the ones that templater allows to use and in fact I use these a lot in my templates as I do a lot of scripting. Ideally I would want to be able to declare a simple function that I can use everywhere that is exposed through the dv object that allows me to do some error handling or more advanced stuff such as including a smoll sqllite database. @s-blu @M-bot

EDIT: updated the query

quantumfate avatar Sep 26 '23 06:09 quantumfate