Front-end macros API
Types define a block or inline macro, its parameters and the content it renders.
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 content to display, as UniAst nodes (see Rendering output).
import type {
BlockMacro,
MacroParameterType,
} from "@xwiki/platform-macros-api";
import type { Block } from "@xwiki/platform-uniast-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 }): Block[] {
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) | Block[] | 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) | InlineContent[] | 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 output
The render function returns UniAst nodes, the editor-independent content model of the @xwiki/platform-uniast-api package: block macros return Block[] and inline macros return InlineContent[]. The package itself is described in Front-End Rendering API.
XWiki <18.10.0
The macros API had its own rendering AST: render returned MacroBlock[] or MacroInlineContent[], built from the Macro* types. To migrate a macro, replace each type with the UniAst type of the same name without the Macro prefix (MacroBlock with Block, MacroLinkTarget with LinkTarget, and so on), then adapt to these differences:
- an internal link target needs a parsedReference (or null)
- a list no longer has numbered: give its items a number instead
- the content of a list item is Block[]
- a nested macro is { type, call: { kind, id, params, body } } instead of { type, name, params }
- an image requires styles (for example {})
Block
A block-level 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 list. It is numbered when at least one of its items has a number. | items (ListItem[]), 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 Image) | target, styles, optional alt, caption, widthPx, heightPx |
| "break" | A thematic break | (none) |
| "macroBlock" | A nested block macro call | call (MacroBlockInvocation) |
| "rawHtml" | A block of raw HTML | html |
| "macroBlockEditableArea" | Where the editable body of a block macro is displayed, inside the macro's own output | styles |
InlineContent
An inline node, discriminated by its type.
| type | Description | Key fields |
|---|---|---|
| "text" | Styled text | content, styles (TextStyles) |
| "image" | An inline image | same fields as the "image" block |
| "link" | A link | target (LinkTarget), content (inline content, excluding nested links) |
| "inlineMacro" | A nested inline macro call | call (InlineMacroInvocation) |
| "rawHtml" | Raw inline HTML | html |
| "inlineMacroEditableArea" | Where the editable body of an inline macro is displayed, inside the macro's own output | (none) |
Supporting types
| Type | Description |
|---|---|
| TextStyles | Optional text styling: bold, italic, strikethrough, underline, code, textColor, backgroundColor. |
| BlockStyles | Optional block styling: textColor, backgroundColor, textAlignment ("left", "center", "right" or "justify"), cssClasses. |
| Image | An image, with target (LinkTarget), styles ({ alignment? }, with the same values as textAlignment), and optional alt, caption, widthPx and heightPx. |
| ListItem | A list item: content (Block[]), styles, optional number and checked. |
| LinkTarget | Either { type: "internal", rawReference, parsedReference }, where parsedReference is the parsed entity reference or null when rawReference is invalid, or { type: "external", url }. |
| TableColumn | A table column: optional headerCell (TableCell) and widthPx. |
| TableCell | A table cell: content, styles, optional rowSpan and 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.