Confluence Rest API Maintaining Macros After View Conversion

codycuellar September 14, 2017

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 votes
Stephen Deutsch
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 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
TAGS
AUG Leaders

Atlassian Community Events