Zum Inhalt springen

Renderer Plugins

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

StudioCMS renderer plugins provide a way to extend and customize the rendering process of your StudioCMS application. They allow you to modify how content is rendered on the frontend by adding custom components, wrappers, or other modifications to the rendering pipeline.

To get started with creating a renderer plugin, you need to define a StudioCMS plugin that registers your custom page type and its associated renderer component. Below is an example of how to create a simple renderer plugin that adds a custom page type with a renderer and an editor component.

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 {
const createResolver: (_base: string) => {
resolve: (...path: Array<string>) => string;
}

Allows resolving paths relatively to the integration folder easily. Call it like this:

@param_base - The location you want to create relative references from. import.meta.url is usually what you'll want.

@seehttps://astro-integration-kit.netlify.app/core/create-resolver/

@example

const { resolve } = createResolver(import.meta.url);
const pluginPath = resolve("./plugin.ts");

This way, you do not have to add your plugin to your package.json exports.

createResolver
} from 'astro-integration-kit';
import {
(alias) interface AstroIntegration
import AstroIntegration
AstroIntegration
} from 'astro';
const {
const resolve: (...path: Array<string>) => string
resolve
} =
function createResolver(_base: string): {
resolve: (...path: Array<string>) => string;
}

Allows resolving paths relatively to the integration folder easily. Call it like this:

@param_base - The location you want to create relative references from. import.meta.url is usually what you'll want.

@seehttps://astro-integration-kit.netlify.app/core/create-resolver/

@example

const { resolve } = createResolver(import.meta.url);
const pluginPath = resolve("./plugin.ts");

This way, you do not have to add your plugin to your package.json exports.

createResolver
(import.

The type of import.meta.

If you need to declare that a given property exists on import.meta, this type may be augmented via interface merging.

meta
.
ImportMeta.url: string

The absolute file: URL of the module.

This is defined exactly the same as it is in browsers providing the URL of the current module file.

This enables useful patterns such as relative file loading:

import { readFileSync } from 'node:fs';
const buffer = readFileSync(new URL('./data.proto', import.meta.url));

url
);
// Define the StudioCMS Plugin
export const
const myPlugin: () => StudioCMSPlugin
myPlugin
= () =>
function definePlugin(options: StudioCMSPlugin): StudioCMSPlugin

Defines a plugin for StudioCMS.

@paramoptions - The configuration options for the plugin.

@returnsThe plugin configuration.

definePlugin
({
StudioCMSPlugin.identifier: string

The identifier of the plugin, usually the package name.

identifier
: 'my-plugin',
StudioCMSPlugin.name: string

The name of the plugin, displayed in the StudioCMS Dashboard.

name
: 'My Plugin',
StudioCMSPlugin.studiocmsMinimumVersion: string

The minimum version of StudioCMS required for this plugin to function correctly. This is used to ensure compatibility between the plugin and the StudioCMS core. It should be a semantic version string (e.g., "1.0.0"). If the plugin is not compatible with the current version of StudioCMS, it should not be loaded. This is a required field.

@example "1.0.0"

studiocmsMinimumVersion
: '0.1.0-beta.28',
StudioCMSPlugin.hooks: {
'studiocms:astro:config'?: PluginHook<{
logger: AstroIntegrationLogger;
addIntegrations: (args_0: AstroIntegration | AstroIntegration[]) => void;
}>;
'studiocms:config:setup'?: PluginHook<...>;
} & Partial<...>
hooks
: {
'studiocms:config:setup': ({
setRendering: (args_0: {
pageTypes?: {
label: string;
identifier: string;
fields?: ({
name: string;
label: string;
input: "checkbox";
required?: boolean | undefined;
readOnly?: boolean | undefined;
color?: "primary" | "success" | "warning" | "danger" | "info" | "mono" | undefined;
defaultChecked?: boolean | undefined;
size?: "sm" | "md" | "lg" | undefined;
} | {
name: string;
label: string;
input: "input";
type?: "number" | "text" | "password" | "email" | "tel" | "url" | "search" | undefined;
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
name: string;
label: string;
input: "textarea";
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
options: {
value: string;
label: string;
disabled?: boolean | undefined;
}[];
name: string;
label: string;
input: "radio";
required?: boolean | undefined;
readOnly?: boolean | undefined;
color?: "primary" | "success" | "warning" | "danger" | "info" | "mono" | undefined;
defaultValue?: string | undefined;
direction?: "horizontal" | "vertical" | undefined;
} | {
options: {
value: string;
label: string;
disabled?: boolean | undefined;
}[];
name: string;
label: string;
input: "select";
type?: "search" | "basic" | undefined;
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
name: string;
label: string;
input: "row";
fields: ({
name: string;
label: string;
input: "checkbox";
required?: boolean | undefined;
readOnly?: boolean | undefined;
color?: "primary" | "success" | "warning" | "danger" | "info" | "mono" | undefined;
defaultChecked?: boolean | undefined;
size?: "sm" | "md" | "lg" | undefined;
} | {
name: string;
label: string;
input: "input";
type?: "number" | "text" | "password" | "email" | "tel" | "url" | "search" | undefined;
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
name: string;
label: string;
input: "textarea";
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
options: {
value: string;
label: string;
disabled?: boolean | undefined;
}[];
name: string;
label: string;
input: "radio";
required?: boolean | undefined;
readOnly?: boolean | undefined;
color?: "primary" | "success" | "warning" | "danger" | "info" | "mono" | undefined;
defaultValue?: string | undefined;
direction?: "horizontal" | "vertical" | undefined;
} | {
options: {
value: string;
label: string;
disabled?: boolean | undefined;
}[];
name: string;
label: string;
input: "select";
type?: "search" | "basic" | undefined;
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
})[];
required?: boolean | undefined;
readOnly?: boolean | undefined;
alignCenter?: boolean | undefined;
gapSize?: "sm" | "md" | "lg" | undefined;
})[] | undefined;
description?: string | undefined;
pageContentComponent?: string | undefined;
rendererComponent?: string | undefined;
apiEndpoint?: string | undefined;
}[] | undefined;
augments?: ({
type: "component";
components ...
setRendering
}) => {
setRendering: (args_0: {
pageTypes?: {
label: string;
identifier: string;
fields?: ({
name: string;
label: string;
input: "checkbox";
required?: boolean | undefined;
readOnly?: boolean | undefined;
color?: "primary" | "success" | "warning" | "danger" | "info" | "mono" | undefined;
defaultChecked?: boolean | undefined;
size?: "sm" | "md" | "lg" | undefined;
} | {
name: string;
label: string;
input: "input";
type?: "number" | "text" | "password" | "email" | "tel" | "url" | "search" | undefined;
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
name: string;
label: string;
input: "textarea";
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
options: {
value: string;
label: string;
disabled?: boolean | undefined;
}[];
name: string;
label: string;
input: "radio";
required?: boolean | undefined;
readOnly?: boolean | undefined;
color?: "primary" | "success" | "warning" | "danger" | "info" | "mono" | undefined;
defaultValue?: string | undefined;
direction?: "horizontal" | "vertical" | undefined;
} | {
options: {
value: string;
label: string;
disabled?: boolean | undefined;
}[];
name: string;
label: string;
input: "select";
type?: "search" | "basic" | undefined;
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
name: string;
label: string;
input: "row";
fields: ({
name: string;
label: string;
input: "checkbox";
required?: boolean | undefined;
readOnly?: boolean | undefined;
color?: "primary" | "success" | "warning" | "danger" | "info" | "mono" | undefined;
defaultChecked?: boolean | undefined;
size?: "sm" | "md" | "lg" | undefined;
} | {
name: string;
label: string;
input: "input";
type?: "number" | "text" | "password" | "email" | "tel" | "url" | "search" | undefined;
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
name: string;
label: string;
input: "textarea";
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
options: {
value: string;
label: string;
disabled?: boolean | undefined;
}[];
name: string;
label: string;
input: "radio";
required?: boolean | undefined;
readOnly?: boolean | undefined;
color?: "primary" | "success" | "warning" | "danger" | "info" | "mono" | undefined;
defaultValue?: string | undefined;
direction?: "horizontal" | "vertical" | undefined;
} | {
options: {
value: string;
label: string;
disabled?: boolean | undefined;
}[];
name: string;
label: string;
input: "select";
type?: "search" | "basic" | undefined;
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
})[];
required?: boolean | undefined;
readOnly?: boolean | undefined;
alignCenter?: boolean | undefined;
gapSize?: "sm" | "md" | "lg" | undefined;
})[] | undefined;
description?: string | undefined;
pageContentComponent?: string | undefined;
rendererComponent?: string | undefined;
apiEndpoint?: string | undefined;
}[] | undefined;
augments?: ({
type: "component";
components ...
setRendering
({
pageTypes?: {
label: string;
identifier: string;
fields?: ({
name: string;
label: string;
input: "checkbox";
required?: boolean | undefined;
readOnly?: boolean | undefined;
color?: "primary" | "success" | "warning" | "danger" | "info" | "mono" | undefined;
defaultChecked?: boolean | undefined;
size?: "sm" | "md" | "lg" | undefined;
} | {
name: string;
label: string;
input: "input";
type?: "number" | "text" | "password" | "email" | "tel" | "url" | "search" | undefined;
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
name: string;
label: string;
input: "textarea";
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
options: {
value: string;
label: string;
disabled?: boolean | undefined;
}[];
name: string;
label: string;
input: "radio";
required?: boolean | undefined;
readOnly?: boolean | undefined;
color?: "primary" | "success" | "warning" | "danger" | "info" | "mono" | undefined;
defaultValue?: string | undefined;
direction?: "horizontal" | "vertical" | undefined;
} | {
options: {
value: string;
label: string;
disabled?: boolean | undefined;
}[];
name: string;
label: string;
input: "select";
type?: "search" | "basic" | undefined;
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
name: string;
label: string;
input: "row";
fields: ({
name: string;
label: string;
input: "checkbox";
required?: boolean | undefined;
readOnly?: boolean | undefined;
color?: "primary" | "success" | "warning" | "danger" | "info" | "mono" | undefined;
defaultChecked?: boolean | undefined;
size?: "sm" | "md" | "lg" | undefined;
} | {
name: string;
label: string;
input: "input";
type?: "number" | "text" | "password" | "email" | "tel" | "url" | "search" | undefined;
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
name: string;
label: string;
input: "textarea";
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
} | {
options: {
value: string;
label: string;
disabled?: boolean | undefined;
}[];
name: string;
label: string;
input: "radio";
required?: boolean | undefined;
readOnly?: boolean | undefined;
color?: "primary" | "success" | "warning" | "danger" | "info" | "mono" | undefined;
defaultValue?: string | undefined;
direction?: "horizontal" | "vertical" | undefined;
} | {
options: {
value: string;
label: string;
disabled?: boolean | undefined;
}[];
name: string;
label: string;
input: "select";
type?: "search" | "basic" | undefined;
required?: boolean | undefined;
readOnly?: boolean | undefined;
placeholder?: string | undefined;
defaultValue?: string | undefined;
})[];
required?: boolean | undefined;
readOnly?: boolean | undefined;
alignCenter?: boolean | undefined;
gapSize?: "sm" | "md" | "lg" | undefined;
})[] | undefined;
description?: string | undefined;
pageContentComponent?: string | undefined;
rendererComponent?: string | undefined;
apiEndpoint?: string | undefined;
}[] | undefined
pageTypes
: [
{
identifier: string
identifier
: 'my-custom-page-type',
label: string
label
: 'My Custom Page Type',
rendererComponent?: string | undefined
rendererComponent
:
const resolve: (...path: Array<string>) => string
resolve
('./components/render.js'),
pageContentComponent?: string | undefined
pageContentComponent
:
const resolve: (...path: Array<string>) => string
resolve
('./components/Editor.astro')
}
]
})
}
}
});

For the renderer component, you need to create a JavaScript or TypeScript file (if you have a build step) that exports an object conforming to the PluginRenderer type. This object should include the rendering logic for your custom page type.

components/render.js
import type {
(alias) interface PluginRenderer
import PluginRenderer

Represents a plugin renderer with optional sanitization and rendering logic.

@propertyname - The unique name of the plugin renderer.

@propertysanitizeOpts - Optional configuration for content sanitization.

@propertyrenderer - Optional asynchronous function to render content as a string.

PluginRenderer
} from 'studiocms/types';
const
const render: {
name: string;
renderer: (content: string) => Promise<string>;
sanitizeOpts: {};
}
render
= {
PluginRenderer.name: string
name
: 'my-custom-renderer',
PluginRenderer.renderer?: GenericAsyncFn<string, string>
renderer
: async (
content: string
content
: string) => {
// Custom rendering logic goes here
return
content: string
content
;
},
PluginRenderer.sanitizeOpts?: SanitizeOptions
sanitizeOpts
: {},
} satisfies
(alias) interface PluginRenderer
import PluginRenderer

Represents a plugin renderer with optional sanitization and rendering logic.

@propertyname - The unique name of the plugin renderer.

@propertysanitizeOpts - Optional configuration for content sanitization.

@propertyrenderer - Optional asynchronous function to render content as a string.

PluginRenderer
;
export default
const render: {
name: string;
renderer: (content: string) => Promise<string>;
sanitizeOpts: {};
}
render
;

For the editor component, you need to create an Astro component that provides a user interface for editing the content of your custom page type. This component will receive the current content as a prop and should emit updates to the content as the user makes changes. The <textarea> below is used in the final page edit form, allowing users to edit the content directly. So any changes made in any custom editor component should update the value of this <textarea> to ensure the content is saved correctly.

components/Editor.astro
---
import type { PluginPageTypeEditorProps } from 'studiocms/types';
interface Props extends PluginPageTypeEditorProps {}
const { content } = Astro.props;
---
<div class="editor-container">
<textarea id="page-content" name="page-content">{content}</textarea>
</div>