It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How many macros in a page? Edited

Hi,

I am creating a user macro and would like to count how many times a specific macro is present in a page. Is there a way to accomplish this using Velocity?

Thank you.

2 answers

0 votes
Davin_Studer Community Leader Oct 10, 2019

What are you trying to accomplish with counting macros on the page?

Hi Davin,

I would like to differentiate the element ID of the HTML container by using an increment on it before the page was rendered at the client side, something like id="macro-1", id="macro-2", etc.

I can do it at the client side via Javascript but would like to see if I could render the page already with the proper number in the ID. I managed to identify the macro I wish in a page but not to count how many instances of it exist.

Thank you.

Davin_Studer Community Leader Oct 14, 2019

So, are you trying to apply a unique id to user macros?

Hi Vitor,

it’s certainly possible, you can read the storage format, which is just xhtml, and each macro is defined by an <ac:macro> element. You would have to treat it as a string, though, no DOM operations would be available in a user macro.

However, the biggest problem is that the xhtml element is “translated” by Confluence into regular html code during the final page rendering process. A user macro has no access to this final html code. So, if you added an id to the macro element, it would just be ignored. The best you could do would be to wrap the macro in a div element and assign an id to that.

It’s a bit long of an answer, but I hope it makes sense.

Hi Stephen,

Thank you for your help but I am not quite sure if your answer covers exactly what I'm looking for (very likely because my post was very, very vague)...

The macro I created for test purposes is using a <div id="macro-${macroNumber}">... I would like to to confirm the existence of macros with the same name in the page, how many and then assign n+1 to $macroNumber, being n the number of macros found.

Thanks

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

What project did you transition or start on Confluence with the shift to remote work?

It’s been great to hear from fellow users over the last few weeks about the best tips and fun moments you’ve had working on Confluence since the transition to working remote. I’d love to keep the c...

159 views 3 8
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you