Greasy Fork

Greasy Fork is available in English.

Confluence Auto Numbered Headings

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.

当前为 2020-03-20 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Confluence Auto Numbered Headings
// @namespace    https://gist.github.com/elahd/28f64feddd9ece56f4f0566d195d0cbd
// @version      0.5
// @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      http://greasyfork.icu/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// @grant        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]++;
            jQuery(this).html(indices.join(".")+". " + removeNo(jQuery(this).html()));
        });
    }

    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(){
            jQuery('#rte-toolbar > div.aui-toolbar2-primary.toolbar-primary').append('<ul class="aui-buttons rte-toolbar-group-link"><li class="toolbar-item" data-tooltip="Auto-Number Headings" id="addIndex"><a class="toolbar-trigger aui-button aui-button-subtle" href="#" ><span class="aui-icon aui-icon-small aui-iconfont-table-of-contents">Auto-Number Headings</span></a></li></ul>')
            jQuery("#addIndex").click(function(e) {
                e.preventDefault();
                addIndex();
            });
        });
    }

    // Wait for editor to load before creating button.
    waitForKeyElements ("form#editpageform.editor.aui", createButton); //For editing existing pages.
    waitForKeyElements ("form#createpageform.editor.aui", createButton); //For creating new pages.
})();