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:
- Fetch the page content
GET /pages/{id}?body-format=atlas_doc_format
- Parse the response ADF
- Update the ADF (Examples – Atlaskit might help you)
- 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,
);