How to insert link to page in other space in user macro

Hi, I want to create an annotated variant of the Include Page macro in confluence. My version should surround the included page in a panel and add a caption (included from <link>).

I got it working for pages in the same space, but not for pages in a different space, because I do not know how to set the <ri:page ri:space-key> parameter from my ## @param ip:title=Included Page|type=confluence-content|required=true|desc=Select a page to include parameter:

 

Example storage format for an include page macro:

<p><ac:structured-macro ac:name="include" ac:schema-version="1" ac:macro-id="7c3338cb-d85f-4aeb-8c61-5fb36e24d61f"><ac:parameter ac:name=""><ac:link><ri:page ri:space-key="TFML" ri:content-title="Instructions for use" /></ac:link></ac:parameter></ac:structured-macro></p>

 

My user macro:


## @param ip:title=Included Page|type=confluence-content|required=true|desc=Select a page to include
## @param Title:title=Title|type=string|desc=Title

<ac:structured-macro ac:name="panel">
<ac:parameter ac:name="borderColor">#6699CC</ac:parameter>
<ac:parameter ac:name="borderWidth">1</ac:parameter>
<ac:parameter ac:name="title">$!paramTitle</ac:parameter>
<ac:rich-text-body>
<p><ac:structured-macro ac:name="include" ><ac:parameter ac:name="">
<ac:link><ri:page ri:content-title="$paramip" /></ac:link></ac:parameter>
</ac:structured-macro></p>
<div><sub><em>(included from <ac:link><ri:page ri:content-title="$paramip" /></ac:link>)</em></sub></div>
</ac:rich-text-body>
</ac:structured-macro>

-------------

Of course, I could add a second @param where the user has to enter the space name, but that would be independend of the page parameter and allow invalid input combinations. 

How can I extract the space from the confluence-content parameter or how can I construct correct links only using the confluence-content parameter?

 

(if I use a same-space-page in my macro, it works as expected, but for a foreign-space-page, this error message is rendered:

image.png

2 answers

1 accepted

Accepted Answer
0 votes

I literally spent Tuesday creating this same macro. Today I found your post! (It would have saved me all those hours, but to be honest I think I learned a lot more doing it myself.)

Does this help?

 

## Macro title: Page Link with Ref
## Macro has a body: No
## Body processing: No Macro Body
## @param PageToInclude:title=Page to Include|type=confluence-content|required=true|desc=To specify a page in a different space, use SPACEKEY:Page Title.


#if ( $paramPageToInclude.indexOf(":") == -1 )
  #set ( $spaceKey = $space.key )
  #set ( $contentTitle = $paramPageToInclude)
#else
  #foreach ( $part in $paramPageToInclude.split(":") )
    #set ( $spaceKey = $part )
    #break
  #end
  #foreach ( $part in $paramPageToInclude.split(":") )
    #set ( $contentTitle = $part )
  #end
#end
#set ( $linkSpace = $spaceManager.getSpace($spaceKey) )

#set ( $spacetitle = $linkSpace.getName())
#if ($spacetitle.contains('Space'))  
 
#else
   #set ( $spacetitle = "$spacetitle Space")
#end

<em>
This is a link to
<ac:link>
    <ri:page ri:content-title="$!contentTitle" ri:space-key="$spaceKey"/> $!paramPageToInclude
</ac:link>
 in the <a href="$linkSpace.homePage.urlPath"> $spacetitle. </a>
<br>

<ac:macro ac:name="panel">
        <ac:parameter ac:name="titleBGColor">lightyellow</ac:parameter>
        <ac:parameter ac:name="borderStyle">solid</ac:parameter>
        <ac:parameter ac:name="borderColor">#6699CC</ac:parameter>
        <ac:parameter ac:name="borderWidth">1</ac:parameter>
        <ac:parameter ac:name="titleColor">#000000</ac:parameter>
  ##     <ac:parameter ac:name="title">This is a link to "$!contentTitle" in the $spacetitle.</ac:parameter>

    <ac:rich-text-body>
              <ac:structured-macro ac:name="include">
                <ac:parameter ac:name="">
                  <ac:link>
                    <ri:page ri:content-title="$contentTitle" ri:space-key="$spaceKey"/>
                  </ac:link>
                </ac:parameter>
              </ac:structured-macro>

</ac:rich-text-body>
</ac:macro>

Thank you very much, this helps a lot!

Very nice solution, supports auto-complete in the parameter editor including spaces.

What exactly do the $paramip look like when you select a "foriegn page"? You may want to add a debug help in the macro to just display the contents of that variable to see what it is returning. Maybe the space key infor is prepended?

I haven't done this type of page navigation before. As your macro is now, it lets a user search and find a page in any space?

I've tried that. $paramip contains only the page name, no space key. The insert-macro-dialog  allows an auto-complete search over the complete confluence wiki, same as for the original include page macro (by defining type=confluence-content). 

Then you are forced to have the user select the space key so that you have that info. Not sure how you trap invalid combinations.

There maybe a way to extract the key data from the page id with a java method. Maybe this will give you enough hints:

https://gist.github.com/chids/870528

 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 09, 2018 in Confluence

Introducing Praecipio Consulting, an Atlassian Solution Partner

Hey there Community!  My name is Vannya Vallejo, the Channel Communication Specialist at Atlassian and I want to help Atlassian users like you learn about our Solution Partners and how they c...

389 views 0 9
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you