Inline confluence user macro
Asked Answered
C

1

6

How to create a confluence macro with body that produces an inline output? The following macro:

## @noparams
<font color="red">$body</font>

applied to this text

Before macro [macro starts here]macro body[macro ends here] after macro.

Will create this HTML code:

<p>Before macro </p>
<font color="red">macro body</font>
<p>after macro.</p>

How to remove the <p></p> tags?

Cassiodorus answered 24/4, 2012 at 16:18 Comment(0)
J
3

This is an issue with Confluence. To avoid this, you have to use html output. If the body or your macro contain wiki markup, then you will have to render this by hand. My workaround is as follows:

## Use this macro to avoid new lines:
#macro(doNothing)#end
##
## (Do stuff with body in wiki format, if appropriate)
##
## Convert to html and remove paragraph tags:
#set($renderedhtml = "")
#if ($body && ($body.length()>0))
  #set($globalHelper = $action.getHelper())
  #set($renderedhtml = $globalHelper.renderConfluenceMacro("$body").replaceAll("</?[pP]>",""))
#end
##
## (Do stuff with body in html format, if appropriate)
##
## Output text:
$renderedhtml#doNothing()

EDIT: You will want to modify the regex if there are p tags in your macro that you want to keep. The regex in the code above will remove ALL p tags.

Jegger answered 25/4, 2012 at 7:23 Comment(6)
Thanks for your response. Unfortunately my problem is apparently not the new paragraphs in the code generated by my macro but that confluence inserts </p> before and <p> after my macro, embedding the preceding and the following text into new paragraphs. Can I cancel also these tags?Cassiodorus
That's exactly what the template above does. Try it.Jegger
Can you please explain how to render by hand?Drub
I ended up basing something off of your code. #set($body = "[link|doc:mypage]"). Obviously this overwrites body, but the macro isn't supposed to have a body in the first place.Drub
By render by hand I am just referring to what I do in the script. The idea is actually that you set body to whatever wiki formatting you want to render where it says "(Do stuff with body in wiki format, if appropriate)", so your usage fits perfectly with my intent.Jegger
This is one of several problems with the new Confluence editor after wiki markup was ditched in Confluence V4 which has since been resolved as of Confluence 5.4.W

© 2022 - 2024 — McMap. All rights reserved.