// ==UserScript== // @name Confluence Auto Numbered Headings // @namespace https://gist.github.com/elahd/28f64feddd9ece56f4f0566d195d0cbd // @version 0.3 // @description Adds numbered headings button to the page editor in Atlassian Confluence. Based on work by Markus Jenu at https://community.atlassian.com/t5/Confluence-questions/Is-it-possible-to-add-numbering-to-headings-in-Confluence/qaq-p/315517#M87046. // @author Elahd Bar-Shai // @match https://*.atlassian.net/wiki/spaces/* // @match https://*.atlassian.net/wiki/spaces/* // @require http://code.jquery.com/jquery-3.4.1.min.js // @require https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012 // @grant none // @downloadURL none // ==/UserScript== (function() { 'use strict'; var jQuery = window.jQuery; var AJS = window.AJS; function addIndex() { var indices = []; jQuery("#wysiwygTextarea_ifr").contents().find("h1,h2,h3,h4,h5,h6").each(function(i,e) { 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); } function createButton () { AJS.toInit(function(){ AJS.$('#rte-toolbar > div.aui-toolbar2-primary.toolbar-primary').append('') AJS.$("#addIndex").click(function(e) { e.preventDefault(); addIndex(); }); }); } waitForKeyElements ("form#editpageform.editor.aui", createButton); })();