منى
منى

Reputation: 666

Convert plain text / wiki syntax to HTML with Jira ScriptRunner (show bullet points, checkmark smileys, etc.)

I am trying to retrieve the text of the most recent comment I typed in a JIRA issue page, here is a screenshot of the most recent comment I typed: enter image description here

I am using this code in the Jira script runner to retrieve the last comment I have typed in:

import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat
import com.opensymphony.util.TextUtils
import com.atlassian.jira.issue.comments.*
import org.w3c.dom.*;
import javax.xml.parsers.*;
import groovy.xml.*
import grrovy.util.*; 
import org.xml.sax.InputSource; 
import java.io.*; 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

def commentManager = ComponentAccessor.getCommentManager()

Comment comment = commentManager.getLastComment (issue)

if(comment != null) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm", Locale.ENGLISH)
    
    
    
    def body = comment.body //.replaceAll("[\r\n]+","\n")
     
    body= """<div style="max-height:100px; overflow:auto;" title="${comment.authorFullName} added last comment - ${dateFormat.format(comment.created)}">
    <span style="white-space: pre-wrap;">${TextUtils.htmlEncode(body)}</span>
    </div>""" 
    return body
   
}

The output of the code above is the following:

enter image description here

I would like the output to be displayed as in its original format meaning, I would like the bullet points to appear, the smiley to appear and the green checkmark to appear as well. I don't want to see the stars and the code color and the smiley abbreviation :) I would like the output to look like the following:

enter image description here

How can I fix this?

Upvotes: 1

Views: 1673

Answers (1)

CraZ
CraZ

Reputation: 1834

You need to render wiki-syntax source to HTML output and use existing wiki renderer.

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

RendererManager rendererManager = ComponentAccessor.getComponentOfType(RendererManager.class);
JiraRendererPlugin renderer = rendererManager.getRendererForType("atlassian-wiki-renderer");

String lastComment = "* 1\n* 2\n* 3:)\n* {color:#FF0000}(/){color}"

return renderer.render(lastComment, null)

Upvotes: 2

Related Questions