How do I create a User Macro for my ToC?

LightWave Docs October 26, 2018

Hi,
At the top of my documentation pages, I always have a table of contents contained within an Expand macro that I have to edit so that the description reads "Click here to expand Table of Contents". For the Table of Contents itself, I almost always want it to be biggest heading H2, smallest H3, absolute URL. I need to edit that as well, then move the ToC into the Expand box. All this is very repetitive and I wanted to create a user macro for it. I have copied the code from a finished ToC like so:

## Macro title: Table of Contents complete
## Macro has a body: N
## Body processing: Unrendered
## Output: Selected output option
##
## Developed by: Ben Vost
## Date created: 26/10/2018
## Installed by: Ben Vost

<ac:structured-macro ac:macro-id="c08f3550-71c0-456a-95b5-4b35eab745c1" ac:name="expand" ac:schema-version="1">
  <ac:parameter ac:name="title">Click here to expand Table of Contents</ac:parameter>
  <ac:rich-text-body>
    <p>
      <ac:structured-macro ac:macro-id="dcfb2e12-22bc-49bb-97b8-0a75e11391f5" ac:name="toc" ac:schema-version="1">
        <ac:parameter ac:name="maxLevel">3</ac:parameter>
        <ac:parameter ac:name="minLevel">2</ac:parameter>
      </ac:structured-macro>
    </p>
  </ac:rich-text-body>
</ac:structured-macro>

What am I doing wrong, oh masters of Confluence?

Thanks for your help!

B

2 answers

1 accepted

0 votes
Answer accepted
LightWave Docs October 27, 2018

Here is my end result and yes, it is atomic and poses no further questions:

## Macro title: Expanding ToC
## Macro has a body: N
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: Ben Vost, aided by Jan-Peter Rusch
## Date created: 26/10/2018
## Installed by: Ben Vost

## @noparams

<ac:structured-macro ac:macro-id="2ba609e9-51e9-48ed-9de9-2324b8767c20" ac:name="expand" ac:schema-version="1">
  <ac:parameter ac:name="title">Click here to expand Table of Contents...</ac:parameter>
  <ac:rich-text-body>
    <p>
      <ac:structured-macro ac:macro-id="de573681-fba8-4b31-a11c-e1f566847346" ac:name="toc" ac:schema-version="1">
        <ac:parameter ac:name="maxLevel">3</ac:parameter>
        <ac:parameter ac:name="minLevel">2</ac:parameter>
        <ac:parameter ac:name="absoluteUrl">true</ac:parameter>
      </ac:structured-macro>
    </p>
  </ac:rich-text-body>
</ac:structured-macro>

Thanks to Jan-Peter!

B

Bill Bailey
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 27, 2018

I would suggest deleting the fields:

ac:macro-id

They are supposed to be unique identifiers. I always delete them from my macros.

0 votes
JP _AC Bielefeld Leader_
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 26, 2018

Hi B

what version of Confluence are you running?

I tried that myself & ran into some strange problems on 6.11.0

If I create a default user macro & copy your code after the generated code, name it "test" & save it, it works like a charm...

So create a new macro: No Body macro

## Macro title: My Macro
## Macro has a body: Y or N
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: My Name
## Date created: dd/mm/yyyy
## Installed by: My Name

## This is an example macro
## @param Name:title=Name|type=string|required=true|desc=Text for Expand|default=Click here to expand Table of Contents

<ac:structured-macro ac:macro-id="97707e12-9b24-48b9-a6ae-cc2efac83d3e" ac:name="expand" ac:schema-version="1">
<ac:parameter ac:name="title">$paramName</ac:parameter>
<ac:rich-text-body>
<p>
<ac:structured-macro ac:macro-id="5e23a343-70b1-4787-ade2-02cea1413ac9" ac:name="toc" ac:schema-version="1">
<ac:parameter ac:name="maxLevel">3</ac:parameter>
<ac:parameter ac:name="minLevel">2</ac:parameter>
<ac:parameter ac:name="absoluteUrl">true</ac:parameter>
</ac:structured-macro>
</p>
</ac:rich-text-body>
</ac:structured-macro> 

Best

JP 

LightWave Docs October 26, 2018

Hallo Jan-Peter,

Thank you so much! It doesn't quite work however. The first time I ran it, Confluence told me it couldn't find the server and trying it again, it just did the expand part of the deal. There was no ToC inside.

I am on Confluence 6.6.1

B

JP _AC Bielefeld Leader_
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 26, 2018

You should copy your code part because your macro ids are not the same as mine. Keep the parameter for the expand title.

LightWave Docs October 27, 2018

Ah!  I see, I thought the Macro IDs would be universal. Is there a way to make the macro self-contained, so that you add it without needing to click anything? After all, it has all been set up already.

Thanks for your help with this,

B

JP _AC Bielefeld Leader_
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 27, 2018

So you want it on any page without adding it manually?

You can do this by editing the Page Layout in the admin site layout section: Check woth the source editor how your user macro is inserted into a page & copy that code into the page layout.

We did this with a macro listing all incoming links to a page, if there are any...

Check for 

 <div id="main-content" class="wiki-content">
$helper.renderConfluenceMacro("{YOUR MACRO NAME}")
$body
</div> 

This can harm your whole confluence site, if the html is not valid!

Another option would be to put it into the global templates.

Best

JP

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events