KevinBatdorf / obsidian-raycast

Raycast extension with commands for the note taking app Obsidian.
MIT License
106 stars 19 forks source link

Determine existing Vault locations from obsidian.json #18

Closed cuddlecake closed 2 years ago

cuddlecake commented 2 years ago

Obsidian creates a json file that has the location of every vault that has been opened with Obsidian, with the following content:

{
  "vaults": {
    "531c5e4cb86fe7f1": {
      "path": "/Users/<user>/Development/cuddlecake/Notes",
      "ts": 1634469919450,
      "open": true
    }
  }
}

With path being the interesting part, of course.

On my machine, that file is located in:

/Users/<user>/Library/Application Support/obsidian/obsidian.json

And I believe it could be leveraged at the very least for configuration, or serve as the standard source for vault locations:

function loadVaults(): Promise<Vault[]> {
  return readFile(path.resolve(`${homedir()}/Library/Application Support/obsidian/obsidian.json`), "utf8").then(
    (data) => {
      const parsed = JSON.parse(data);
      return Object.values(parsed.vaults);
    }
  );
}

I am not 100% sure whether this file exists on every system in the same location, but it could also be a best-effort thing that could be ignored if the file is not found.

At least this is the way I implemented it in my personal extension, and I do recognize that most people do not "spam vaults" like I do 😄 But if you're interested, this could at least make onboarding for this extension a bit smoother, and for my use case, a setting to track vaults using this file would be great.

If you give me the go ahead and a quick outline of what you'd like to see, I could also try to implement this.

marcjulianschwarz commented 2 years ago

Hi @cuddlecake,

thank you for this detailed explanation. This sound like a great idea. If you want, you can implement it and create a pull request in this repo as this will be released with other features in an upcoming update.

I would suggest adding a new function to utils.tsxwhich uses the existing parseVaults() function if the obsidian.json file doesnt exist.

If you need something else, please let me know.

marcjulianschwarz commented 2 years ago

There might be some issues with the package.json configs. In there you have to specify whether a preference is optional or required. Right now, a vault path or multiple vault paths is a required preference as the extension doesnt work without it. With the obsidian.json file this setting has to be optional when it exists and required when it doesnt. I dont know yet how this behaviour could be achieved.

cuddlecake commented 2 years ago

I took a stab at implementing something, but yeah, not being able to customize the onboarding experience is definitely a hindrance. My current attempt would use the vaultpaths from preferences if it is already configured, otherwise use vault paths from obsidian.json like this.

You can see the code on the main branch of my fork: https://github.com/cuddlecake/obsidian-raycast where I updated just the Create Note Command.

Not feeling comfortable with it, because I do see the value of having an obsidian vault being the focus of the plugin, as I primarily work with one specific vault as well.

marcjulianschwarz commented 2 years ago

Thanks for trying to implement it.

Can we somehow verify that obsidian.json exists for every Mac-user? I can ask in the Obsidian discord server, I guess someone there will know. If this has been verfied then we are good to go. We should however keep the preference (marked as not required) so that one can "select" vaults as one may not want all vaults to be used by Raycast.

cuddlecake commented 2 years ago

Created a PR and updated all the commands to use the obsidian.json vaults if preference.

I would expect every Obsidian installation to have an obsidian.json as soon as at least one vault has been created or opened with that Obsidian installation. Tested this with a clean install of Obsidian, where the obsidian.json was created when I opened a vault.

So, what to do in the case of a user not even having opened any vaults? One solution could be to show a Detail with content like this: image