Front-end macros API
Reference
Defining a macro
A macro is described by the Macro type, which is the union of BlockMacro and InlineMacro.
Every macro provides:
- infos - the macro metadata (see MacroInfos).
- renderAs - "block" or "inline".
- render(params, rawBody) - a function returning the rendering AST.
import type {
BlockMacro,
MacroBlock,
MacroParameterType,
} from "@xwiki/platform-macros-api";
// Parameter definitions.
const params = {
message: { type: "string" },
} satisfies Record<string, MacroParameterType>;
// A block macro rendering its "message" parameter as a paragraph.
class InfoMacro implements BlockMacro<typeof params> {
readonly infos = {
id: "info",
name: "Information",
description: "Displays a message.",
params,
paramsDescription: { message: "The message to display." },
defaultParameters: { message: "" },
bodyType: "none",
} as const;
readonly renderAs = "block" as const;
render(params: { message: string }): MacroBlock[] {
return [
{
type: "paragraph",
styles: {},
content: [{ type: "text", content: params.message, styles: {} }],
},
];
}
}Macros are made available to the front-end by registering them with the Macros Service. See Service API.
MacroInfos
Metadata describing a macro.
| Field | Type | Description |
|---|---|---|
| id | string | The macro identifier. Only lowercase letters, uppercase letters, digits, and underscores are allowed. |
| name | string | Human-readable name, shown in menus. |
| description | string | Description of the macro. |
| params | Record<string, MacroParameterType> | The macro's parameter definitions. |
| paramsDescription | record of string | A human-readable description for each parameter. |
| defaultParameters | concrete parameters or false | Default parameter values used when inserting the macro. false hides the macro. When bodyType is "raw", a dialog box is shown to insert the macro. |
| bodyType | "none", "wysiwyg" or "raw" | Whether the macro has a body and how it is edited: "none" (no body), "wysiwyg" (WYSIWYG-editable body), "raw" (editable but not WYSIWYG). |
BlockMacro
A macro that renders as a block.
| Member | Type | Description |
|---|---|---|
| infos | MacroInfos | The macro metadata. |
| renderAs | "block" | Indicates that the macro renders as a block. |
| render(params, rawBody) | MacroBlock[] | Renders the macro. params are the (concrete) parameter values; optional fields may be absent or undefined. rawBody is the raw body string when bodyType is "raw", otherwise null. |
InlineMacro
A macro that renders as inline content.
| Member | Type | Description |
|---|---|---|
| infos | MacroInfos | The macro metadata. |
| renderAs | "inline" | Indicates that the macro renders as inline content. |
| render(params, rawBody) | MacroInlineContent[] | Renders the macro (see render in BlockMacro for the parameters). |
Macro types
| Type | Description |
|---|---|
| Macro<Parameters> | A macro: either a BlockMacro or an InlineMacro. |
| MacroWithUnknownParamsType | A Macro whose parameter shape is unknown. Used by generic code such as the Macros Service. |
| MacroClassWithUnknownParamsType | Constructor type for an instantiable macro with an unknown parameter shape. |
Macro parameters
MacroParameterType
The definition of a single macro parameter.
type MacroParameterType = (
| { type: "boolean" }
| { type: "float" }
| { type: "string" }
) & { optional?: true };The concrete TypeScript type of a parameter is derived from this definition: "boolean" maps to boolean, "float" to number ("float" is used instead of "number" to be more explicit for developers) and "string" to string. Setting optional to true makes the parameter optional.
Parameter type helpers
Advanced type-level helpers, mostly used internally to derive concrete parameter types.
| Type | Description |
|---|---|
| UnknownMacroParamsType | Generic record of a macro's runtime parameter values (Record<string, boolean | number | string>). |
| GetConcreteMacroParameterType<T> | Derives the concrete TypeScript type of a single parameter from its MacroParameterType definition. |
| GetConcreteMacroParametersType<T> | Derives the concrete parameters record type from a parameters definition; optional parameters become optional properties. |
| FilterUndefined<T> | Removes the properties that may be assigned undefined from a record. |
| UndefinableToOptional<T> | Makes the properties that may be assigned undefined optional in a record. |
Rendering AST
The render function returns an array of AST nodes: block macros return MacroBlock[] and inline macros return MacroInlineContent[].
MacroBlock
A block-level AST node, discriminated by its type.
| type | Description | Key fields |
|---|---|---|
| "paragraph" | A paragraph of inline content | content, styles |
| "heading" | A heading | level (1 to 6), content, styles |
| "list" | A bullet or numbered list | items, optional numbered, styles |
| "quote" | A block quote | content (nested blocks), styles |
| "code" | A code block | content (string), optional language |
| "table" | A table | columns, rows, styles |
| "image" | An image (see MacroImage) | target, optional alt, widthPx, heightPx |
| "macroBlock" | A nested block macro reference | name, params |
| "rawHtml" | A raw HTML block | html |
| "macroBlockEditableArea" | Editable-area placeholder for a block macro body | styles |
MacroInlineContent
An inline AST node, discriminated by its type.
| type | Description | Key fields |
|---|---|---|
| "text" | Styled text (see MacroText) | content, styles |
| "link" | A link (see MacroLink) | target, content |
| "rawHtml" | Raw inline HTML | html |
| "inlineMacro" | A nested inline macro reference | name, params |
| "inlineMacroEditableArea" | Editable-area placeholder for an inline macro body | (none) |
Supporting AST types
| Type | Description |
|---|---|
| MacroText | A text node: content (string) and styles (MacroTextStyles). |
| MacroTextStyles | Optional text styling: bold, italic, strikethrough, underline, code, textColor, backgroundColor. |
| MacroBlockStyles | Optional block styling: cssClasses, textColor, backgroundColor, textAlignment (MacroAlignment). |
| MacroAlignment | One of "left", "center", "right", "justify". |
| MacroListItem | A list item: content, styles, optional checked. |
| MacroImage | An |
| MacroLink | A link: target (MacroLinkTarget) and content (inline content, excluding nested links). |
| MacroLinkTarget | Either { type: "internal", rawReference } or { type: "external", url }. |
| MacroTableColumn | A table column: optional headerCell (content + styles) and widthPx. |
| MacroTableCell | A table cell: content, styles, optional rowSpan, colSpan. |
Functions
eraseParamsTypeForMacroClass(macro)
Casts a typed macro class to a MacroClassWithUnknownParamsType, dropping its parameter shape. This is useful when macros with different parameter shapes need to be handled together, for example
when registering them.
function eraseParamsTypeForMacroClass<
Params extends Record<string, MacroParameterType>,
>(
macro: new (...args: any[]) => Macro<Params>,
): MacroClassWithUnknownParamsType;| Parameter | Type | Description |
|---|---|---|
| macro | macro class | The macro class to cast. |
Returns the same macro class, retyped without its parameter shape.