How do I get the macro-id for the current macro?

As the question states, I want to figure how to to access the macro-id of my current macro.

conversionContext.getPageContext().getEntity().getBodyContent().getBody() gives me a list of all the macros and their macro-ids and parameters but I want that information for only the macro currently being processed by java and not all the macros on the page. How do I do so?

Hello hello

I am not sure if this is documented anywhere, but I happen to know that we are setting some properties on the context, one of those properties is the MacroDefinition .
You can access it as follow:

import com.atlassian.confluence.content.render.xhtml.ConversionContext;
import com.atlassian.confluence.content.render.xhtml.storage.macro.MacroId;
import com.atlassian.confluence.macro.Macro;
import com.atlassian.confluence.xhtml.api.MacroDefinition;

import java.util.Map;

public class MyMacro implements Macro {
    @Override
    public String execute(
            final Map<String, String> parameters,
            final String body,
            final ConversionContext context
    ) {
        MacroDefinition macroDefinition = (MacroDefinition) context.getProperty("macroDefinition");
        String macroId = macroDefinition.getMacroIdentifier().map(MacroId::getId).orElse(null);
        return String.format("<p>Devbox macroId=%s</p>", macroId);
    }
...
}

In preview mode however, the macroId is not set, so you need to take that into account.

It would also be great for us to understand why you’d need the macroId in your processing, so the team can revisit the API at some point

I hope this helps.

Cheers
Hasnae R.
former Confluence person

3 Likes

Hi Viqueen, hope you doing everything well.

Nice to see you’ve resolved this issue.

Unfortunately, I’ve hitting the similar issue on REST API.

Do you know how to get macroId from content of a page?

Here is my issue: How to get macroId for API api/content-getMacroBodyByMacroId

would you mind to give me some advices on it?

Have a great day ahead.