Confluence Rest API Maintaining Macros After View Conversion Edited

Hi, I built a Python module that downloads the HTML from a confluence page, modifies it, and then updates the page. Currently what I'm doing:

I first fetch the storage JSON content by doing a 'get' request using Python requests module from the following link:

{SERVER_URL}/rest/api/content/{PAGE_ID}?expand=body.storage

From there I do a post request with this JSON data to the link:

{SERVER_URL}/rest/api/contentbody/convert/view

 The resulting data is standard HTML which I then modify, convert back to storage format with another post request to:

{SERVER_URL}/rest/api/contentbody/convert/storage

And from there I re-publish the page with a put request containing all the updated JSON information.

Almost everything works, and when just viewing the page normally, everything looks correct. The only problem is certain macros - although displaying correctly in view mode - are not converted correctly back to storage format, so when modifying the page in edit mode, things like info macros do not visually show in the editor. Instead of showing the actual info box, it looks like regualar bold text until I close the editor.

Is this normal behavior for the conversion? Is there a way to republish the page and keep all macros exactly as they would appear if added in the editor or should my approach be to just figure out how to modify the storage format directly without converting it to view HTML?

Here's some code snippets for reference --

Original storage content:

<ac:structured-macro ac:name="info" ac:schema-version="1" ac:macro-id="e5767804-bcd7-449d-a3c6-d8b56a17222e">
<ac:rich-text-body><strong>INFO Text Here</strong>
</ac:rich-text-body>
</ac:structured-macro> 

 Original converted HTML view:

<div class="confluence-information-macro confluence-information-macro-information conf-macro output-block" data-hasbody="true" data-macro-name="info">
<span class="aui-icon aui-icon-small aui-iconfont-info confluence-information-macro-icon"></span>
<div class="confluence-information-macro-body"><strong>INFO Text Here</strong></div>
</div>

Converted back to storage:

<div class="confluence-information-macroconfluence-information-macro-information conf-macro output-block">
<span class="aui-icon aui-icon-small aui-iconfont-info confluence-information-macro-icon"></span>
<div class="confluence-information-macro-body"><strong>INFO Text Here</strong></div>
</div>

Any guidance would be greatly appreciated, thanks! 

1 answer

0 vote
Stephen Deutsch Community Champion Sep 15, 2017

I would say your best bet is to try to modify the storage format directly. I'm surprised that you can even change view mode back to storage, as there's nothing in the view code that allows Confluence to tell what the original macro looked like. This means that you'll never be able to get the original code from re-conversion, so changing the storage format would be the way to go.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

2,896 views 27 12
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you