Skip to content

feat: Add obisidian API's free functions to API #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dljsjr
Copy link
Contributor

@dljsjr dljsjr commented Feb 20, 2025

Adds the Obsidian free functions to the app object under the functions object.

This provides access to a bunch of useful built-in Obsidian utilities via the local API.

Adds the obsidian free functions to the `app` object under the
`functions` object.
@blacksmithgu
Copy link
Owner

Aren't these already available as app.<function>? I thought the Obsidian app object was already in the global context.

@FeralFlora
Copy link

Aren't these already available as app.<function>? I thought the Obsidian app object was already in the global context.

Usage of global app is discouraged, see:
https://docs.obsidian.md/Plugins/Releasing/Plugin+guidelines#Avoid+using+global+app+instance

and:
https://github.com/Fevol/obsidian-typings/blob/main/CHANGELOG.md#114

NOTE: the global app variable has been removed from the official Obsidian API as of 1.6.0, it will not be re-added by obsidian-typings

@dljsjr
Copy link
Contributor Author

dljsjr commented Mar 5, 2025

Aren't these already available as app.<function>? I thought the Obsidian app object was already in the global context.

No, the free functions like normalizePath aren't attached to the app object. They're exported as part of the obsidian namespace, meaning they aren't available to code blocks since you can't import "obsidian"

@RyotaUshio
Copy link
Contributor

RyotaUshio commented Mar 18, 2025

I'm afraid I don't think it's a great idea to modify the app object itself. Also, exposing the obsidian module to the global context can be helpful but is it really inside Datacore's scope?
It might make sense if it is added to the local scope where we write queries.

Copy link
Collaborator

@GamerGirlandCo GamerGirlandCo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good work. keep in mind that monkey patching app is advised against, because it's not a matter of "if" but "when" an update to obsidian itself will break everything.

@@ -17,6 +17,7 @@ declare module "obsidian" {
}
interface App {
appId?: string;
functions: Record<string, typeof Function>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this. don't add new properties to app.

}
}
}
this.app.functions = obsidianFreeFunctions;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.app.functions = obsidianFreeFunctions;
window.obsidian = obsidianFreeFunctions;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants