Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit 3de67b5

Browse files
Initial commit
0 parents  commit 3de67b5

14 files changed

+403
-0
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# top-most EditorConfig file
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
end_of_line = lf
7+
insert_final_newline = true
8+
indent_style = tab
9+
indent_size = 4
10+
tab_width = 4

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
npm node_modules
2+
build

.eslintrc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"root": true,
3+
"parser": "@typescript-eslint/parser",
4+
"env": { "node": true },
5+
"plugins": [
6+
"@typescript-eslint"
7+
],
8+
"extends": [
9+
"eslint:recommended",
10+
"plugin:@typescript-eslint/eslint-recommended",
11+
"plugin:@typescript-eslint/recommended"
12+
],
13+
"parserOptions": {
14+
"sourceType": "module"
15+
},
16+
"rules": {
17+
"no-unused-vars": "off",
18+
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
19+
"@typescript-eslint/ban-ts-comment": "off",
20+
"no-prototype-builtins": "off",
21+
"@typescript-eslint/no-empty-function": "off"
22+
}
23+
}

.gitignore

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# vscode
2+
.vscode
3+
4+
# Intellij
5+
*.iml
6+
.idea
7+
8+
# npm
9+
node_modules
10+
11+
# Don't include the compiled main.js file in the repo.
12+
# They should be uploaded to GitHub releases instead.
13+
main.js
14+
15+
# Exclude sourcemaps
16+
*.map
17+
18+
# obsidian
19+
data.json
20+
21+
# Exclude macOS Finder (System Explorer) View States
22+
.DS_Store

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tag-version-prefix=""

README.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Obsidian Sample Plugin
2+
3+
This is a sample plugin for Obsidian (https://obsidian.md).
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.
7+
8+
**Note:** The Obsidian API is still in early alpha and is subject to change at any time!
9+
10+
This sample plugin demonstrates some of the basic functionality the plugin API can do.
11+
- Changes the default font color to red using `styles.css`.
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.
17+
18+
## First time developing plugins?
19+
20+
Quick starting guide for new plugin devs:
21+
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.
31+
32+
## Releasing new releases
33+
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.
39+
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`
42+
43+
## Adding your plugin to the community plugin list
44+
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.
49+
50+
## How to use
51+
52+
- Clone this repo.
53+
- `npm i` or `yarn` to install dependencies
54+
- `npm run dev` to start compilation in watch mode.
55+
56+
## Manually installing the plugin
57+
58+
- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`.
59+
60+
## Improve code quality with eslint (optional)
61+
- [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.
62+
- To use eslint with this project, make sure to install eslint from terminal:
63+
- `npm install -g eslint`
64+
- To use eslint to analyze this project use this command:
65+
- `eslint main.ts`
66+
- eslint will then create a report with suggestions for code improvement by file and line number.
67+
- 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:
68+
- `eslint .\src\`
69+
70+
71+
## API Documentation
72+
73+
See https://github.com/obsidianmd/obsidian-api

esbuild.config.mjs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import esbuild from "esbuild";
2+
import process from "process";
3+
import builtins from 'builtin-modules'
4+
5+
const banner =
6+
`/*
7+
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
8+
if you want to view the source, please visit the github repository of this plugin
9+
*/
10+
`;
11+
12+
const prod = (process.argv[2] === 'production');
13+
14+
esbuild.build({
15+
banner: {
16+
js: banner,
17+
},
18+
entryPoints: ['main.ts'],
19+
bundle: true,
20+
external: [
21+
'obsidian',
22+
'electron',
23+
'@codemirror/autocomplete',
24+
'@codemirror/closebrackets',
25+
'@codemirror/collab',
26+
'@codemirror/commands',
27+
'@codemirror/comment',
28+
'@codemirror/fold',
29+
'@codemirror/gutter',
30+
'@codemirror/highlight',
31+
'@codemirror/history',
32+
'@codemirror/language',
33+
'@codemirror/lint',
34+
'@codemirror/matchbrackets',
35+
'@codemirror/panel',
36+
'@codemirror/rangeset',
37+
'@codemirror/rectangular-selection',
38+
'@codemirror/search',
39+
'@codemirror/state',
40+
'@codemirror/stream-parser',
41+
'@codemirror/text',
42+
'@codemirror/tooltip',
43+
'@codemirror/view',
44+
'@lezer/common',
45+
'@lezer/highlight',
46+
'@lezer/lr',
47+
...builtins],
48+
format: 'cjs',
49+
watch: !prod,
50+
target: 'es2016',
51+
logLevel: "info",
52+
sourcemap: prod ? false : 'inline',
53+
treeShaking: true,
54+
outfile: 'main.js',
55+
}).catch(() => process.exit(1));

main.ts

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
2+
3+
// Remember to rename these classes and interfaces!
4+
5+
interface MyPluginSettings {
6+
mySetting: string;
7+
}
8+
9+
const DEFAULT_SETTINGS: MyPluginSettings = {
10+
mySetting: 'default'
11+
}
12+
13+
export default class MyPlugin extends Plugin {
14+
settings: MyPluginSettings;
15+
16+
async onload() {
17+
await this.loadSettings();
18+
19+
// This creates an icon in the left ribbon.
20+
const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
21+
// Called when the user clicks the icon.
22+
new Notice('This is a notice!');
23+
});
24+
// Perform additional things with the ribbon
25+
ribbonIconEl.addClass('my-plugin-ribbon-class');
26+
27+
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
28+
const statusBarItemEl = this.addStatusBarItem();
29+
statusBarItemEl.setText('Status Bar Text');
30+
31+
// This adds a simple command that can be triggered anywhere
32+
this.addCommand({
33+
id: 'open-sample-modal-simple',
34+
name: 'Open sample modal (simple)',
35+
callback: () => {
36+
new SampleModal(this.app).open();
37+
}
38+
});
39+
// This adds an editor command that can perform some operation on the current editor instance
40+
this.addCommand({
41+
id: 'sample-editor-command',
42+
name: 'Sample editor command',
43+
editorCallback: (editor: Editor, view: MarkdownView) => {
44+
console.log(editor.getSelection());
45+
editor.replaceSelection('Sample Editor Command');
46+
}
47+
});
48+
// This adds a complex command that can check whether the current state of the app allows execution of the command
49+
this.addCommand({
50+
id: 'open-sample-modal-complex',
51+
name: 'Open sample modal (complex)',
52+
checkCallback: (checking: boolean) => {
53+
// Conditions to check
54+
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
55+
if (markdownView) {
56+
// If checking is true, we're simply "checking" if the command can be run.
57+
// If checking is false, then we want to actually perform the operation.
58+
if (!checking) {
59+
new SampleModal(this.app).open();
60+
}
61+
62+
// This command will only show up in Command Palette when the check function returns true
63+
return true;
64+
}
65+
}
66+
});
67+
68+
// This adds a settings tab so the user can configure various aspects of the plugin
69+
this.addSettingTab(new SampleSettingTab(this.app, this));
70+
71+
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
72+
// Using this function will automatically remove the event listener when this plugin is disabled.
73+
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
74+
console.log('click', evt);
75+
});
76+
77+
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
78+
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
79+
}
80+
81+
onunload() {
82+
83+
}
84+
85+
async loadSettings() {
86+
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
87+
}
88+
89+
async saveSettings() {
90+
await this.saveData(this.settings);
91+
}
92+
}
93+
94+
class SampleModal extends Modal {
95+
constructor(app: App) {
96+
super(app);
97+
}
98+
99+
onOpen() {
100+
const {contentEl} = this;
101+
contentEl.setText('Woah!');
102+
}
103+
104+
onClose() {
105+
const {contentEl} = this;
106+
contentEl.empty();
107+
}
108+
}
109+
110+
class SampleSettingTab extends PluginSettingTab {
111+
plugin: MyPlugin;
112+
113+
constructor(app: App, plugin: MyPlugin) {
114+
super(app, plugin);
115+
this.plugin = plugin;
116+
}
117+
118+
display(): void {
119+
const {containerEl} = this;
120+
121+
containerEl.empty();
122+
123+
containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'});
124+
125+
new Setting(containerEl)
126+
.setName('Setting #1')
127+
.setDesc('It\'s a secret')
128+
.addText(text => text
129+
.setPlaceholder('Enter your secret')
130+
.setValue(this.plugin.settings.mySetting)
131+
.onChange(async (value) => {
132+
console.log('Secret: ' + value);
133+
this.plugin.settings.mySetting = value;
134+
await this.plugin.saveSettings();
135+
}));
136+
}
137+
}

manifest.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": "obsidian-sample-plugin",
3+
"name": "Sample Plugin",
4+
"version": "1.0.1",
5+
"minAppVersion": "0.12.0",
6+
"description": "This is a sample plugin for Obsidian. This plugin demonstrates some of the capabilities of the Obsidian API.",
7+
"author": "Obsidian",
8+
"authorUrl": "https://obsidian.md",
9+
"isDesktopOnly": false
10+
}

package.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "obsidian-sample-plugin",
3+
"version": "1.0.1",
4+
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
5+
"main": "main.js",
6+
"scripts": {
7+
"dev": "node esbuild.config.mjs",
8+
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
9+
"version": "node version-bump.mjs && git add manifest.json versions.json"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "MIT",
14+
"devDependencies": {
15+
"@types/node": "^16.11.6",
16+
"@typescript-eslint/eslint-plugin": "5.29.0",
17+
"@typescript-eslint/parser": "5.29.0",
18+
"builtin-modules": "3.3.0",
19+
"esbuild": "0.14.47",
20+
"obsidian": "latest",
21+
"tslib": "2.4.0",
22+
"typescript": "4.7.4"
23+
}
24+
}

styles.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Sets all the text color to red! */
2+
body {
3+
color: red;
4+
}

tsconfig.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"inlineSourceMap": true,
5+
"inlineSources": true,
6+
"module": "ESNext",
7+
"target": "ES6",
8+
"allowJs": true,
9+
"noImplicitAny": true,
10+
"moduleResolution": "node",
11+
"importHelpers": true,
12+
"isolatedModules": true,
13+
"strictNullChecks": true,
14+
"lib": [
15+
"DOM",
16+
"ES5",
17+
"ES6",
18+
"ES7"
19+
]
20+
},
21+
"include": [
22+
"**/*.ts"
23+
]
24+
}

0 commit comments

Comments
 (0)