Is it possible to add numbering to headings in Confluence?

Morning,

In Confluence we have headings and we have numbered lists.

Is it possible to combine the two (and then use them as part of a template)?

And to have the numbering working automatically?

The idea is that this would work as it does in Word. So once you start using numbered headings, all subsequent headings get numbered both automatically and down a level each time. For example:

Heading 1 = 1. A Heading

<some text>

Heading 2 = 2.1 Help

<some text>

Heading 2 = 2.2 I Need

<some text>

Heading 2 = 2.3 Your Help

<some text>

Etc, etc, so everytime a new heading level was added, the numbering also changed to accommodate that.

Does this make sense?

Cheers.

7 answers

1 accepted

There's a third-party plugin for that: https://marketplace.atlassian.com/16063

Amalia, thanks for pointing me in the right direction. :)

But it's not available for OnDemand instances :(

The add-on now has a stable OnDemand counterpart, you can find it here.

We recently made some changes to Numbered Headings. Confluence Server users can now choose between a Free and a Pro version of Numbered Headings.

You can read about the differences here: https://medium.com/numbered-headings/numbered-headings-free-vs-pro-9c8fcc545afa

The situations is not changing for Confluence Cloud users. They still have access to the same Numbered Headings Cloud.

For information on how to install Numbered Headings Free, Pro or Cloud, visit our documentation here: https://addons.avisi.com/numbered-headings/documentation/

Engineering Essentials has just been released to the public.

This resolves the issue of numbered headings space-wide, without needing to put your content inside a macro.

With Engineering Essentials it is now possible to configure an entire space to have numbered headings. Any enabled space will instantly have it's headings numbered. No additional editing of a page is required. This enforces a consistent look and feel within each space. Additionally, because your content does not need to be wrapped in a macro it does not interfere with page differences, change emails, exports, or other plugins. Ensuring your content will continue to be available into the future, regardless of what plugins you have installed. This simplifies export templates and improves compatibility by defining the correct layout for the particular format within your own templates.

As numbered headings are often required for formal documentation we've also provided the ability to separately force version comments on all changes within a space. Enabling fully automatic change history documentation.

With years of experience supporting Confluence in an engineering environment, Precision Plugins have big plans to plug some small gaps, helping Confluence and other extensions be the best environment for your formal documentation.

Checkout the Engineering Essentials package and let us know what you think here:
https://marketplace.atlassian.com/plugins/com.precisionplugins.engineeringessentials

Here's the OnDemand version of the plugin (I can't edit Amalia's answer)

https://marketplace.atlassian.com/plugins/nl.avisi.confluence.plugins.numberedheadings-connect

Currently it has some issues but I hope they get resolved in near future

Since then both the add-on and the OnDemand platform have seen vast improvements. Check our issue tracker for the current status: http://addons.avisi.com/confluence/numbered-headings/issues.html

We've recently updated our add-ons website. Anyone looking for help on Numbered Headings can find it here: http://addons.avisi.com/numbered-headings/help/

Hi All,

It seems like a very very long pending issue with Confluence.

I found something that sort of works

http://philarcher.org/diary/2011/numbering/

The script works really really well, except the headings start at 3 and everything (even things in the sidebar) get numbered. It will be awesome if someone can figure out how to get this working. I know that there are free plugins, but they clutter up the document and remove the clarity. If we get this working, we can simply put this into <head> and then bingo, everything is right the way it should be.

window.onload = function () {
  var elements=document.getElementsByTagName('*');
  var numbers=[0,0,0,0,0,0,0];
  for (var i in elements) { 
    var e=elements[i];
    if(!e||!e.tagName||!e.tagName.match(/^H([1-6])$/)){continue}
    var eLevel=RegExp.$1;
    var txt='';
    numbers[eLevel]++;
    for(var l=1;l&lt;=6;l++) {
      if(l&lt;=eLevel){
        txt+=numbers[l]+'.'
      } else {
        numbers[l]=0;
      }
    }
    e.textContent=txt+' '+e.textContent.replace(/^[0-9\.\s]+/,'');
  }
}

To try this,

Put the current script in the HTML macro at the end of the page.

I think we need to change this line

var elements=document.getElementsByTagName('*');

The document should be replaced by something that confluence knows as 'page contents'. Please see if you guys can solve this!

I created a script (copied some code from https://stackoverflow.com/questions/5127017/automatic-numbering-of-headings-h1-h6-using-jquery ) you can use in Confluence editor. You can add the script to usermacro and add the macro to header via space tools -> look and feel.

Hotkey hh and a button in the editor.

<script>

AJS.whenIType('hh').execute(function () { //hotkey
addIndex();
});

function addIndex() {
var indices = [];
jQuery("#wysiwygTextarea_ifr").contents().find("h1,h2,h3").each(function(i,e) { //.find("h1,h2,h3,h4,h5,h6") if you want all headings numbered
var hIndex = parseInt(this.nodeName.substring(1)) - 1;
if (indices.length - 1 > hIndex) {
indices= indices.slice(0, hIndex + 1 );
}
if (indices[hIndex] == undefined) {
indices[hIndex] = 0;
}
indices[hIndex]++;
if(jQuery(this).text().indexOf(indices.join(".")+" ") <0 ){
jQuery(this).prepend(indices.join(".")+" ");
}
});
}

AJS.toInit(function(){
AJS.$('#rte-toolbar > div.aui-toolbar2-primary.toolbar-primary').append('<ul class="aui-buttons"><li class="aui-button" id="addIndex">Number headings</li></ul>')
AJS.$("#addIndex").click(function(e) {
e.preventDefault();
addIndex();
});
});

</script>

Hello Markus, just wanted to thank you. Works great!

Few more tweaks:

  1. Removes old heading numbers
    1. Removes all numbers from the start of the heading (including dates for example)
  2. does not lose inline comments or inline macros
function addIndex() {
var indices = [];
jQuery("#wysiwygTextarea_ifr").contents().find("h1,h2,h3").each(function(i,e) { //.find("h1,h2,h3,h4,h5,h6") if you want all headings numbered
var hIndex = parseInt(this.nodeName.substring(1)) - 1;
if (indices.length - 1 > hIndex) {
indices= indices.slice(0, hIndex + 1 );
}
if (indices[hIndex] == undefined) {
indices[hIndex] = 0;
}
indices[hIndex]++;
var lapset=[];
jQuery(this).children().each(function(){
var text = AJS.$(this).prop('outerHTML');
lapset.push(text);
});
jQuery(this).html(indices.join(".")+". " + removeNo(jQuery(this).text()));
jQuery(this).append(lapset.join(""));

});
}

function removeIndex(){
jQuery("#wysiwygTextarea_ifr").contents().find("h1,h2,h3,h4,h5,h6").each(function(i,e) {
var lapset=[];
jQuery(this).children().each(function(){
var text = AJS.$(this).prop('outerHTML');
lapset.push(text);
});
jQuery(this).text(removeNo(jQuery(this).text()));
jQuery(this).append(lapset.join(""));
});
}

function removeNo(str) {
let newstr = str.trim();
newstr = newstr.replace(/[\u00A0\u1680​\u180e\u2000-\u2009\u200a​\u200b​\u202f\u205f​\u3000]/g,' ');
if(IsNumeric(newstr.substring(0,newstr.indexOf(' ')))){
return newstr.substring(newstr.indexOf(' ')+1).trim();
}
return newstr;
}

function IsNumeric(num) {
num = num.split('.').join("");
return (num >=0 || num < 0);
}

I noticed it also added some dots to the numbering that weren't there before.

Thanks a lot again.

Btw I call the function removeIndex() right before addIndex(). I hope that's the intended usage.

I have a dedicated button for the removeIndex() function that I can use if I want to remove the numbers from headings. You don't have to call the function before addIndex, the addIndex removes the numbers too.

jQuery(this).html(indices.join(".")+". " + removeNo(jQuery(this).text()));
AJS.$('#rte-toolbar > div.aui-toolbar2-primary.toolbar-primary').append('<ul class="aui-buttons"><li class="aui-button aui-button-compact" id="addIndex">Number headings</li><li class="aui-button aui-button-compact" id="removeIndex">remove index</li></ul>')
AJS.$("#addIndex").click(function(e) {
e.preventDefault();
addIndex();
});
AJS.$("#removeIndex").click(function(e) {
e.preventDefault();
removeIndex();
});

Alright, I see it now. Thank you.

0 vote

Since all answers here either suggest paid plugins or extensive use of javascript macros I wanted to note that numbering headings can easily be done with a stylesheet (which can be applied globally or space-wide depending on your preferences) using css counters:

#content.page.view {
     /* Set "section" to 0 */
    counter-reset: section;
}

#content.page.view h1 {
     /* Set "subsection" to 0 */
    counter-reset: subsection;
}

#content.page.view h1:before {
    /* Increment "section" by 1 */
    counter-increment: section;
    content: counter(section) " ";
}

/* Continue this scheme as deep as you need */

#content.page.view h2 {
    counter-reset: subsubsection;
}

#content.page.view h2:before {
    /* Increment "subsection" by 1 */
    counter-increment: subsection;
    content: counter(section) "." counter(subsection) " ";
}

#content.page.view h3:before {
    counter-increment: subsubsection;
    content: counter(section) "." counter(subsection) "." counter(subsubsection) " ";
}

Hi, this doesn't work for me. I want to add section numbering in my PDF report. Where do I have to paste your css?

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Kesha Thillainayagam
Posted Apr 13, 2018 in Confluence

We want to hear how your non-technical teams are using Confluence!

Hi Community! Kesha (kay-sha) from the Confluence marketing team here! Can you share stories with us on how your non-technical (think Marketing, Sales, HR, legal, etc.) teams are using Confluen...

2,909 views 27 12
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you