Editing Source Code in Confluence

Susan Vineyard December 3, 2015

We recently had to upgrade Confluence to 5.9.1 because of a bug in indexing that is supposed to be fixed in this version.  But then we discovered that  the source code editor we were using no longer works with that version. I used that editor to format our templates and create elements for CSS to format. We MUST have access to the html. Although we are allergic to plugins with a price, we may have to pay to find one that works. It's that important.  Does anyone have any suggestions....please....

2 answers

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

7 votes
Stephen Deutsch
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 4, 2015

Well, if you are willing to pay for it, I wouldn't turn it down, but there is an option for changing the source code without the Source Editor plugin.  It takes a few manual steps, but it's not so hard.

  1. On the page you want to change, go to the ••• menu and choose "View Storage Format"
  2. Copy and paste it into your favorite editor (I like Notepad++)
  3. Make the changes you want
  4. (NO LONGER NECESSARY) Remove all line breaks (i.e. make it so the whole thing is on one line).  In Notepad++, you can do a search and replace for \r\n (don't forget to choose "Extended") and make sure that the replace box is empty and choose "replace all"
  5. (NO LONGER NECESSARY) Do another search and replace and replace all " characters with \"
  6. Copy that and paste it over the <PASTE CODE HERE> part of this script (but leave the backticks `` intact):

    var newBodyText = `<PASTE CODE HERE>`;
    jQuery.ajax({
        url: contextPath + "/rest/api/content/" + AJS.params.pageId + "?expand=body.storage,version,ancestors",
        success: function(response) {
            response.body.storage.value = newBodyText;
            response.version.number += 1;
            jQuery.ajax({
                contentType: 'application/json',
                type: 'PUT',
                url: contextPath + "/rest/api/content/" + AJS.params.pageId,
                data: JSON.stringify(response),
                success: function(response) {
                    console.log(response);
                    console.log("Success!");
                },
                error: function(response) {
                    console.log(response);
                    console.log("Error!");
                }
            });
        }
    });
  7. While you are on the page you want to change the code for, open up the browser console (Often by pressing F12 or Shift+Ctrl+I).
  8. Paste that new script into the browser console, and press enter.
  9. If everything went well, you should see Success!, and if you refresh the page, you should see your changes.

This would actually work in Confluence Cloud as well.

EDIT: Modern browsers let you make multiline strings using backticks (`) which makes steps 4 and 5 unnecessary.

Maarten Joosen December 4, 2017

Nice, simple en to the point

Nicky van Steensel van der Aa December 4, 2017

Gemoji image for :thumbsup

Philip Schlesinger January 2, 2018

Can't give this enough up-votes

1 vote
Mohammed Davoodi
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 13, 2016

Our organization offers a paid plugin, Source Editor for Confluence to edit the source of Confluence pages. We support both server and cloud. 

Let me know if you have any questions or feedback about the plugin.

Mohammed

TAGS
AUG Leaders

Atlassian Community Events