Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,295,423
Community Members
 
Community Events
165
Community Groups

Scriptrunner: Macro code broken after manipulating body with setBodyAsString

Edited

Hi all!

We want to strip out some divs that where accidentally added to the body content by the google translate browser extension. 

The stripping works great but other macros are broken because the hard-coded indentation within the macro xml that are interpreted as spaces at the beginning of the macro options values.

Is there a way to prevent this automatic modification?

 

Thanks a lot!

 

Philipp 

 


def SpaceKey = 'TPFR3';

import com.atlassian.confluence.pages.PageManager

import com.atlassian.confluence.spaces.SpaceManager

import com.atlassian.sal.api.component.ComponentLocator

import org.jsoup.Jsoup

import org.jsoup.*

import org.jsoup.nodes.*

def pageManager = ComponentLocator.getComponent(PageManager)

def spaceManager = ComponentLocator.getComponent(SpaceManager)

def targetSpace = spaceManager.getSpace(SpaceKey)

pageManager.getPages(targetSpace, true).each { page ->

log.debug "Inspecting page ${page.title}"

def body = page.bodyContent.body

def parsedBody = Jsoup.parse(body)

def macroBody = parsedBody.select("div.gtx-trans-icon")

log.debug "elements:" + macroBody

if (!macroBody.empty) {

log.debug "elements:" + macroBody

pageManager.saveNewVersion(page) { pageObject ->

macroBody.remove()

pageObject.setBodyAsString(parsedBody.toString())

}

}

 

Modified with spaces and indents 

Screenshot 2022-04-30 at 12.08.19.png

 

Orginal 

Screenshot 2022-04-30 at 12.07.05.png

 

Broken macro

Screenshot 2022-04-30 at 14.27.10.png

1 answer

1 accepted

0 votes
Answer accepted

I found the answer: prettyPrint has to be disabled.

paersedBody.outputSettings.prettyPrint(false)

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

297 views 0 9
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you