中文
SiYuan plugin sample
Get started
- Make a copy of this repo as a template with the Use this template button, please note that the repo name must be the same as the plugin name, the default branch must be
main
- Clone your repo to a local development folder. For convenience, you can place this folder in your
{workspace}/data/plugins/ folder
- Install NodeJS and pnpm, then run
pnpm i in the command line under your repo folder
- Execute
pnpm run dev for real-time compilation
- Open SiYuan marketplace and enable plugin in downloaded tab
Development
- i18n/*
- icon.png (optional default icon, 160*160)
- index.css
- index.js
- plugin.json
- preview.png (optional default preview, 1024*768)
- README*.md
- Frontend API
- Backend API
I18n
In terms of internationalization, our main consideration is to support multiple languages. Specifically, we need to
complete the following tasks:
- Meta information about the plugin itself, such as plugin description and readme
displayName, description and readme fields in plugin.json, and the corresponding README*.md file
- Text used in the plugin, such as button text and tooltips
- src/i18n/*.json language configuration files
- Use
this.i18n.key to get the text in the code
It is recommended that the plugin supports at least English and Simplified Chinese, so that more people can use it more conveniently. Unsupported languages do not need to be declared in the displayName, description and readme fields in plugin.json.
plugin.json
A typical example is as follows:
{
"name": "plugin-sample",
"author": "Vanessa",
"url": "https://github.com/siyuan-note/plugin-sample",
"version": "0.5.1",
"minAppVersion": "3.8.4",
"kernels": ["all"],
"backends": ["all"],
"frontends": ["all"],
"settingsWindow": true,
"disabledInPublish": false,
"publish": {
"resources": [],
"data": ["readonlyText"]
},
"displayName": {
"default": "Plugin Sample",
"zh-CN": "插件示例"
},
"description": {
"default": "This is a plugin development sample",
"zh-CN": "这是一个插件开发示例"
},
"readme": {
"default": "README.md",
"zh-CN": "README.zh-CN.md"
},
"icon": "icon.png",
"preview": "preview.png",
"funding": {
"custom": ["https://ld246.com/sponsor"]
},
"keywords": [
"开发者参考",
"developer reference",
"示例插件"
]
}
name: Plugin package name, must be the same as the GitHub repository name, and cannot be duplicated with other plugins in the marketplace
author: Plugin author name
url: Plugin repo URL
version: Plugin version number, needs to follow the semver specification
minAppVersion: Minimum SiYuan version required to use this plugin
settingsWindow: Opt in to an independent plugin instance in native desktop settings windows (SiYuan 3.8.7+); omitted or false means the plugin is not loaded there. This does not select how settings are opened; see Independent settings windows
disabledInPublish: Whether to disable the plugin when using the publish service, defaults to false, i.e., not disabled
publish.resources: Extra frontend resources available to publishing-service visitors, using exact file paths relative to the plugin directory, such as images/logo.png, or directory paths ending in /, such as fonts/; wildcards are not supported
publish.data: Public snapshot field names; each value must be a string, number, boolean or null, and access requires a separate administrator grant
backends: Backend environment required by the plugin, optional values are windows, linux, darwin, docker, android, ios, harmony and all
windows: Windows desktop
linux: Linux desktop
darwin: macOS desktop
docker: Docker
android: Android APP
ios: iOS APP
harmony: HarmonyOS APP
all: All environments
kernels: Backend environment supported by the plugin's kernel plugin (kernel.js), optional values are the same as backends (windows, linux, darwin, docker, android, ios, harmony and all)
- Only needed when the plugin includes a kernel plugin; if this field is missing or empty, the kernel plugin will not be started, but the plugin can still be installed and used on the frontend
frontends: Frontend environment required by the plugin, optional values are desktop, desktop-window, mobile, browser-desktop, browser-mobile and all
desktop: Desktop
desktop-window: Desktop window converted from tab
mobile: Mobile APP
browser-desktop: Desktop browser
browser-mobile: Mobile browser
all: All environments
displayName: Plugin name (plain text), displayed in the marketplace list
default: Default language, must exist. If the plugin supports English, English should be used here
zh-CN, en and other languages: optional, must be BCP 47 tags (e.g. zh-CN, zh-TW, en, ja, pt-BR)
description: Plugin description (plain text), displayed in the marketplace list
default: Default language, must exist. If the plugin supports English, English should be used here
zh-CN, en and other languages: optional, must be BCP 47 tags
readme: Readme file name, displayed in the marketplace details page
default: Default language, must exist. If the plugin supports English, English should be used here
zh-CN, en and other languages: optional, must be BCP 47 tags
- Relative images are loaded from
package.zip when present; otherwise the online marketplace falls back to the matching GitHub Release. Include them in package.zip for offline use
icon: Optional marketplace icon filename at the package root. Supports PNG, JPEG, WebP, and AVIF up to 64 KiB; the recommended size is 160*160
preview: Optional marketplace preview filename at the package root. Supports PNG, JPEG, WebP, and AVIF up to 512 KiB; the recommended size is 1024*768
- SVG is unsupported. To omit an image, remove its field and the legacy
icon.png or preview.png; an empty field value is invalid
funding: Plugin sponsorship information
openCollective: Open Collective name
patreon: Patreon name
github: GitHub login name
custom: Custom sponsorship link list
links: Labeled custom sponsorship links, for example {"label": "Sponsor", "url": "https://example.com"}
keywords: Search keyword list, used for marketplace search function, supplements search keywords beyond the values of name, author, displayName, and description fields
Independent settings windows
Since SiYuan 3.8.7, an enabled, compatible plugin with "settingsWindow": true in plugin.json can run in both built-in and plugin-specific native desktop settings windows. Browser and mobile frontends do not use this mechanism. Omitting the field or setting it to false only prevents loading in these windows; ordinary frontend instances are unaffected.
The flag does not force settings to open in a new window. The user selects the built-in settings mode in Settings - App - Open settings in a separate window. A plugin selects its own Setting opening mode with new Setting({openInWindow: true}); the default is a dialog, as used by this sample. These choices are independent of settingsWindow. An ordinary settings dialog uses the plugin instance already running in its frontend.
The new Setting({...}) call in src/index.ts includes a commented openInWindow: true option and explains its effect. Uncomment it to try a separate plugin settings window on the native desktop client (SiYuan 3.8.7+); browsers, mobile frontends, and calls within a settings window use a dialog. The published siyuan 1.2.9 SDK still lacks this constructor option. pnpm install applies patches/siyuan@1.2.9.patch, which synchronizes the option, its comments, and the missing scroll-position type export and import with the official Petal declarations, so no additional SettingOptions declaration or type assertion is needed. When upgrading to an SDK that includes these declarations, remove the patch and its patchedDependencies entry and regenerate the lockfile.
Each settings window creates its own plugin instance, with its own memory and DOM. It follows the existing onload, onLayoutReady, onDataChanged, and onunload lifecycle, but has no main-window document layout. addTopBar, addStatusBar, addDock, addCommand (including global shortcuts), addToolbarItem, addBreadcrumbButton, addTab, addFloatLayer, and addAgentCapability do not register entries there. Do not rely on their return values to access mounted UI. Messages can still be displayed in the current window.
The base Plugin.openSetting() delegates to the settings window host when called from a settings instance, provided this.setting exists. If you override it, retain that behavior with super.openSetting(); directly calling this.setting.open() bypasses this routing. Custom settings dialogs remain the plugin's responsibility.
See onload, initSetting, onLayoutReady, onDataChanged, onunload, and openSetting in src/index.ts. The sample checks document.body.classList.contains("body--settings"), initializes its settings without registering main-window examples, and adds the plugin-owned plugin-sample--settings body class as a style hook. The class is removed on unload. getFrontend() returns "desktop" in a settings window; "desktop-window" identifies a detached document window and cannot be used for this check. body--settings is the current DOM marker, not a separate frontend API value.
SiYuan loads the plugin's index.css in each settings instance. Dynamic styles and listeners created by plugin code must be applied and cleaned up by that instance; DOM changes in the main window are not copied. Extend the sample's style hook in src/index.scss when needed, and release timers, listeners, and generated style elements in onunload.
onDataChanged handles plugin storage changes, including sync and writes from other frontend instances; it is not a notification for every application configuration change. The sample re-reads its private settings and resets the cache first so a deleted file cannot restore stale values. Reapply any data-dependent styles after reading. Saving code should also update its own UI because the originating frontend is excluded from the cross-window write notification. Without an onDataChanged override, SiYuan reloads the plugin instead. Application configuration messages can be observed through ws-main as needed.
To opt out of settings-window execution, remove settingsWindow or set it to false and reopen the settings window. The sample keeps its existing minimum application version because the extra instance is optional; the settings-window behavior itself requires SiYuan 3.8.7 or later. Run pnpm test for the sample's settings and publishing regressions, and pnpm exec tsc --noEmit for type checks.
Publishing service
This sample requires SiYuan 3.8.4 or later. It keeps private settings accessed through loadData / saveData separate from public snapshots accessed through loadPublishData / savePublishData. Visitors and other code on the published page can read the public snapshot, so publish only content suitable for disclosure.
- In the administrator interface, enable the plugin and allow it in the publishing service, then open Plugin published data on its downloaded-plugin card and grant access to
readonlyText
- Open this plugin's settings, edit Readonly text, and click Generate published snapshot to publish the current text
- Open the published page and use the plugin's top bar menu to view the snapshot; use Refresh published snapshot to load updates
The administrator selects the public field explicitly:
await this.savePublishData({readonlyText: textareaElement.value});
The published frontend reads only the public snapshot:
const label = document.createElement("span");
try {
const data = await this.loadPublishData();
label.textContent = typeof data.readonlyText === "string" ? data.readonlyText : this.i18n.readonlyText;
} catch {
label.textContent = this.i18n.readonlyText;
}
See src/index.ts