Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Scriptrunner: Macro code broken after manipulating body with setBodyAsString

suedti April 30, 2022

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

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
suedti May 1, 2022

I found the answer: prettyPrint has to be disabled.

paersedBody.outputSettings.prettyPrint(false)

TAGS
AUG Leaders

Atlassian Community Events