Dynamic Content Macro: How to retrieve rendered macro body


#1

My addon accepts a macro body of type rich text.
According to the dynamic content macro guide it suggests that I retrieve the macro body with the REST API.

This returns the macro body in Confluence storage format. Is there a way to retrieve it in rendered HTML?

Or do I need to subsequently call convert content body. Could you help me understand why I get a 403 for this while using AP.require('request'? with “XSRF check failed”.

Thanks


#2

Okay nevermind, I managed to call convert content body. That was a lot of pain to work out. The docs need improvement.


#3

@joshua.tjhin, would you mind sharing the answer for conversion? We are having the same issue. Thanks in adavnce!


#4

hi @criley, something like this.
I am transpiling ES6 + async/await here but you can just treat AP.request to return a Promise. You’ll need to load the superbatch css to get your content styled.

Hope this helps! Good luck!

const convertStorageToHtml = async contentAsStorage => {
  const convertResponse = await AP.request({
    url:
      "/rest/api/contentbody/convert/view?expand=webresource.superbatch.uris.css",
    type: "POST",
    headers: {
      Accept: "application/json",
      "X-Atlassian-Token": "nocheck"
    },
    contentType: "application/json",
    data: JSON.stringify({
      value: contentAsStorage,
      representation: "storage"
    })
  });

  return JSON.parse(convertResponse.body);
};

convertStorageToHtml(contentAsStorage).then(converted => {
  console.log('macroBody', converted.value);
  console.log('superBatchUri', converted.webresource.superbatch.uris.css); // You must load this css file to style your content
});


#5

Hi @joshua.tjhin, how did you mange to get embedded images in the retrieved HTML rendered correctly?

I have several Confluence having image attachments embedded with the ac:image macro as follows

<ac:image ac:height=\"250\"><ri:attachment ri:filename=\"hqdefault.jpg\" /></ac:image>

When the content body convert REST API resource is called following HTML is generated, see the snippet

<img src=\"/plugins/servlet/confluence/placeholder/error?i18nKey=editor.placeholder.broken.image&locale=en_GB&version=2\" title=\"\" class=\"transform-error\" data-encoded-xml=\"%3Cac%3Aimage+ac%3Aheight%3D%22250%22%3E%3Cri%3Aattachment+ri%3Afilename%3D%22hqdefault.jpg%22+%2F%3E%3C%2Fac%3Aimage%3E\" />

Seems the images could not be rendered according to the img html tag. Do you have any idea or is rendering of images with the convert method not possible?


#6

You are right. I could not get the images to render properly from the convert API. I’ve given up on my macro/add-on.

Maybe you could try constructing the image URLs yourself from the attachment name.