Front-end macros API

Last modified by Vincent Massol on 2026/11/30 17:29

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.

FieldTypeDescription
idstringThe macro identifier. Only lowercase letters, uppercase letters, digits, and underscores are allowed.
namestringHuman-readable name, shown in menus.
descriptionstringDescription of the macro.
paramsRecord<string, MacroParameterType>The macro's parameter definitions.
paramsDescriptionrecord of stringA human-readable description for each parameter.
defaultParametersconcrete parameters or falseDefault 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.

MemberTypeDescription
infosMacroInfosThe 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.

MemberTypeDescription
infosMacroInfosThe 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

TypeDescription
Macro<Parameters>A macro: either a BlockMacro or an InlineMacro.
MacroWithUnknownParamsTypeA Macro whose parameter shape is unknown. Used by generic code such as the Macros Service.
MacroClassWithUnknownParamsTypeConstructor 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.

TypeDescription
UnknownMacroParamsTypeGeneric 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.

typeDescriptionKey fields
"paragraph"A paragraph of inline contentcontent, styles
"heading"A headinglevel (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 quotecontent (nested blocks), styles
"code"A code blockcontent (string), optional language
"table"A tablecolumns, rows, styles
"image"An image (see Image)target, styles, optional alt, caption, widthPx, heightPx
"break"A thematic break(none)
"macroBlock"A nested block macro callcall (MacroBlockInvocation)
"rawHtml"A block of raw HTMLhtml
"macroBlockEditableArea"Where the editable body of a block macro is displayed, inside the macro's own outputstyles

InlineContent

An inline node, discriminated by its type.

typeDescriptionKey fields
"text"Styled textcontent, styles (TextStyles)
"image"An inline imagesame fields as the "image" block
"link"A linktarget (LinkTarget), content (inline content, excluding nested links)
"inlineMacro"A nested inline macro callcall (InlineMacroInvocation)
"rawHtml"Raw inline HTMLhtml
"inlineMacroEditableArea"Where the editable body of an inline macro is displayed, inside the macro's own output(none)

Supporting types

TypeDescription
TextStylesOptional text styling: bold, italic, strikethrough, underline, code, textColor, backgroundColor.
BlockStylesOptional block styling: textColor, backgroundColor, textAlignment ("left", "center", "right" or "justify"), cssClasses.
ImageAn image, with target (LinkTarget), styles ({ alignment? }, with the same values as textAlignment), and optional alt, caption, widthPx and heightPx.
ListItemA list item: content (Block[]), styles, optional number and checked.
LinkTargetEither { type: "internal", rawReference, parsedReference }, where parsedReference is the parsed entity reference or null when rawReference is invalid, or { type: "external", url }.
TableColumnA table column: optional headerCell (TableCell) and widthPx.
TableCellA 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;
ParameterTypeDescription
macromacro classThe macro class to cast.

Returns the same macro class, retyped without its parameter shape.

Get Connected