How to convert JIRA wiki markup to HTML programmly using Atlassian native API?

5.1k Views Asked by At

I am trying to get the description of an issue from JIRA to put it in a Confluence Storage Format template in order to create a page in Confluence. But I could't find a way to render the description raw data to a storage format recognizable format. Here is a concrete example : For an issue in JIRA with following description :

enter image description here

The description string I get by calling com.atlassian.jira.issue.Issue.getDescription() is :

{color:#14892c}Recently Updated{color}
h1. *_As you and your team create content this area will fill up and display the latest updates._*

If I don't make it wrong, the string I got is its wiki template representation. Insert it directly in Storage format will not be recognized by the template engine so will not be properly rendered.

I have tried using <ac:rich-text-body> to enclose the string but it doesn't work. Seems to I have to convert the wiki representation to HTML, or XHTML. How can I achieve this in Java code?

2

There are 2 best solutions below

1
qingl97 On

To convert the JIRA wiki markup to HTML rendered output from JIRA:

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.ComponentManager;

public String renderWikiMarkup(Issue issue) {
    RendererManager rendererManager = ComponentManager.getComponent(RendererManager.class);
    JiraRendererPlugin renderer = rendererManager.getRendererForType("atlassian-wiki-renderer");
    String output = renderer.render(issue.description, issue.getIssueRenderContext());
    return output;
}
0
Matti Kiviharju On

Here is fully working solution for Atlassian Jira 8.0.0 or later.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.RendererManager;
import com.atlassian.jira.issue.fields.renderer.JiraRendererPlugin;
import com.atlassian.jira.issue.fields.renderer.IssueRenderContext;

public String renderWikiMarkupOfDescription(String descriptor, Issue issue) {
    RendererManager rendererManager = ComponentAccessor.getComponentOfType(RendererManager.class);
    JiraRendererPlugin renderer = rendererManager.getRendererForType("atlassian-wiki-renderer");
    String output = renderer.render(descriptor, new IssueRenderContext(issue));
    return output;
}

public String renderWikiMarkupOfEnvironment(String environment, Issue issue) {
    RendererManager rendererManager = ComponentAccessor.getComponentOfType(RendererManager.class);
    JiraRendererPlugin renderer = rendererManager.getRendererForType("atlassian-wiki-renderer");
    String output = renderer.render(environment, new IssueRenderContext(issue));
    return output;
}

And usage:

renderWikiMarkupOfDescription(issue.getDescription(), issue);
renderWikiMarkupOfEnvironment(issue.getEnvironment(), issue);

And screenshots to prove this working 100 % success:

Description Rendered to HTML 1 Description Rendered to HTML 2 Environment Rendered to HTML 1