From 9bb372bc7f23d8b0e3cf0e2f3bb4c6ef26e63b34 Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Wed, 2 Dec 2026 06:27:51 +0000 Subject: [PATCH] docs: document the sample code with TSDoc comments (#195) * docs: document the sample code with TSDoc comments Add TSDoc comments to the sample plugin's classes, methods and settings, and a README section on writing them, so plugins built from the template start with documented code that editors show on hover. Closes #100 * docs: make TSDoc comments an optional suggestion --- README.md | 6 ++++++ src/main.ts | 20 ++++++++++++++++++++ src/settings.ts | 5 +++++ 3 files changed, 31 insertions(+) diff --git a/README.md b/README.md index 3d0c1e3..eb97243 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,12 @@ Quick starting guide for new plugin devs: - 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. diff --git a/src/main.ts b/src/main.ts index db213e8..ab21328 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,9 +14,19 @@ import { // Remember to rename these classes and interfaces! +/** + * The plugin's entry point. Obsidian creates one instance of this class + * when the plugin is enabled and calls {@link MyPlugin.onload}. + */ export default class MyPlugin extends Plugin { + /** The current settings, loaded in {@link MyPlugin.onload}. */ settings!: MyPluginSettings; + /** + * Called when the plugin is enabled. Register commands, views and + * event handlers here, so Obsidian can remove them when the plugin + * is disabled. + */ async onload() { await this.loadSettings(); @@ -86,8 +96,16 @@ export default class MyPlugin extends Plugin { ); } + /** + * Called when the plugin is disabled. Anything registered with the + * `register*` helpers is cleaned up automatically. + */ onunload() {} + /** + * Loads the saved settings, filling in a default for every value that + * has not been saved yet. + */ async loadSettings() { this.settings = Object.assign( {}, @@ -96,11 +114,13 @@ export default class MyPlugin extends Plugin { ); } + /** Saves the current settings to the plugin's `data.json`. */ async saveSettings() { await this.saveData(this.settings); } } +/** A modal that shows a short message, opened by the sample commands. */ class SampleModal extends Modal { onOpen() { const { contentEl } = this; diff --git a/src/settings.ts b/src/settings.ts index 9eae902..1f26145 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -1,14 +1,18 @@ import { App, PluginSettingTab, Setting } from 'obsidian'; import MyPlugin from './main'; +/** The settings this plugin saves. */ export interface MyPluginSettings { + /** An example text setting. */ mySetting: string; } +/** The value of each setting until the user changes it. */ export const DEFAULT_SETTINGS: MyPluginSettings = { mySetting: 'default', }; +/** The plugin's tab in the Obsidian settings window. */ export class SampleSettingTab extends PluginSettingTab { plugin: MyPlugin; @@ -17,6 +21,7 @@ export class SampleSettingTab extends PluginSettingTab { this.plugin = plugin; } + /** Builds the tab's content each time the user opens it. */ display(): void { const { containerEl } = this;