Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Nesting macros in user macros - how to stop render of JSON output by nested macro before user macro?

Austin April 16, 2018

I am using SQL Query to get a JSON string from Postgres which is then fed via a user macro to JavaScript to initialize Chart.js plugin for better charts

 

Seems like I can only place another macro inside my user macro if I select "Rendered" body. However, this causes Confluence to render the resulting JSON as Confluence markup, which breaks everything completely and horribly. How can I allow a macro as the body of my user macro but stop it from processing that data before handoff to my user macro? Is there an intermediary macro I can use to stop that processing? Is there a way to escape curly brackets without whatever escape character following through to the user macro? the user macro needs valid JSON as input.

 

Right now everything works if i change my user macro to "unrendered" and paste my JSON manually, but the JSON is dynamic hence the SQL query.

1 answer

1 accepted

0 votes
Answer accepted
Austin April 16, 2018

I didn't find a way around it, I ended up using javascript to remove the crap.

 

1) Load underscore.js in Custom HTML <head> or in place 

2) Set the SQL Query output to "escape special wiki characters"

3) Use the following javascript on document load:

<script type="text/javascript">
var json =`$body`;

json = json.replace(/<\/?span[^>]*>/g,"");
json = json.replace(/(<|)br\s*\/*(>|)/g,' ');
json = _.unescape(json);
json = JSON.parse(json);

//var json should now be valid JSON, hopefully

</script>
Ramakrishnan Srinivasan
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.
June 29, 2019

Hi Austin,

 Thank you for your post, I have tried nesting with chart macro and successful. But the charts are very basic and I dont have much control.

I was going through the chart.js documentation but could not figure out how to nest Chart.js inside a user macro, 

 

Can you please share that part where you nested chart.js in your user macro?

Thank you 

with warm regards

ramki

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events