跳转到内容

插件基础指南

StudioCMS 插件是扩展平台功能的核心机制,提供了一种简单灵活的方式来增强项目能力。本文将深入解析 StudioCMS 插件的工作原理和基础结构。

StudioCMS 插件类似于 Astro 集成,但在函数对象上附加了额外元数据。这些元数据用于确定插件的加载和使用方式。插件通过添加新功能或修改现有行为来扩展 StudioCMS 的能力。

interface StudioCMSPlugin {
identifier: string; // 插件唯一标识符
name: string; // 插件显示名称
studiocmsMinimumVersion: string; // 要求的最低 StudioCMS 版本
hooks: {
// Astro 配置钩子
'studiocms:astro:config': (params: {
logger: AstroIntegrationLogger; // 日志记录器
addIntegrations: (args_0: AstroIntegration | AstroIntegration[]) => void; // 添加集成
}) => void | Promise<void>;
// 配置初始化钩子
'studiocms:config:setup': (params: {
logger: AstroIntegrationLogger;
// 设置站点地图
setSitemap: (params: {
triggerSitemap?: boolean | undefined;
sitemaps?: {
pluginName: string;
sitemapXMLEndpointPath: string | URL;
}[] | undefined;
}) => void;
// 设置仪表板
setDashboard: (params: {
dashboardGridItems?: GridItemInput[] | undefined;
dashboardPages?: {
user?: (...)[] | undefined;
admin?: (...)[] | undefined;
} | undefined;
settingsPage?: {
fields: (...)[];
endpoint: string;
} | undefined;
}) => void;
// 设置前端
setFrontend: (params: {
frontendNavigationLinks?: {
label: string; // 导航标签
href: string; // 链接地址
}[] | undefined;
}) => void;
// 设置渲染
setRendering: (params: {
pageTypes?: {
label: string; // 页面类型标签
identifier: string; // 唯一标识符
description?: string | undefined; // 描述
fields?: (...)[] | undefined; // 字段定义
pageContentComponent?: string | undefined; // 内容组件
rendererComponent?: string | undefined; // 渲染组件
apiEndpoint?: string | undefined; // API端点
}[] | undefined;
}) => void;
}) => void | Promise<void>;
}
}

定义 StudioCMS 插件需创建符合上述接口的对象,必须包含以下属性:

  • identifier: 插件的唯一标识符(通常取自 package.json)
  • name: 在 StudioCMS 仪表板中显示的插件名称
  • studiocmsMinimumVersion: 插件所需的最低 StudioCMS 版本

以下是一个完整的 StudioCMS 插件定义示例,包含所有必需属性并通过 Astro 集成实现自定义逻辑:

my-plugin.ts
import {
function definePlugin(options: StudioCMSPlugin): StudioCMSPlugin

Defines a plugin for StudioCMS.

@paramoptions - The configuration options for the plugin.

@returnsThe plugin configuration.

definePlugin
} from 'studiocms/plugins';
import {
(alias) interface AstroIntegration
import AstroIntegration
AstroIntegration
} from 'astro';
// 定义插件和集成的选项接口
interface
interface Options
Options
{
Options.foo: string
foo
: string; // 示例配置项
}
// 创建 Astro 集成
const
const myIntegration: (options: Options) => AstroIntegration
myIntegration
= (
options: Options
options
:
interface Options
Options
):
(alias) interface AstroIntegration
import AstroIntegration
AstroIntegration
=> ({
AstroIntegration.name: string

The name of the integration.

name
: 'my-astro-integration',
AstroIntegration.hooks: {
'astro:db:setup'?: (options: {
extendDb: (options: {
configEntrypoint?: URL | string;
seedEntrypoint?: URL | string;
}) => void;
}) => void | Promise<void>;
... 12 more ...;
'studiocms:plugins'?: PluginHook<...>;
} & Partial<...>

The different hooks available to extend.

hooks
: {
"astro:config:setup": () => {
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
('我的 Astro 集成已启动!');
}
}
});
// 定义 StudioCMS 插件
export const
const myPlugin: (options: Options) => StudioCMSPlugin
myPlugin
= (
options: Options
options
:
interface Options
Options
) =>
function definePlugin(options: StudioCMSPlugin): StudioCMSPlugin

Defines a plugin for StudioCMS.

@paramoptions - The configuration options for the plugin.

@returnsThe plugin configuration.

definePlugin
({
StudioCMSPlugin.identifier: string
identifier
: 'my-plugin', // 插件标识符
StudioCMSPlugin.name: string
name
: '我的插件', // 插件显示名称
StudioCMSPlugin.studiocmsMinimumVersion: string
studiocmsMinimumVersion
: '0.1.0-beta.18', // 最低兼容版本
StudioCMSPlugin.hooks: {
'studiocms:astro:config'?: PluginHook<...>;
'studiocms:config:setup'?: PluginHook<...>;
} & Partial<...>
hooks
: {
// Astro 配置钩子
'studiocms:astro:config': ({
addIntegrations: (args_0: AstroIntegration | AstroIntegration[]) => void
addIntegrations
}) => {
addIntegrations: (args_0: AstroIntegration | AstroIntegration[]) => void
addIntegrations
(
const myIntegration: (options: Options) => AstroIntegration
myIntegration
(
options: Options
options
)) // 推荐添加集成
}
}
});

在以下示例中,我们定义了一个名为 My Plugin 的 StudioCMS 插件,该插件要求 StudioCMS 版本为 0.1.0-beta.18 或更高。该插件还提供了 Astro 集成功能,在触发 astro:config:setup 钩子时向控制台输出日志信息:

深入了解插件开发实践,请参阅高效插件开发指南