diff --git a/.editorconfig b/.editorconfig index 84b8a66..bc3bb7b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,10 +1,10 @@ -# top-most EditorConfig file -root = true - -[*] -charset = utf-8 -end_of_line = lf -insert_final_newline = true -indent_style = tab -indent_size = 4 -tab_width = 4 +# top-most EditorConfig file +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_style = space +indent_size = 2 +tab_width = 2 diff --git a/.eslintrc b/.eslintrc index 0807290..00ae081 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,23 +1,21 @@ { - "root": true, - "parser": "@typescript-eslint/parser", - "env": { "node": true }, - "plugins": [ - "@typescript-eslint" - ], - "extends": [ - "eslint:recommended", - "plugin:@typescript-eslint/eslint-recommended", - "plugin:@typescript-eslint/recommended" - ], - "parserOptions": { - "sourceType": "module" - }, - "rules": { - "no-unused-vars": "off", - "@typescript-eslint/no-unused-vars": ["error", { "args": "none" }], - "@typescript-eslint/ban-ts-comment": "off", - "no-prototype-builtins": "off", - "@typescript-eslint/no-empty-function": "off" - } - } \ No newline at end of file + "root": true, + "parser": "@typescript-eslint/parser", + "env": { "node": true }, + "plugins": ["@typescript-eslint"], + "extends": [ + "eslint:recommended", + "plugin:@typescript-eslint/eslint-recommended", + "plugin:@typescript-eslint/recommended" + ], + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "no-unused-vars": "off", + "@typescript-eslint/no-unused-vars": ["error", { "args": "none" }], + "@typescript-eslint/ban-ts-comment": "off", + "no-prototype-builtins": "off", + "@typescript-eslint/no-empty-function": "off" + } +} diff --git a/esbuild.config.mjs b/esbuild.config.mjs index b33ec53..a2b6956 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -1,42 +1,44 @@ -import esbuild from "esbuild"; -import process from "process"; +import esbuild from 'esbuild' +import process from 'process' import builtins from 'builtin-modules' -const banner = -`/* +const banner = `/* THIS IS A GENERATED/BUNDLED FILE BY ESBUILD if you want to view the source, please visit the github repository of this plugin */ -`; +` -const prod = (process.argv[2] === 'production'); +const prod = process.argv[2] === 'production' -esbuild.build({ - banner: { - js: banner, - }, - entryPoints: ['src/main.ts'], - bundle: true, - external: [ - 'obsidian', - 'electron', - '@codemirror/autocomplete', - '@codemirror/collab', - '@codemirror/commands', - '@codemirror/language', - '@codemirror/lint', - '@codemirror/search', - '@codemirror/state', - '@codemirror/view', - '@lezer/common', - '@lezer/highlight', - '@lezer/lr', - ...builtins], - format: 'cjs', - watch: !prod, - target: 'es2018', - logLevel: "info", - sourcemap: prod ? false : 'inline', - treeShaking: true, - outfile: 'main.js', -}).catch(() => process.exit(1)); +esbuild + .build({ + banner: { + js: banner, + }, + entryPoints: ['src/main.ts'], + bundle: true, + external: [ + 'obsidian', + 'electron', + '@codemirror/autocomplete', + '@codemirror/collab', + '@codemirror/commands', + '@codemirror/language', + '@codemirror/lint', + '@codemirror/search', + '@codemirror/state', + '@codemirror/view', + '@lezer/common', + '@lezer/highlight', + '@lezer/lr', + ...builtins, + ], + format: 'cjs', + watch: !prod, + target: 'es2018', + logLevel: 'info', + sourcemap: prod ? false : 'inline', + treeShaking: true, + outfile: 'main.js', + }) + .catch(() => process.exit(1)) diff --git a/manifest.json b/manifest.json index 0897aff..3553c72 100644 --- a/manifest.json +++ b/manifest.json @@ -1,11 +1,11 @@ { - "id": "obsidian-sample-plugin", - "name": "Sample Plugin", - "version": "1.0.0", - "minAppVersion": "0.15.0", - "description": "This is a sample plugin for Obsidian. This plugin demonstrates some of the capabilities of the Obsidian API.", - "author": "Obsidian", - "authorUrl": "https://obsidian.md", - "fundingUrl": "https://obsidian.md/pricing", - "isDesktopOnly": false + "id": "obsidian-sample-plugin", + "name": "Sample Plugin", + "version": "1.0.0", + "minAppVersion": "0.15.0", + "description": "This is a sample plugin for Obsidian. This plugin demonstrates some of the capabilities of the Obsidian API.", + "author": "Obsidian", + "authorUrl": "https://obsidian.md", + "fundingUrl": "https://obsidian.md/pricing", + "isDesktopOnly": false } diff --git a/package.json b/package.json index c3c697b..8f3a3cd 100644 --- a/package.json +++ b/package.json @@ -1,27 +1,27 @@ { - "name": "obsidian-sample-plugin", - "version": "1.0.0", - "description": "This is a sample plugin for Obsidian (https://obsidian.md)", - "main": "main.js", - "scripts": { - "dev": "node esbuild.config.mjs", - "build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", - "version": "node version-bump.mjs && git add manifest.json versions.json", - "format:check": "prettier --check .", - "format:fix": "prettier --write ." - }, - "keywords": [], - "author": "", - "license": "MIT", - "devDependencies": { - "@types/node": "^16.11.6", - "@typescript-eslint/eslint-plugin": "5.29.0", - "@typescript-eslint/parser": "5.29.0", - "builtin-modules": "3.3.0", - "esbuild": "0.14.47", - "obsidian": "latest", - "prettier": "2.8.3", - "tslib": "2.4.0", - "typescript": "4.7.4" - } + "name": "obsidian-sample-plugin", + "version": "1.0.0", + "description": "This is a sample plugin for Obsidian (https://obsidian.md)", + "main": "main.js", + "scripts": { + "dev": "node esbuild.config.mjs", + "build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", + "version": "node version-bump.mjs && git add manifest.json versions.json", + "format:check": "prettier --check .", + "format:fix": "prettier --write ." + }, + "keywords": [], + "author": "", + "license": "MIT", + "devDependencies": { + "@types/node": "^16.11.6", + "@typescript-eslint/eslint-plugin": "5.29.0", + "@typescript-eslint/parser": "5.29.0", + "builtin-modules": "3.3.0", + "esbuild": "0.14.47", + "obsidian": "latest", + "prettier": "2.8.3", + "tslib": "2.4.0", + "typescript": "4.7.4" + } } diff --git a/src/main.ts b/src/main.ts index 50b75f3..1a77dcd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,137 +1,153 @@ -import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; +import { + App, + Editor, + MarkdownView, + Modal, + Notice, + Plugin, + PluginSettingTab, + Setting, +} from 'obsidian' // Remember to rename these classes and interfaces! interface MyPluginSettings { - mySetting: string; + mySetting: string } const DEFAULT_SETTINGS: MyPluginSettings = { - mySetting: 'default' + mySetting: 'default', } export default class MyPlugin extends Plugin { - settings: MyPluginSettings; + settings: MyPluginSettings - async onload() { - await this.loadSettings(); + async onload() { + await this.loadSettings() - // This creates an icon in the left ribbon. - const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => { - // Called when the user clicks the icon. - new Notice('This is a notice!'); - }); - // Perform additional things with the ribbon - ribbonIconEl.addClass('my-plugin-ribbon-class'); + // This creates an icon in the left ribbon. + const ribbonIconEl = this.addRibbonIcon( + 'dice', + 'Sample Plugin', + (evt: MouseEvent) => { + // Called when the user clicks the icon. + new Notice('This is a notice!') + } + ) + // Perform additional things with the ribbon + ribbonIconEl.addClass('my-plugin-ribbon-class') - // This adds a status bar item to the bottom of the app. Does not work on mobile apps. - const statusBarItemEl = this.addStatusBarItem(); - statusBarItemEl.setText('Status Bar Text'); + // This adds a status bar item to the bottom of the app. Does not work on mobile apps. + const statusBarItemEl = this.addStatusBarItem() + statusBarItemEl.setText('Status Bar Text') - // This adds a simple command that can be triggered anywhere - this.addCommand({ - id: 'open-sample-modal-simple', - name: 'Open sample modal (simple)', - callback: () => { - new SampleModal(this.app).open(); - } - }); - // This adds an editor command that can perform some operation on the current editor instance - this.addCommand({ - id: 'sample-editor-command', - name: 'Sample editor command', - editorCallback: (editor: Editor, view: MarkdownView) => { - console.log(editor.getSelection()); - editor.replaceSelection('Sample Editor Command'); - } - }); - // This adds a complex command that can check whether the current state of the app allows execution of the command - this.addCommand({ - id: 'open-sample-modal-complex', - name: 'Open sample modal (complex)', - checkCallback: (checking: boolean) => { - // Conditions to check - const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView); - if (markdownView) { - // If checking is true, we're simply "checking" if the command can be run. - // If checking is false, then we want to actually perform the operation. - if (!checking) { - new SampleModal(this.app).open(); - } + // This adds a simple command that can be triggered anywhere + this.addCommand({ + id: 'open-sample-modal-simple', + name: 'Open sample modal (simple)', + callback: () => { + new SampleModal(this.app).open() + }, + }) + // This adds an editor command that can perform some operation on the current editor instance + this.addCommand({ + id: 'sample-editor-command', + name: 'Sample editor command', + editorCallback: (editor: Editor, view: MarkdownView) => { + console.log(editor.getSelection()) + editor.replaceSelection('Sample Editor Command') + }, + }) + // This adds a complex command that can check whether the current state of the app allows execution of the command + this.addCommand({ + id: 'open-sample-modal-complex', + name: 'Open sample modal (complex)', + checkCallback: (checking: boolean) => { + // Conditions to check + const markdownView = + this.app.workspace.getActiveViewOfType(MarkdownView) + if (markdownView) { + // If checking is true, we're simply "checking" if the command can be run. + // If checking is false, then we want to actually perform the operation. + if (!checking) { + new SampleModal(this.app).open() + } - // This command will only show up in Command Palette when the check function returns true - return true; - } - } - }); + // This command will only show up in Command Palette when the check function returns true + return true + } + }, + }) - // This adds a settings tab so the user can configure various aspects of the plugin - this.addSettingTab(new SampleSettingTab(this.app, this)); + // This adds a settings tab so the user can configure various aspects of the plugin + this.addSettingTab(new SampleSettingTab(this.app, this)) - // If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin) - // Using this function will automatically remove the event listener when this plugin is disabled. - this.registerDomEvent(document, 'click', (evt: MouseEvent) => { - console.log('click', evt); - }); + // If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin) + // Using this function will automatically remove the event listener when this plugin is disabled. + this.registerDomEvent(document, 'click', (evt: MouseEvent) => { + console.log('click', evt) + }) - // When registering intervals, this function will automatically clear the interval when the plugin is disabled. - this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000)); - } + // When registering intervals, this function will automatically clear the interval when the plugin is disabled. + this.registerInterval( + window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000) + ) + } - onunload() { + onunload() {} - } + async loadSettings() { + this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()) + } - async loadSettings() { - this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); - } - - async saveSettings() { - await this.saveData(this.settings); - } + async saveSettings() { + await this.saveData(this.settings) + } } class SampleModal extends Modal { - constructor(app: App) { - super(app); - } + constructor(app: App) { + super(app) + } - onOpen() { - const {contentEl} = this; - contentEl.setText('Woah!'); - } + onOpen() { + const { contentEl } = this + contentEl.setText('Woah!') + } - onClose() { - const {contentEl} = this; - contentEl.empty(); - } + onClose() { + const { contentEl } = this + contentEl.empty() + } } class SampleSettingTab extends PluginSettingTab { - plugin: MyPlugin; + plugin: MyPlugin - constructor(app: App, plugin: MyPlugin) { - super(app, plugin); - this.plugin = plugin; - } + constructor(app: App, plugin: MyPlugin) { + super(app, plugin) + this.plugin = plugin + } - display(): void { - const {containerEl} = this; + display(): void { + const { containerEl } = this - containerEl.empty(); + containerEl.empty() - containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'}); + containerEl.createEl('h2', { text: 'Settings for my awesome plugin.' }) - new Setting(containerEl) - .setName('Setting #1') - .setDesc('It\'s a secret') - .addText(text => text - .setPlaceholder('Enter your secret') - .setValue(this.plugin.settings.mySetting) - .onChange(async (value) => { - console.log('Secret: ' + value); - this.plugin.settings.mySetting = value; - await this.plugin.saveSettings(); - })); - } + new Setting(containerEl) + .setName('Setting #1') + .setDesc("It's a secret") + .addText((text) => + text + .setPlaceholder('Enter your secret') + .setValue(this.plugin.settings.mySetting) + .onChange(async (value) => { + console.log('Secret: ' + value) + this.plugin.settings.mySetting = value + await this.plugin.saveSettings() + }) + ) + } } diff --git a/tsconfig.json b/tsconfig.json index 2d6fbdf..a4108b9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,15 +10,8 @@ "moduleResolution": "node", "importHelpers": true, "isolatedModules": true, - "strictNullChecks": true, - "lib": [ - "DOM", - "ES5", - "ES6", - "ES7" - ] + "strictNullChecks": true, + "lib": ["DOM", "ES5", "ES6", "ES7"] }, - "include": [ - "**/*.ts" - ] + "include": ["**/*.ts"] } diff --git a/version-bump.mjs b/version-bump.mjs index d409fa0..9058311 100644 --- a/version-bump.mjs +++ b/version-bump.mjs @@ -1,14 +1,14 @@ -import { readFileSync, writeFileSync } from "fs"; +import { readFileSync, writeFileSync } from 'fs' -const targetVersion = process.env.npm_package_version; +const targetVersion = process.env.npm_package_version // read minAppVersion from manifest.json and bump version to target version -let manifest = JSON.parse(readFileSync("manifest.json", "utf8")); -const { minAppVersion } = manifest; -manifest.version = targetVersion; -writeFileSync("manifest.json", JSON.stringify(manifest, null, "\t")); +let manifest = JSON.parse(readFileSync('manifest.json', 'utf8')) +const { minAppVersion } = manifest +manifest.version = targetVersion +writeFileSync('manifest.json', JSON.stringify(manifest, null, '\t')) // update versions.json with target version and minAppVersion from manifest.json -let versions = JSON.parse(readFileSync("versions.json", "utf8")); -versions[targetVersion] = minAppVersion; -writeFileSync("versions.json", JSON.stringify(versions, null, "\t")); +let versions = JSON.parse(readFileSync('versions.json', 'utf8')) +versions[targetVersion] = minAppVersion +writeFileSync('versions.json', JSON.stringify(versions, null, '\t')) diff --git a/versions.json b/versions.json index 26382a1..af9a39e 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ { - "1.0.0": "0.15.0" + "1.0.0": "0.15.0" }