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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,460,050
Community Members
 
Community Events
176
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

Atlassian Community Events