How to change text on confluence page via content action

Hello everyone

I’m quite new to confluence.
One of my first ideas was to change the CSS of the text so the headings would be more visibly different. This apparently was not so easy to do (not possible?).
Now I’m trying to add numbers (1, 1.1, 2, 2.1, 2.2, …) Infront of the titles.
I can’t seem to find how to change text via a forge script.

Do you know if this is possible and have examples?

Kind regards
Sam Rotthier

Hi Sam, it’s possible to edit the text of Confluence pages.

In fact, there is already an app doing exactly that: https://marketplace.atlassian.com/apps/16063/numbered-headings

1 Like

Hi Corentin, thank you for the info.

Do you know if there is example code somewhere so i could make a sort of inspired app?

Sure. Here is some sample code to replace text in typescript

import { traverse } from "@atlaskit/adf-utils/traverse";
import { ADFEntity } from "@atlaskit/adf-utils/types";

export const executeTextReplacement = (document: ADFEntity): ADFEntity => {
    const transformedDocument = traverse(document, {
        text: (node: ADFEntity) => {
            if (typeof node.text === "string") {
                return {
                    ...node,
                    text: "REPLACED",
                };
            }
            return node;
        },
    });

    return transformedDocument;
};

High level steps would be:

  1. Fetch the page content

GET /pages/{id}?body-format=atlas_doc_format

  1. Parse the response ADF
  2. Update the ADF (Examples – Atlaskit might help you)
  3. Update the page

PUT /pages/{id}

Something similar to this

    const updatePageRequestBody = {
        id,
        title,
        body: {
            representation: "atlas_doc_format",
            value: JSON.stringify(newAdf),
        },
        status: "current",
        version: {
            number: (oldPage?.version?.number ?? 0) + 1,
            message: "Updated by app",
        },
    };

    const updateResponse = await PageApi.updatePage(
        {
            id,
            updatePageRequest: updatePageRequestBody,
        },
        options,
    );