How many macros in a page?

Vitor LC October 10, 2019

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
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.
October 12, 2019

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.

Vitor LC October 18, 2019

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

0 votes
Davin Studer
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.
October 10, 2019

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

Vitor LC October 11, 2019

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
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.
October 14, 2019

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

Vitor LC October 18, 2019

Yes.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events