GithubHelp home page GithubHelp logo

johannrichard / obsidian-user-plugins Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mnowotnik/obsidian-user-plugins

0.0 0.0 0.0 73 KB

Allows user scripts to use plugin API

License: MIT License

JavaScript 7.01% TypeScript 92.54% CSS 0.45%

obsidian-user-plugins's Introduction

User Plugins for Obsidian

User Plugins is a simple plugin that lets you use Obsidian plugin API in your snippets or javascript files to modify the behaviour of Obsidian just as if you created a plugin, but without the hassle.

Stop and read

โš ๏ธ This plugin is for advanced users only: DO NOT use code in scripts you do not fully understand. It can delete your notes or worse. See legal notice.

Use cases

  • adding custom commands
  • testing an idea for a plugin
  • using plugin API to do anything you want

Motivating example

Add command Create new note in given folder that allows you to choose a folder before creating a note.

module.exports = {}

module.exports.onload = async (plugin) => {
  const MarkdownView = plugin.passedModules.obsidian.MarkdownView
  plugin.addCommand({
    id: 'new-note-in-folder',
    name: 'Create new note in a folder',
    callback: async () => {
      const folders = plugin.app.vault.getAllLoadedFiles().filter(i => i.children).map(folder => folder.path);
      const folder = await plugin.helpers.suggest(folders, folders);
      const created_note = await plugin.app.vault.create(folder + "/Untitled.md", "")
      const active_leaf = plugin.app.workspace.activeLeaf;
      if (!active_leaf) {
        return;
      }
      await active_leaf.openFile(created_note, {
        state: { mode: "source" },
      });
      plugin.app.workspace.trigger("create",created_note)
      const view = app.workspace.getActiveViewOfType(MarkdownView);
      if (view) {
        const editor = view.editor;
        editor.focus()
      }
    }
  });
}

Command in palette

Quick start

Installation

This plugin is not yet available in the Community Plugins panel.

You can easily add this plugin from Community Plugins panel. Alternatively, here's a manual way:

git clone this repo to <YOUR VAULT>/.obsidian/plugins folder and then execute:

npm install
npm run build

Usage

Scripts can be added either by manually adding snippets or enabling each individual file in a scripts directory in a vault. Scripts have access to a Plugin object. Its API is declared here. plugin has two additional members:

  • helpers

    Currently it has a single method that opens a suggester modal:

    suggest<T>(
        textItems: string[] | ((item: T) => string),
        items: T[],
        placeholder?: string,
        limit?: number
    )
  • passedModules

    Currently only gives access to the obsidian module

Snippet

A snippet is just a javascript block of code that has access to a plugin variable. It is executed in the onload method of the plugin. Example:

plugin.addCommand({
    name: 'FooBar',
    callback: () => {
        console.log('foobar');
    }
});

Script file

A script file follows CommonJS module specification and exports at least onload function that takes a single argument plugin as an input. You must specify onload function in th exported module and you can also specify onunload if needed.

To use scripts specify scripts folder in settings, hit the reload button to search for scripts in the specified path and then enable each found script using a toggle button.

Example:

module.exports = {}
module.exports.onload = function(plugin) {
    plugin.addCommand({
        name: 'FooBar',
        callback: () => {
            console.log('foobar');
        }
    });
}
module.exports.onunload = function(plugin) {
    console.log('unload')
}

Notice

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

obsidian-user-plugins's People

Contributors

mnowotnik avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.