Macro

With the release of @forge/react version 11.0.0, enhancements have been made to the useConfig hook to improve performance in macro config apps when receiving configuration value changes.

Confluence macro config apps relying on the useProductContext hook or view.getContext() need to transition to the useConfig hook before upgrading to @forge/react version 11.0.0 or higher in order to properly access the latest values after the configuration updates.

Confluence macro config apps using the useConfig hook should upgrade to @forge/react version 11.0.0 for improved performance.

The macro module inserts dynamic content into the user interface via an editor. Editor macros are only compatible with the Atlassian editor. All cloud sites use the Atlassian editor by default.

The macro module works in Confluence, where the macro is inserted by typing / and selecting from the quick insert menu of the editor. The macro module is implemented by a Forge function.

For UI Kit 1, see the Macro component documentation for more information.

On apps that use Custom UI, module content is displayed inside a special Forge iframe which has the sandbox attribute configured. This means that HTML links (for example, <a href="https://domain.tld/path">...</a>) in this iframe won't be clickable. To make them clickable, use the router.navigate API from the @forge/bridge package.

Example of a macro

Manifest structure

1
2
modules {}
└─ macro []
   ├─ key (string) [Mandatory]
   ├─ resource (string) [Mandatory]
   ├─ render (string) [Optional]
   ├─ resolver {} [Optional]
   ├─ viewportSize (string) [Optional]
   ├─ title (string | i18n) [Mandatory]
   ├─ icon (string) [Optional]
   ├─ categories (string[]) [Optional]
   ├─ description (string | i18n) [Optional]
   └─ config (boolean | {} | config object) [Optional]
     ├─ title (string | i18n) [Optional]
     ├─ resource (string) [Mandatory]
     ├─ render (string) [Optional]
     ├─ viewportSize (string) [Optional]
     └─ openOnInsert (boolean) [Optional]
   ├─ export {} [Optional]
   ├─ adfExport {} [Optional]
   ├─ layout (string) [Optional]
   └─ autoconvert [] [Optional]
     └─ matchers [] [Mandatory]
        └─ pattern (string) [Mandatory]

resources []
├─ key (string) [Mandatory]
└─ path (string) [Mandatory]

Properties

PropertyTypeRequiredDescription
key

string

Yes

A key for the module, which other modules can refer to. Must be unique within the manifest.

Regex: ^[a-zA-Z0-9_-]+$

resourcestringIf using Custom UI or modern versions of UI KitThe key of a static resources entry that your module will display. See resources for more details.
render'native'If using modern versions of UI KitIndicates the module uses UI Kit.
functionstringDeprecated Required if using UI Kit 1The key of a function module that returns a UI Kit 1 component.
resolver{ function: string } or
{ endpoint: string }

Set the function property if you are using a hosted function module for your resolver.

Set the endpoint property if you are using Forge remote to integrate with a remote back end.

viewportSize'small', 'medium', 'large', 'xlarge' or 'max'The display size of resource. Can only be set if the module is using the resource property. Remove this property to enable automatic resizing of the module.
titlestring or i18n objectYes

The title of the macro. In Confluence, this is displayed in the editor.

The i18n object allows for translation. See i18n object.

iconstring

The icon displayed next to the title.


For custom UI and UI Kit apps, the icon property accepts a relative path from a declared resource. Alternatively, you can also use an absolute URL to a self-hosted icon. See Icons for more information.

If no icon is provided, or if there's an issue preventing the icon from loading, a generic app icon will be displayed.

categoriesstring[]The categories of the macro. In Confluence, this is used for categorisation in the macro browser.
  • formatting
  • confluence-content
  • media
  • visuals
  • navigation
  • external-content
  • communication
  • reporting
  • admin
  • development
descriptionstring or i18n object

The description of the macro. In Confluence, this is displayed in the editor.

The i18n object allows for translation. See i18n object.

configboolean, { function: string } or config object

Set config to true if you are using classic macro configuration.

Set config with the function property if you are using a UI Kit 1 macro.

Set config to the config object if you are using a custom macro configuration.

config.titlestring or i18n objectA title for the config.
config.resourcestringRequired if using custom UI or the latest version of UI Kit.A reference to the static resources entry that your context menu app wants to display. See resources for more details.
config.render'native'Yes for UI KitIndicates the module uses UI Kit.
config.viewportSize'small', 'medium', 'large', 'xlarge' or 'max'The display size of resource. Can only be set if the module is using the resource property. Remove this property to enable automatic resizing of the module.
config.openOnInsertbooleanDefaults to true. An optional configuration to control if the custom configuration modal is automatically opened when first inserted.
export{ function: string }For UI Kit 1 and Custom UI use only. Contains a function property, which references the function module that defines the export view of the macro, specified in UI Kit 1 components. The specified function can consume the exportType from the extension context in order to specify different export views per export type.
adfExport{ function: string }For UI Kit and Custom UI use only. Contains a function property, which references the function module that defines the export view of the macro, specified in Atlassian document format. The specified function can consume the exportType directly from the function's payload in order to specify different views per export type. The exportType can be one of pdf, word, or other. See this tutorial for more information.
layout'block', 'inline' or 'bodied'

'block' type is used by default.

'inline' shows the element inline with existing text.

  • For UI Kit apps, inline macros dynamically resize to wrap the content.
  • A limitation exists for custom UI apps that prevents inline macros from dynamically resizing when the content of the macro is changed.

'bodied' sets the macro to have a rich text body.

  • This allows users to insert and edit rich content (such as images and tables) within the macro using the Confluence editor, and allows your app to insert a body using a custom editor.
  • Please see the link to the tutorial here.

autoconvertautoconvert object Inserts a macro into the editor when a recognised URL is pasted in by the user. See Macro autoconvert.
autoconvert.matchers[matcher, ...]Yes, if using autoconvertThe list of patterns that define what URLs should be matched.
autoconvert.matchers.patternstringYes, if using autoconvert A string that defines a specific URL pattern to be matched, using wildcards for variable parts of the URL, such as unique IDs.
  • Use multiple wildcards to match multiple sub-paths. Do not include all sub-paths with a single wildcard.
  • Ensure URLs do not contain whitespace unless it is URL encoded.
  • Wildcards cannot be used in place of a protocol. The protocol must be specified as http or https.
  • Maximum length of the pattern is 1024 characters.
openOnInsertbooleanNoDefaults to true. An optional configuration to control if the custom configuration modal is automatically opened when first inserted or not.

i18n object

KeyTypeRequiredDescription
i18nstringYesA key referencing a translated string in the translation files. For more details, see Translations.

Extension context

UI Kit and Custom UI

PropertyTypeDescription
typestringThe type of the module (macro).
content.idstringA string that represents the unique identifier of the content object
space.idstringA string that represents the unique identifier of the space object.
space.keystringA string that represents the unique key of the space object.
isEditingbooleanIndicates whether the macro is opened in the editor or not.
referencesReferenceEntity[]An array of reference entities (if any). Reference entities are a list of any other ADF nodes on the page that are referenced by this macro.
configobjectThe configuration parameters saved in this macro
macro.bodyADF documentThe rich text body of the macro. Available for layout: bodied macros only.
autoConvertLinkstringThe link pasted by a user that has matched an AutoConvert app

UI Kit 1

PropertyTypeDescription
typestringThe type of the module (macro).
exportTypestringThe type of the current export. Available for the export function only. Valid values: One of pdf, email, word or other.
Note: other implies export other than pdf, email or word. For example, when the macro is shown in the Page history or exported via API.

Macro autoconvert

Macro autoconvert allows your app to automatically insert a macro into the editor when a user pastes a recognized URL. This is achieved by defining URL patterns in the manifest using the matchers property. These matchersare registered in the editor when the app is installed.

Example

1
2
modules:
  macro:
    - key: autoconvert-app
      resource: main
      render: native
      resolver:
        function: resolver
      title: Forge app for autoconvert
      description: Example for autoconvert manifest
      autoConvert:
        matchers:
          - pattern: https://www.example.com/*/about
          - pattern: https://www.example.com/*/music
          - pattern: https://*.example.com/*/movies/*
          - pattern: https://example.com/gifs/*/
          - pattern: http://*.example.com/media/*/.gif
            
  function:
    - key: resolver
      handler: index.handler
resources:
  - key: main
    path: src/frontend/index.jsx
app:
  id: '<your app id>'

The URL patterns use wildcards to match parts of the URL that can vary, such as unique IDs. Wildcards are defined using *.

Use a new * for each segment in the URL you want a wildcard for. For example, https://www.example.com/* will match https://www.example.com/about but will not match https://www.example.com/about/contact. To match this path as well you need to include https://www.example.com/*/* as one of your matchers.

You'll need to define a separate matcher for each relevant internet protocol, such as http and https.

Example patterns

Wildcard path
1
2
- "pattern": "https://www.example.com/*/about"
Wildcard in subdomain
1
2
- "pattern": "https://www.*.example.com/help"
Matching wildcard paths
1
2
- "pattern": "https://bitbucket.org/*/*/*"

When a pasted URL matches a defined pattern, the macro is created in the editor, and the URL is captured and inserted as a parameter into the macro body. This parameter can be accessed using the autoConvertLink property.

Macro custom configuration

Extension context in the macro editor

There are two additional extension context parameters available when you are in macro configuration editor context.

ParameterTypeDetails
macro.isConfiguringbooleantrue if the currently rendered resource is the config resource, false if it is the macro's default resource
macro.isInsertingbooleantrue if a new macro is being inserted, false if an existing macro is being edited

Options for submitting the configuration

This table details the options supported by view.submit() in the context of custom macro configuration.

ParameterTypeRequiredDetailsCode
configConfig payloadYesSets the config properties of the macro.
view.submit({
  config: {
    param1: "test",
    param2: [1, 2, 3]
  }
})
bodyADF documentNoSets the rich text body of the macro. Can only be used with layout: bodied macros.
view.submit({
  config: {},
  body: {
    type: "doc",
    version: 1,
    content: [
      // ADF content
    ]
  }
})
keepEditingbooleanNoDefaults to false, which automatically closes the config modal on submit. Set this to true to keep the modal open.
view.submit({
  config: {},
  keepEditing: true
})

Supported config payload format

The config payload only supports values that can be serialised to JSON.

The following types are allowed on the payload:

  1. undefined
  2. string
  3. number
  4. boolean
  5. object (can contain any of the allowed types, including nested objects)
  6. array (can contain strings, numbers, booleans, and objects; all items in the array must be of the same type).

The following types are not allowed:

  • Nested arrays (arrays as direct children of arrays)
  • null
  • Any data types that are not serializable to JSON (e.g. Map, Set, etc.)

If you want greater control over the storage format of your configuration, such as being able to store nested arrays and nulls, we recommend serializing your configuration to JSON upfront, and storing it as a string.

Error code guide

This table details the possible error codes that may be thrown by view.submit():

Error codeDetails
INVALID_PAYLOADThe top-level parameter passed to view.submit() must be an object.
INVALID_CONFIGThe config prop provided must be an object that is compliant with the config payload format above.
INVALID_EXTENSION_TYPEWhen providing a body, the macro must be a rich text macro (layout: "bodied").
INVALID_BODYThe provided body is not a valid ADF document node.
MACRO_NOT_FOUNDThe macro that you are attempting to update no longer exists. It may have been deleted by another user editing the page.

Tutorials

Rate this page: