|
1 | | -# Obsidian Sample Plugin |
| 1 | +# Obsidian JS Engine Plugin |
2 | 2 |
|
3 | | -This is a sample plugin for Obsidian (https://obsidian.md). |
| 3 | +This plugin for Obsidian allows you to run JavaScript from within your notes using special code blocks. |
4 | 4 |
|
5 | | -This project uses Typescript to provide type checking and documentation. |
6 | | -The repo depends on the latest plugin API (obsidian.d.ts) in Typescript Definition format, which contains TSDoc comments describing what it does. |
| 5 | +## Usage |
7 | 6 |
|
8 | | -**Note:** The Obsidian API is still in early alpha and is subject to change at any time! |
| 7 | +Start by creating a code block with the `js-engine` plugin. Inside the code block you can write any JS code and at the end return a value. |
| 8 | +The plugin will then render the returned value instead of the code block. When you return nothing, the plugin will not render anything and the code block will be invisible. |
9 | 9 |
|
10 | | -This sample plugin demonstrates some of the basic functionality the plugin API can do. |
| 10 | +## API Docs |
11 | 11 |
|
12 | | -- Adds a ribbon icon, which shows a Notice when clicked. |
13 | | -- Adds a command "Open Sample Modal" which opens a Modal. |
14 | | -- Adds a plugin setting tab to the settings page. |
15 | | -- Registers a global click event and output 'click' to the console. |
16 | | -- Registers a global interval which logs 'setInterval' to the console. |
| 12 | +The following variables are available in the code blocks. |
17 | 13 |
|
18 | | -## First time developing plugins? |
| 14 | +| Name | Type | |
| 15 | +|-----------|-------------------------------------------------| |
| 16 | +| app | `App` (Obsidian) | |
| 17 | +| engine | `API` (this plugin) | |
| 18 | +| context | `ExecutionContext` (this plugin) or `undefined` | |
| 19 | +| component | `Component` (Obsidian) | |
| 20 | +| container | `HTMLElement` | |
19 | 21 |
|
20 | | -Quick starting guide for new plugin devs: |
| 22 | +Documentation for the API and types that are available inside the code block can be found [here](https://mprojectscode.github.io/obsidian-js-engine-plugin/classes/API.API.html). |
21 | 23 |
|
22 | | -- 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. |
23 | | -- Make a copy of this repo as a template with the "Use this template" button (login to GitHub if you don't see it). |
24 | | -- Clone your repo to a local development folder. For convenience, you can place this folder in your `.obsidian/plugins/your-plugin-name` folder. |
25 | | -- Install NodeJS, then run `npm i` in the command line under your repo folder. |
26 | | -- Run `npm run dev` to compile your plugin from `main.ts` to `main.js`. |
27 | | -- Make changes to `main.ts` (or create new `.ts` files). Those changes should be automatically compiled into `main.js`. |
28 | | -- Reload Obsidian to load the new version of your plugin. |
29 | | -- Enable plugin in settings window. |
30 | | -- For updates to the Obsidian API run `npm update` in the command line under your repo folder. |
| 24 | +## Examples |
31 | 25 |
|
32 | | -## Releasing new releases |
| 26 | +### Markdown Builder |
33 | 27 |
|
34 | | -- Update your `manifest.json` with your new version number, such as `1.0.1`, and the minimum Obsidian version required for your latest release. |
35 | | -- Update your `versions.json` file with `"new-plugin-version": "minimum-obsidian-version"` so older versions of Obsidian can download an older version of your plugin that's compatible. |
36 | | -- Create 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 |
37 | | -- 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. |
38 | | -- Publish the release. |
| 28 | +```js |
| 29 | +let markdownBuilder = engine.markdown.createBuilder() |
39 | 30 |
|
40 | | -> You can simplify the version bump process by running `npm version patch`, `npm version minor` or `npm version major` after updating `minAppVersion` manually in `manifest.json`. |
41 | | -> The command will bump version in `manifest.json` and `package.json`, and add the entry for the new version to `versions.json` |
| 31 | +markdownBuilder.createHeading(2, "Test Heading") |
| 32 | +markdownBuilder.createParagraph("This is a test paragraph.") |
42 | 33 |
|
43 | | -## Adding your plugin to the community plugin list |
| 34 | +markdownBuilder.createHeading(3, "This is a sub heading") |
| 35 | +markdownBuilder.createHeading(4, "This is a sub sub heading") |
| 36 | +markdownBuilder.createParagraph("This is another test paragraph.") |
| 37 | +``` |
44 | 38 |
|
45 | | -- Check https://github.com/obsidianmd/obsidian-releases/blob/master/plugin-review.md |
46 | | -- Publish an initial version. |
47 | | -- Make sure you have a `README.md` file in the root of your repo. |
48 | | -- Make a pull request at https://github.com/obsidianmd/obsidian-releases to add your plugin. |
| 39 | +#### Output |
49 | 40 |
|
50 | | -## How to use |
| 41 | +> ## Test Heading |
| 42 | +> This is a test paragraph. |
| 43 | +> ### This is a sub heading |
| 44 | +> #### This is a sub sub heading |
| 45 | +> This is another test paragraph. |
51 | 46 |
|
52 | | -- Clone this repo. |
53 | | -- `npm i` or `yarn` to install dependencies |
54 | | -- `npm run dev` to start compilation in watch mode. |
| 47 | +### Rendering Strings as Markdown |
55 | 48 |
|
56 | | -## Manually installing the plugin |
| 49 | +```js |
| 50 | +let str = "*test*"; |
| 51 | +return str; |
| 52 | +``` |
57 | 53 |
|
58 | | -- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`. |
| 54 | +```js |
| 55 | +let str = "*test*"; |
| 56 | +return engine.markdown.create(str); |
| 57 | +``` |
59 | 58 |
|
60 | | -## Improve code quality with eslint (optional) |
| 59 | +The top example renders the string as plain text and the second one renders the text as markdown. |
61 | 60 |
|
62 | | -- [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. |
63 | | -- To use eslint with this project, make sure to install eslint from terminal: |
64 | | - - `npm install -g eslint` |
65 | | -- To use eslint to analyze this project use this command: |
66 | | - - `eslint main.ts` |
67 | | - - eslint will then create a report with suggestions for code improvement by file and line number. |
68 | | -- If your source code is in a folder, such as `src`, you can use eslint with this command to analyze all files in that folder: |
69 | | - - `eslint .\src\` |
| 61 | +#### Output |
70 | 62 |
|
71 | | -## Funding URL |
| 63 | +> \*test\* |
72 | 64 |
|
73 | | -You can include funding URLs where people who use your plugin can financially support it. |
| 65 | +> *test* |
74 | 66 |
|
75 | | -The simple way is to set the `fundingUrl` field to your link in your `manifest.json` file: |
| 67 | +### Importing JS |
76 | 68 |
|
77 | | -```json |
78 | | -{ |
79 | | - "fundingUrl": "https://buymeacoffee.com" |
80 | | -} |
| 69 | +```js |
| 70 | +let lib = await engine.importJs("lib.js"); |
| 71 | +return lib.getGreeting(); |
81 | 72 | ``` |
82 | 73 |
|
83 | | -If you have multiple URLs, you can also do: |
| 74 | +With a file named `lib.js` in the root of the vault. |
84 | 75 |
|
85 | | -```json |
86 | | -{ |
87 | | - "fundingUrl": { |
88 | | - "Buy Me a Coffee": "https://buymeacoffee.com", |
89 | | - "GitHub Sponsor": "https://github.com/sponsors", |
90 | | - "Patreon": "https://www.patreon.com/" |
91 | | - } |
| 76 | +```js |
| 77 | +export function getGreeting() { |
| 78 | + return "Hello!"; |
92 | 79 | } |
93 | 80 | ``` |
94 | 81 |
|
95 | | -## API Documentation |
| 82 | +#### Output |
| 83 | + |
| 84 | +> Hello! |
| 85 | +
|
96 | 86 |
|
97 | | -See https://github.com/obsidianmd/obsidian-api |
0 commit comments