如何在 Confluence 中获得自动编号的标题?

发布于 2025-01-19 05:22:37 字数 74 浏览 3 评论 0 原文

目前,Confluence 无法自动对文档结构的标题进行编号。我知道有(付费)第三方插件可用。

如何实现连续编号的标题?

Currently it is not possible in confluence to have the headings of the document structure numbered automatically. I am aware that there are (paid) 3rd party plugins available.

How can I achieve continuous numbered headings?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱人如己 2025-01-26 05:22:37

TL;DR

为以下 javascript 创建书签,然后在 confluence 编辑模式下单击它以重新编号标题。

javascript:(function()%7Bfunction%20addIndex()%20%7Bvar%20indices%20%3D%20%5B%5D%3BjQuery(%22.ak-editor-content-area%20.ProseMirror%22).find(%22h1%2Ch2%2Ch3%2Ch4%2Ch5%2Ch6%22).each(function(i%2Ce)%20%7Bvar%20hIndex%20%3D%20parseInt(this.nodeName.substring(1))%20-%201%3Bif%20(indices.length%20-%201%20%3E%20hIndex)%20%7Bindices%3D%20indices.slice(0%2C%20hIndex%20%2B%201%20)%3B%7Dif%20(indices%5BhIndex%5D%20%3D%3D%20undefined)%20%7Bindices%5BhIndex%5D%20%3D%200%3B%7Dindices%5BhIndex%5D%2B%2B%3BjQuery(this).html(indices.join(%22.%22)%2B%22.%20%22%20%2B%20removeNo(jQuery(this).html()))%3B%7D)%3B%7Dfunction%20removeNo(str)%20%7Blet%20newstr%20%3D%20str.trim()%3Bnewstr%20%3D%20newstr.replace(%2F%5B%5Cu00A0%5Cu1680%E2%80%8B%5Cu180e%5Cu2000-%5Cu2009%5Cu200a%E2%80%8B%5Cu200b%E2%80%8B%5Cu202f%5Cu205f%E2%80%8B%5Cu3000%5D%2Fg%2C'%20')%3Bif(IsNumeric(newstr.substring(0%2Cnewstr.indexOf('%20'))))%7Breturn%20newstr.substring(newstr.indexOf('%20')%2B1).trim()%3B%7Dreturn%20newstr%3B%7Dfunction%20IsNumeric(num)%20%7Bnum%20%3D%20num.split('.').join(%22%22)%3Breturn%20(num%20%3E%3D0%20%7C%7C%20num%20%3C%200)%3B%7DaddIndex()%7D)()

结果

初始结构
编号结构

如何使用

对结构进行更改后,单击已添加书签的 javascript 会对文档重新编号。

局限性在于它仅提供 nnn 编号,但对于许多情况来说这已经足够了。该脚本也可以根据需要进行定制。

背景、解释和披露

我尝试了这个 TaperMonkey 脚本,显然结果是来自 这篇文章,但它没有按原样工作。所以我拿了它的 源代码 并剥离了集成代码、旧版本兼容性,并做了一些小改动调整以获得这个:(

function addIndex() {
        var indices = [];

            jQuery(".ak-editor-content-area .ProseMirror").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);
    }
    
addIndex();

我不是 JavaScript 开发人员,我确信它可以写得更好/更好)

然后我使用了 bookmarklet 将其转换为顶部的 JavaScript 书签,点击即可触发该功能。

TL;DR

Create a bookmark for the following javascript and click it in edit mode in confluence to renumber your headings.

javascript:(function()%7Bfunction%20addIndex()%20%7Bvar%20indices%20%3D%20%5B%5D%3BjQuery(%22.ak-editor-content-area%20.ProseMirror%22).find(%22h1%2Ch2%2Ch3%2Ch4%2Ch5%2Ch6%22).each(function(i%2Ce)%20%7Bvar%20hIndex%20%3D%20parseInt(this.nodeName.substring(1))%20-%201%3Bif%20(indices.length%20-%201%20%3E%20hIndex)%20%7Bindices%3D%20indices.slice(0%2C%20hIndex%20%2B%201%20)%3B%7Dif%20(indices%5BhIndex%5D%20%3D%3D%20undefined)%20%7Bindices%5BhIndex%5D%20%3D%200%3B%7Dindices%5BhIndex%5D%2B%2B%3BjQuery(this).html(indices.join(%22.%22)%2B%22.%20%22%20%2B%20removeNo(jQuery(this).html()))%3B%7D)%3B%7Dfunction%20removeNo(str)%20%7Blet%20newstr%20%3D%20str.trim()%3Bnewstr%20%3D%20newstr.replace(%2F%5B%5Cu00A0%5Cu1680%E2%80%8B%5Cu180e%5Cu2000-%5Cu2009%5Cu200a%E2%80%8B%5Cu200b%E2%80%8B%5Cu202f%5Cu205f%E2%80%8B%5Cu3000%5D%2Fg%2C'%20')%3Bif(IsNumeric(newstr.substring(0%2Cnewstr.indexOf('%20'))))%7Breturn%20newstr.substring(newstr.indexOf('%20')%2B1).trim()%3B%7Dreturn%20newstr%3B%7Dfunction%20IsNumeric(num)%20%7Bnum%20%3D%20num.split('.').join(%22%22)%3Breturn%20(num%20%3E%3D0%20%7C%7C%20num%20%3C%200)%3B%7DaddIndex()%7D)()

Result

initial structure
numbered structure

How to use

After changes to the structure have been made, clicking the bookmarked javascript renumbers the document.

Limitations are that it only provides n.n.n. numbering, but for many cases that's sufficient. The script can also be customized as required.

Background, explanation and disclosure

I tried this TaperMonkey script that apparently resulted from this post, but it didn't work as is. So I took its source code and stripped it of the integration code, old version compatibility and made some minor adjustments to get this:

function addIndex() {
        var indices = [];

            jQuery(".ak-editor-content-area .ProseMirror").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);
    }
    
addIndex();

(I'm not a JavaScript developer, I'm sure it can be written nicer/better)

Then I used bookmarklet to convert it into the javascript bookmark at the top, which can be clicked to trigger the functionality.

瞎闹 2025-01-26 05:22:37

基于小书签方法,我创建了一个 Chrome 扩展,它将在上下文菜单中添加此功能并将脚本注入页面中。
请参阅https://tdalon.blogspot.com/2024/03 /crx-confluence-numbered-headings.html 以及 GitHub https://github.com/tdalon/confluence_crx

这避免了设置书签的麻烦,并使功能也更容易访问。
除此之外,Chrome 扩展程序还有一些其他不错的功能,例如按标签快速组合搜索。

Based on the bookmarklet approach, I have created a Chrome Extension that will add this functionality in a Context Menu and inject the script in the page.
See https://tdalon.blogspot.com/2024/03/crx-confluence-numbered-headings.html and the source code available in GitHub https://github.com/tdalon/confluence_crx

This avoids the trouble to setup the bookmarklets and make the functionality also better accessible.
Besides the Chrome Extension has some other nice functionalities like quick combined search by labels.

月依秋水 2025-01-26 05:22:37

启发者: https://2ality.com/2aly.com/2lation.com/2012/2012/01/numbering-headingsheadingshtml.html.html.html.html.html.html.html.html.html.html.html.html.html.html.html.html.html.html

这是一个CSS,您可以在Confluence Look中应用于自动编号的感觉/样式表。

.wiki-content h1 {
  counter-increment: h1counter;
  counter-reset: h2counter;
}
.wiki-content h1::before {
  content: counter(h1counter) ".\0000a0\0000a0";
}
.wiki-content h2 {
  counter-increment: h2counter;
  counter-reset: h3counter;
}
.wiki-content h2::before {
  content: counter(h1counter) "." counter(h2counter) ".\0000a0\0000a0";
}
.wiki-content h3 {
  counter-increment: h3counter;
  counter-reset: h4counter;
}
.wiki-content h3::before {
  content: counter(h1counter) "." counter(h2counter) "." counter(h3counter) ".\0000a0\0000a0";
}
.wiki-content h4 {
  counter-increment: h4counter;
}
.wiki-content h4::before {
  content: counter(h1counter) "." counter(h2counter) "." counter(h3counter) "." counter(h4counter) ".\0000a0\0000a0";    }

这样,您可以使TOC中的编号可见:

.toc-macro.hidden-outline span.toc-outline {
    display: inline;
}

Inspired by: https://2ality.com/2012/01/numbering-headingshtml.html

Here is a CSS you can apply in Confluence look&feel/stylesheet for auto numbering.

.wiki-content h1 {
  counter-increment: h1counter;
  counter-reset: h2counter;
}
.wiki-content h1::before {
  content: counter(h1counter) ".\0000a0\0000a0";
}
.wiki-content h2 {
  counter-increment: h2counter;
  counter-reset: h3counter;
}
.wiki-content h2::before {
  content: counter(h1counter) "." counter(h2counter) ".\0000a0\0000a0";
}
.wiki-content h3 {
  counter-increment: h3counter;
  counter-reset: h4counter;
}
.wiki-content h3::before {
  content: counter(h1counter) "." counter(h2counter) "." counter(h3counter) ".\0000a0\0000a0";
}
.wiki-content h4 {
  counter-increment: h4counter;
}
.wiki-content h4::before {
  content: counter(h1counter) "." counter(h2counter) "." counter(h3counter) "." counter(h4counter) ".\0000a0\0000a0";    }

With this, you can make the numbering visible in TOC:

.toc-macro.hidden-outline span.toc-outline {
    display: inline;
}
请叫√我孤独 2025-01-26 05:22:37

我们为Confluence构建了一个编号的标题应用程序。令人惊讶的是,即使您使它工作一次,当您更新/删除标题时会发生什么。没有人愿意为应用程序付费,包括我自己,但是如果您要处理公共页面,示例Confluence在技术文档中很受欢迎。

,但是只有3家公司,即2个公司有2个应用程序,价格有很大差异。

We have built an numbered heading app for Confluence. It's surprisingly complicated, even if you get it to work once, what happens when you update add/delete a heading. Nobody wants to pay for an app, myself included, but if you are dealing with public pages, example Confluence is popular for technical documentation..

5 apps on the Atlassian Marketplace, but only 3 companies, ie 2 have 2 apps and prices vary a lot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文