How to render wiki markup in velocity for JIRA

For those who search for a way to render wiki markup in a Velocity file, for example in JIRA email templates:


#set ($rendererManager = $componentManager.getRendererManager() )

  #set ($renderer = $rendererManager.getRendererForType("atlassian-wiki-renderer"))

  #set ($ircontext = $renderer.getRenderContext() )

  #set ($renderedText = $renderer.render($wikimarkuptext, $ircontext) )

Caveats:

Not all characters will be displayed by Outlook in the email. Bold and URL links worked for me, but not tables or emoticons.

Also, I'm not sure if the ComponentManager object is still accessible in the email template Velocity context for newer versions of JIRA

3 answers

And just for completeness, doing this remotely via an AC addon is of course a different matter, there is a 1.0 API:

https://blah.atlassian.net/rest/api/1.0/render

{"rendererType":"atlassian-wiki-renderer","unrenderedMarkup":"||h||\n| td|","issueKey":"SUPPORT-1"}

The 2.0 API inclusion has been requested (perhaps I cant see it): AC-1155

This is a very nice tip thank you. I've had to implement as part of a custom Velocity template including Description and Comments fields. Small adjustment in that (at least in my Jira 6.2) it actually needs the IssueRenderContext (not just any RenderContext) to be able to display inline images and attachments. Full result looks like this: {code} #set ($componentManagerClass = $constantsManager.getClass().getClassLoader().findClass('com.atlassian.jira.ComponentManager')) #set ($method = $componentManagerClass.getDeclaredMethod('getInstance', null)) #set ($componentManager = $method.invoke(null, null)) #set ($commentManager = $componentManager.getCommentManager()) #set ($rendererManager = $componentManager.getRendererManager()) #set ($wikiRenderer = $rendererManager.getRendererForType('atlassian-wiki-renderer')) #set ($ircontext = $issue.getIssueRenderContext()) #set ($issueComments = $commentManager.getComments($issue)) #if ($issueComments.size()>0) #set ($last = $issueComments.size() - 1) #set ($lastComment = $issueComments.get($last)) #end ... <tr><th valign="top">Response: </th><td>$wikiRenderer.render($lastComment.body, $ircontext)<br/></td></tr> ... {code}

Thanks Matt and David!

ComponentManager object is available in Higher JIRA version, have used it with JIRA 6.4.8 hence we do not need to define it again. The entire code I have used in velocity template of a custom field using wiki renderer is as below:

 

#disable_html_escaping()

 

#set ($rendererManager = $componentManager.getRendererManager())

#set ($wikiRenderer = $rendererManager.getRendererForType('atlassian-wiki-renderer'))

#set ($ircontext = $issue.getIssueRenderContext())

 

#if ($customFieldManager.getCustomFieldObject("customfield_10005"))

    #set ($cfCustomFieldName = $customFieldManager.getCustomFieldObject("customfield_10005").getFieldName())

    #set ($cfCustomFieldVal = $issue.getCustomFieldValue($customFieldManager.getCustomFieldObject("customfield_10005")))

    

#end

 

#if ($cfCustomFieldVal)

 <tr>

    <th>$cfCustomFieldName:</th>

    <td>$wikiRenderer.render($cfCustomFieldVal, $ircontext)

    </td>

 </tr>

#end

 

 

 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira Service Desk

How the Telegram Integration for Jira helps Sergey's team take their support efficiency to the bank

...+ reading Fantasy). The same is true for him at the bank he works for: Efficiency is key when time literally equals money. Read on to learn how Sergey makes most of the time he has by...

286 views 1 3
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