generateMarkdownLink bug? (Uncaught TypeError: Cannot read properties of null)

Using generateMarkdownLink on Obsidian version 1.8.7 (Installer 1.5.11) seems to outright not work?

Trying a basic example such as

app.fileManager.generateMarkdownLink(app.vault.getAbstractFileByPath('Folder/A.md'), 'Folder/B.md')

gives me:

Uncaught TypeError: Cannot read properties of null (reading 'extension')
    at t.fileToLinktext (app.js:1:1921765)
    at e.generateMarkdownLink (app.js:1:1269853)
    at <anonymous>:1:17
t.fileToLinktext @ app.js:1
e.generateMarkdownLink @ app.js:1
(anonymous) @ VM434:1

Is it me who’s using it wrong or is this a bug?

Basically, what I want to do is generate a link to a non existing file.

In your code app.vault.getAbstractFileByPath('Folder/A.md') returned null, so the file couldn’t be found. Ensure you have the correct case-sensitive path

You can’t do that with Obsidian API. You need to do it manually.

For example:

this.app.vault.getConfig("useMarkdownLinks")
  ? `[${getBasenameFromPath(path)}](${encodeLinktext(path)})`
  : `[[${path}]]`

But in reallity there are a lot more to consider, for example the “new link format” option and the file type. If you can create the target file first, that would be better

1 Like

Actually, it is possible to generate a link to a non-existing file, but it’s quite challenging. I have it in my library:

2 Likes