如何使用Atlassian原生API将JIRA wiki标记转换为HTML程序?

时间:2016-06-01 13:42:32

标签: java jira confluence wiki-markup

我正在尝试从JIRA获取问题的描述,将其置于Confluence存储格式模板中,以便在Confluence中创建页面。但我找不到将描述原始数据呈现为存储格式可识别格式的方法。这是一个具体的例子: 有关JIRA中的问题,请参阅以下说明:

enter image description here

我通过调用com.atlassian.jira.issue.Issue.getDescription()得到的描述字符串是:

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

如果我没有弄错,我得到的字符串是它的wiki模板表示。直接以存储格式插入它将无法被模板引擎识别,因此无法正确呈现。

我尝试使用<ac:rich-text-body>来封装字符串,但它不起作用。似乎我必须将wiki表示转换为HTML或XHTML。我怎样才能在Java代码中实现这一点?

2 个答案:

答案 0 :(得分:2)

将JIRA wiki标记转换为JIRA的HTML呈现输出:

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;
}

答案 1 :(得分:0)

这是适用于 Atlassian Jira 8.0.0 或更高版本的完整解决方案。

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;
}

和用法:

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

以及证明此操作 100% 成功的屏幕截图:

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