mirror of
https://github.com/obsidianmd/obsidian-sample-plugin.git
synced 2026-12-03 06:44:36 +00:00
119 lines
6.4 KiB
Markdown
119 lines
6.4 KiB
Markdown
# Obsidian Sample Plugin
|
|
|
|
This is a sample plugin for Obsidian (https://obsidian.md).
|
|
|
|
This project uses TypeScript to provide type checking and documentation.
|
|
The repo depends on the latest plugin API (obsidian.d.ts) in TypeScript Definition format, which contains TSDoc comments describing what it does.
|
|
|
|
This sample plugin demonstrates some of the basic functionality the plugin API can do.
|
|
|
|
- Adds a ribbon icon, which shows a Notice when clicked.
|
|
- Adds a command "Open modal (simple)" which opens a Modal.
|
|
- Adds a plugin setting tab to the settings page.
|
|
- Registers a global click event and outputs a Notice on click.
|
|
- Registers a global interval which logs 'setInterval' to the console.
|
|
|
|
## First time developing plugins?
|
|
|
|
Quick starting guide for new plugin devs:
|
|
|
|
- Check if [someone already developed a plugin for what you want](https://obsidian.md/plugins)! There might be an existing plugin similar enough that you can partner up with.
|
|
- Make a copy of this repo as a template with the "Use this template" button (login to GitHub if you don't see it).
|
|
- Clone your repo to a local development folder. For convenience, you can place this folder in your `.obsidian/plugins/your-plugin-name` folder.
|
|
- Install NodeJS, then run `npm i` in the command line under your repo folder.
|
|
- Run `npm run dev` to compile your plugin from `src/main.ts` to `main.js`.
|
|
- Make changes to `src/main.ts` (or create new `.ts` files). Those changes should be automatically compiled into `main.js`.
|
|
- Reload Obsidian to load the new version of your plugin.
|
|
- Enable plugin in settings window.
|
|
- For updates to the Obsidian API run `npm update` in the command line under your repo folder.
|
|
|
|
## Releasing new releases
|
|
|
|
This template includes a GitHub Actions workflow, [`.github/workflows/release.yml`](.github/workflows/release.yml), that builds your plugin and drafts a release whenever you push a tag.
|
|
|
|
- If your new release needs a newer version of Obsidian, update `minAppVersion` in `manifest.json` first.
|
|
- Run `npm version patch`, `npm version minor` or `npm version major`. This bumps the version in `package.json` and `manifest.json`, and adds the new version to `versions.json` so older versions of Obsidian can download a compatible older version of your plugin.
|
|
- `npm version` also commits these changes and tags the commit with the new version, such as `1.0.1`, so you don't need to create the tag yourself. The tag has no `v` prefix, because `.npmrc` sets `tag-version-prefix=""`.
|
|
- Push the commit and the tag, for example `git push` and then `git push origin 1.0.1`.
|
|
- The workflow then builds `main.js`, creates a [build provenance attestation](https://docs.github.com/en/actions/security-for-github-actions/using-artifact-attestations/using-artifact-attestations-to-establish-provenance-for-builds) for `main.js` and `styles.css` (if present), and creates a draft GitHub release with `manifest.json`, `main.js` and `styles.css` attached.
|
|
- Open the draft on your repo's Releases page, add release notes, and publish it.
|
|
|
|
Users can check that a release file was built by your workflow from your repository with `gh attestation verify main.js --repo <owner>/<repo>`. Artifact attestations are available for public repositories.
|
|
|
|
If you do not use GitHub Actions, you can release by hand instead:
|
|
|
|
- Update the version in `manifest.json` and add it to `versions.json` (or run `npm version` as above).
|
|
- Run `npm run build`.
|
|
- Create a new GitHub release using your new version number as the "Tag version". Use the exact version number, don't include a prefix `v`. See here for an example: https://github.com/obsidianmd/obsidian-sample-plugin/releases
|
|
- Upload the files `manifest.json`, `main.js`, `styles.css` as binary attachments. Note: The manifest.json file must be in two places, first the root path of your repository and also in the release.
|
|
- Publish the release.
|
|
|
|
## Adding your plugin to the community plugin list
|
|
|
|
- Check the [plugin guidelines](https://docs.obsidian.md/Plugins/Releasing/Plugin+guidelines).
|
|
- Publish an initial version.
|
|
- Make sure you have a `README.md` file in the root of your repo.
|
|
- Make a pull request at https://github.com/obsidianmd/obsidian-releases to add your plugin.
|
|
|
|
## How to use
|
|
|
|
- Clone this repo.
|
|
- Make sure your NodeJS is at least v18 (`node --version`).
|
|
- `npm i` to install dependencies.
|
|
- `npm run dev` to start compilation in watch mode.
|
|
|
|
## Manually installing the plugin
|
|
|
|
- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`.
|
|
|
|
## Improve code quality with eslint
|
|
|
|
- [ESLint](https://eslint.org/) is a tool that analyzes your code to quickly find problems. You can run ESLint against your plugin to find common bugs and ways to improve your code.
|
|
- This project already has eslint preconfigured, you can invoke a check by running`npm run lint`
|
|
- Together with a custom eslint [plugin](https://github.com/obsidianmd/eslint-plugin) for Obsidan specific code guidelines.
|
|
- A GitHub action is preconfigured to automatically lint every commit on all branches.
|
|
|
|
## Document your code with TSDoc
|
|
|
|
- The sample code documents its classes, functions and settings with [TSDoc](https://tsdoc.org/) comments (`/** ... */`).
|
|
- Your editor shows these comments when you hover over a name or pick it from autocomplete, the same way it shows the documentation in `obsidian.d.ts`.
|
|
- You can document your own code the same way if you like. It is optional, and most helpful where a name alone does not explain what something does.
|
|
|
|
## Funding URL
|
|
|
|
You can include funding URLs where people who use your plugin can financially support it.
|
|
|
|
The simple way is to set the `fundingUrl` field to your link in your `manifest.json` file:
|
|
|
|
```json
|
|
{
|
|
"fundingUrl": "https://buymeacoffee.com"
|
|
}
|
|
```
|
|
|
|
If you have multiple URLs, you can also do:
|
|
|
|
```json
|
|
{
|
|
"fundingUrl": {
|
|
"Buy Me a Coffee": "https://buymeacoffee.com",
|
|
"GitHub Sponsor": "https://github.com/sponsors",
|
|
"Patreon": "https://www.patreon.com/"
|
|
}
|
|
}
|
|
```
|
|
|
|
## API Documentation
|
|
|
|
See https://docs.obsidian.md
|
|
|
|
## Contributing to this sample plugin
|
|
|
|
Only collaborators can open pull requests in this repository. To propose a change:
|
|
|
|
1. Fork the repository and commit the change on a branch in your fork.
|
|
2. [Open an issue](https://github.com/obsidianmd/obsidian-sample-plugin/issues/new) that explains the change and why it is needed, and links your branch or commit.
|
|
3. A maintainer reviews the proposal and, if it is accepted, lands the change.
|
|
|
|
If you created your plugin from this template, remove this section from your README.
|