Converting/rendering XHTML in user macros
Asked by Robert Khanafiev:

Hi everyone.

I'm new to Atlassian API and Velocity language, but I have to create my macro for exporting comments into PDF file.

I can successfully access the comments with smth like

```
#set ($comments = $content.getComments())
#foreach( $comment in $comments )
   #set ($commentBody = $comment.getContentEntityObject().getBodyAsString())
   $commentBody
#end
```

but seems like comments are returned to me in XHTML-type.

Attached screenshots show the example of original comment and its exported representation. How can I correctly render it?

![OriginalComment.png](/t5/image/serverpage/image-id/116507i7FFC2998F8A3070E/image-dimensions/464x53?v=v2 "OriginalComment.png")![WrongComment.png](/t5/image/serverpage/image-id/116508iCA38C3456169F720/image-dimensions/469x46?v=v2 "WrongComment.png")


Answers:
author: Robert Khanafiev
body: Found something interesting.

If I use

```
$comment.getEntity().getBodyContent().getBody()
```

in Confluence Server v7.4.5 everything works fine and comments are exported to PDF document properly.

![preview url image](https://oko.center/api/v4/files/s7fzbxfgdfbsip8zikcch78cow/preview)

In Confluence Server v7.7.X and v7.8.X my macros doesn't work properly and comments are exported without any render.

![preview url image](https://oko.center/api/v4/files/653xc4kt63naxx1guioo3x7dnh/preview)

Of course all the settings for different versions are the same.

Is it a bug?

comments: author: Bill Bailey
body: That is very interesting. I would suggest looking at the API docs to see what is changed, but unfortunately, the docs are very sparse.

Filing a case is probably your best option. There may already be a bug filed, or you get the joy of filing one. ;-)


author: Gary Sargent
body: Did you get anywhere on this. I'm seeing the same behaviour change when trying to upgrade to V7.8.X from V6.15.9


author: Robert Khanafiev
body: Not yet. Left this task in the background.


author: Robert Khanafiev
body: Up. Still need an answer.

Can anyone at least tell me, how can I properly access $xhtmlContent?


author: Bill Bailey
body: Do you have rendered or unrendered checked for the output of the macro?

comments: author: Robert Khanafiev
body: You mean the "Macro body processing" key? If so, It doesn't depend on it.


author: Bill Bailey
body: Yes, and you output should vary.

And use the inspect function to see how the output is being structured.


author: Bill Bailey
body: And I see there is this method that might help -

```
getBodyAsStringWithoutMarkup()
```


author: Robert Khanafiev
body: Thank you, I'l try it on monday.


author: Robert Khanafiev
body: Have tried

```
getBodyAsStringWithoutMarkup()
```

Yes, It works good, but of course it removes all the links (see attachment), so it is not exactly what I want. Is there any way to render the comment saving links (link to the user profile in my example)?

![CommentNoMarkup.png](https://community.atlassian.com/t5/image/serverpage/image-id/116542i8B1342C5B5674538/image-dimensions/116x40?v=v2 "CommentNoMarkup.png")

Below you can see what I have already tried. Let's assume, that for all the examples below "$comment" is taken from here:

```
#set ($comments = $content.getComments())
#foreach( $comment in $comments )
   #### comment is available here
#end
```

1) Using renderConfluenceMacro. The output it the same as in my first message.

```
#set( $helper= $action.getHelper() )
#set( $commentBody1 = $comment.getContentEntityObject().getBodyContent().getBody() )
#set( $commentBody2 = $comment.getContentEntityObject().getBodyAsString() )
#set( $commentBody3 = "$comment.getContentEntityObject().getBodyAsString()" )


$helper.renderConfluenceMacro( commentBody1 )
$helper.renderConfluenceMacro( commentBody2 )
$helper.renderConfluenceMacro( commentBody3 )

```

2) Using xhtmlContent. Doesn't work at all.

```
#set ($xhtmlContent = $action.getContentBean())
#set( $commentBody1 = $comment.getContentEntityObject().getBodyContent().getBody() )
#set( $commentBody2 = $comment.getContentEntityObject().getBodyAsString() )
#set( $commentBody3 = "$comment.getContentEntityObject().getBodyAsString()" )

$xhtmlContent.convertStorageToView($commentBody1, null)
$xhtmlContent.convertStorageToView($commentBody2, null)
$xhtmlContent.convertStorageToView($commentBody3, null)
```


Tags: confluence-server

server

user-macro