How to get full paths from link text

This seems like an embarrassingly basic question, but I’m really struggling.

Given the link text from a link to a file in a document, that may be an absolute path, a relative path or a short path, how do I find the full path that Obsidian will resolve the link to? This has to be something that people are doing all the time but I can find a solution that works.

Things that I’ve tried that don’t seem to be the correct solution:

app.fileManager.getAllLinkResolutions
getLinkPath
app.filesystem.adaptor.getFullPath

In which context are you working? Templater? QuickAdd? Dataview(js)??

It’s inside a Plugin addCommand

It’s kind of hard to help when you don’t say anything about how and where you want to get this information. It doesn’t provide us with any hints as to what’s available for you to use.

That you want to use it in a call to addCommand() doesn’t really tell anything.

However, it might be that getFirstlinkPathDest(linkpath, sourcePath) could work for you.

Another option if you’re using dataview could be to use dv.page() to get the full metadata on a given page. This also includes a full path to the page it retrieved.

Here are two examples where I’m looking for the a note called 2022-11-22 in my test vault (and it happens to exists multiple times):

```dataviewjs

const metaVariant = app.metadataCache.getFirstLinkpathDest("2022-11-22", "")
dv.paragraph("getFirstLinkpathDest:   " + metaVariant.path)

const dvPageVariant = dv.page("2022-11-22")

dv.span("dv.page: " + dvPageVariant.file.path)
```

In my test vault this produced this output:

Another similar variant would be the getLinkpathDest() which returns all matches against the given name.

const metaVariant = app.metadataCache.getLinkpathDest("2022-11-22", "")
dv.list(metaVariant.map(p => p.path))

Which for me produced:
image

The second parameter to the linkpath dest functions is the path to the file. If omitted or wrong, they’ll return the first and best. If fully specified, then that version of the file is picked. (Note, I’ve not used these functions a lot, so I don’t know which caveats there are related to using either of them )

Thank you, that’s great. I really appreciate the help and I’m sorry that my previous post and response didn’t give enough detail.

I would have given a minimal example but my ears were being pulled off by a four year old at the time :slight_smile:

When I said that the context was in a Plugin addCommand, what I should have said to be more clear was that it is inside the callback of an addCommand call inside the onload method of a Plugin.

Here is a minimal example. Your suggestion of getFirstLinkPathDest has solved the issue for me.

export default class SomePlugin extends Plugin {
    async onload() {
        this.addCommand({
            id: "some-plugin-command",
            name: "Do Stuff",
            callback: () => {
                // grab a random markdown file
                let md_file = this.app.vault.getMarkdownFiles().first();
                if (!md_file) return;
        
                // find the first webm embed in the file and extract the link text
                let md_file_cache = this.app.metadataCache.getFileCache(md_file);
                let audio_link = md_file_cache?.embeds?.find((e) => e.link.endsWith(".webm"))?.link;
                if (!audio_link) return;
        
                // ** THIS IS WHERE I WAS STUCK **
                // discover the file path from the link text
                let audio_path = this.app.metadataCache.getFirstLinkpathDest(audio_link, md_file.path)?.path;
                if (!audio_path) return;
        
                // get the resource path from the file path
                const audio_resource = this.app.vault.adapter.getResourcePath(audio_path);
        
                // play the file
                let audio = new Audio(audio_resource);
                audio.play();

		},
            });
        }

	onunload() {}

}

It feels like I might be missing a less convoluted way to achieve this but it’s working so I’m happy.

1 Like

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