DiscoTOC - 自动目录

:discourse2: Summary DiscoTOC will allow you to generate an interactive table of contents for your topics with one click!
:eyeglasses: Preview Preview on Discourse Theme Creator
:hammer_and_wrench: Repository Link https://github.com/discourse/DiscoTOC
:open_book: New to Discourse Themes? Beginner’s guide to using Discourse Themes

Install this theme component

Samples

Desktop

Mobile

Features

toc = table of contents

  • Automatically generates the entire toc via a button in the composer gear menu

  • The toc will always be on the screen - scrolls with content like the topic progress widget

  • As you scroll past sections in the topic, the active element in the table of contents will be set to active (blue highlight)

  • adds id attributes to headings (you can link to a specific section from another topic / post)

  • clicking on any link in the toc will instruct the browser to navigate to relevant section (smooth-scrolling)

  • adds a copy-able link next to each heading (if you want to link to it)

  • RTL support

  • The colors are based on your current active color palette

How does it work?

In a nutshell, it looks for headings in topics which are marked to have a toc (via the composer button) and if it turns out the current topic is marked, then it takes all the headings and puts them in the toc (nested in order of heading levels) - this means that your markdown must be syntactically correct.

# heading 1
## heading 2
### heading 3
#### heading 4
##### heading 5
###### heading 6

You’re free to go back and fourth in heading levels, but the order must be correct

# heading 2 
## heading 3
## heading 3
### heading 4
## heading 3
# heading 2

etc...

In order for the links in the toc to work, headings must have id attributes. The component will check if the headings already have ids and if they do, then they are respected. This is handy if you ever manually created a toc.

If the headings don’t have ids, then it will generate an id for each heading based on its text (unwanted characters are stripped out)

once all of that is done, it will also add a link next to each button that links directly to that section:

Settings

Name Description
minimum trust level to create TOC The minimum trust level a user must have in order to see the TOC button in the composer
composer toc text Text that appears at the top of the preview pane of the composer to indicate the topic will have a table of contents
auto TOC categories Automatically enable TOC on topics in these categories
auto TOC tags Automatically enable TOC on topics with these tags
TOC min heading Minimum number of headings in a topic for the table of contents to be shown

Translations

Translation Default
table_of_contents table of contents
insert_table_of_contents Insert table of contents
jump_bottom Jump to end
toggle_toc.show_timeline Timeline
toggle_toc.show_toc Contents

The theme comes with three strings that you can translate or change.

table_of_contents: "table of contents"

this used for the button that opens the toc on mobile

insert_table_of_contents: "Insert table of contents"

this is used as the text for the toc button in the composer gear menu

topic_will_contain_a_table_of_contents: "This topic will contain a table of contents"

This is the text that shows up in the composer preview to indicate that the a toc will be generated for the topic

How do I create a toc?

  1. Write a topic with syntactically correct headings
  2. Click the toc button in the gear menu (only shows up when creating a regular topic - replies and PMs are ignored
  3. Profit.

What happens to the topic progress widget when a topic has a toc?

As you can probably guess, there’s no space to show both at the same time, so the way this component works is as follows

in a topic with a toc, the topic progress widget is hidden while the first post is on screen, and you see the toc instead.

Once you scroll past the first post, the toc will not scroll with you and the topic progress will be shown instead while you read any replies.

So, first posts get the toc, and subsequent posts get the regular topic progress widget.

The happens on both desktop and mobile.

Are there any downsides to using this component?

Nothing I am aware of, all the changes are done on the client-side. So you can easily remove the component and your posts would go back to the way they were before you installed it.

Limitations

This component assumes the standard topic layout. As such, it won’t work with themes that modify that layout such as the Vincent theme. Support for popular themes that modify the layout will come at a later stage in the form of component settings.

Credit

I started with Greg Franko’s tocify.js library. However, it looks like it’s not been updated in a while, so this is essentially a hard-fork that removes a lot of unnecessary features, integrates and styles the rest for Discourse.

So, there are no external requests and the total size is ~ 4kb gzip.

Big thanks to @erlend_sh for lots of valuable feedback and to @david for his help with translations.

:discourse2: Hosted by us? Theme components are available to use on our Pro, Business and Enterprise plans.

Last edited by @tobiaseigen 2025-06-24T03:17:08Z

Check documentPerform check on document:
181 个赞

4 篇帖子已拆分为新主题:如何将目录移到帖子左侧?

我不知道这个组件是如何实现的,对 Discourse 的前端结构也不太了解,所以我只能猜一下。

进度条是否可以只在 a) 主题中有多于 1 个帖子时显示,并且 b) 将其起点调整为从第 2 个帖子开始(而不是第 3 个),同时 c) 在两个元素中的一个元素上添加一些舒适的底部/顶部边距,以确保另一个元素有足够的距离(例如 1vh),以免看起来奇怪?

换句话说,不要使用整个第 2 个帖子作为间隙,而是使用 CSS 在它们之间留出一些空间(如果存在多于 1 个帖子)。

再说一遍,这可能完全没有意义,因为我对目前的工作方式了解不多。

3 个赞

您好!我们最近为论坛安装了 DiscoTOC,想知道是否可以让该组件读取图片中的 alt 文本?我们使用图片作为一些补丁说明的标题……

如下所示:
Performance and Stability

不幸的是,目录系统似乎无法解析图片作为标题,在列表中创建了一个空白条目,并创建了一个链接,该链接会将您带到一个空白页面。除了“不要使用图片”之外,还有没有其他解决方法?谢谢!除此之外,我们非常喜欢这个系统。

1 个赞

我猜解决方案是不将图片用作标题,但也许可以通过向您的网站添加一些代码来挂接到 DiscoTOC 代码中来实现。是否值得研究这一点,将取决于您使用帖子标题中的图片对您有多重要。

3 个赞

我们有一段时间以来一直在补丁说明中一致地使用图片作为标题,这是我们品牌和展示的一部分;不仅在论坛上,在 Steam 等地方也是如此。我们希望能够在继续使用 DiscoTOC 的同时继续使用图片作为标题,以保持一致性。

DiscoTOC 在其他方面也很棒,例如 AMA 总结、关于我们专用服务器启动器应用的综合帖子、新玩家指南等。我们非常喜欢这个系统,但希望在展示补丁说明的方式上能增加一点额外功能。

1 个赞

此组件的标题锚点功能与 2.7.0beta6 中添加的 自动标题链接 功能略有冲突,因为标题在悬停时会出现两个图标,一个来自 Discourse,一个来自 DiscoTOC。有没有解决的办法?

1 个赞

您好,

您可以通过以下方式隐藏 自动标题链接 锚点:

.anchor {
  display: none;
}

您好 dodesz,

我将帖子宽度设置得比默认值大得多,安装此组件后看起来有些不对劲,您能告诉我如何解决这个问题吗?

:heart: 谢谢!

Selection_839

1 个赞

在运行 Discourse 2.8.0.beta4(90232af778)的论坛上,启用 DiscoTOC 组件会导致错误消息:

oops

该组件以前也曾被激活,并且在之前安装的 Discourse 版本上也曾引发过问题,尽管我无法确定具体是哪个版本。

您能在您网站的错误日志中找到与该问题相关的任何错误消息吗?

该错误消息是后端错误,而 DiscoTOC 是一个前端主题组件,因此它们之间很难有关系。您是否安装了任何插件?

1 个赞

很遗憾,我在 /logs 中没有找到任何有用的信息。

我安装了,这是 app.yml 中相关的摘录:

hooks:
  after_code:
    - exec:
        cd: $home/plugins
        cmd:
          - git clone https://github.com/discourse/docker_manager.git
          - git clone https://github.com/discourse/discourse-openid-connect.git
          - git clone https://github.com/discourse/discourse-checklist.git
          - git clone https://github.com/discourse/discourse-push-notifications.git
          - git clone https://github.com/discourse/discourse-characters-required.git
          - git clone https://github.com/angusmcleod/discourse-news.git
          - git clone https://github.com/discourse/discourse-data-explorer.git
          - git clone https://github.com/DNOeV/discourse-watch-category.git
          - git clone https://github.com/discourse/discourse-footnote.git
          - git clone https://github.com/discourse/discourse-knowledge-explorer.git
1 个赞

当标题位于引用块内时,该标题不会显示在目录 (TOC) 中。能否更改此行为?

此标题不会显示在 TOC 中

引用的内容

此标题会显示在 TOC 中

引用的内容

我不知道它是如何计划工作的,但通常不会,因为它属于引用的一部分,而不是该文本的标题。

1 个赞

您能否尝试改用 HTML <blockquote> 标签?这样可以将标题 # 放在一行的开头。

例如:

<blockquote>

### 锚点标题

</blockquote>

锚点标题

我没有在目录(TOC)中尝试过,但它似乎在常规帖子中与自动锚点标题一起有效。

您为什么希望标题在目录中以引号显示?您的使用场景是什么?

感谢您的建议。但对我来说没有效果。

这是我在“问题领域:年龄”下使用引用来直观地构建内容的一个示例

https://hub.youthpowercoalition.org/t/a-progressives-style-guide-toward-harnessing-language-in-support-of-intersectionality-and-cross-sector-power-building/285

3 个赞

你为什么那样使用引用? 告知来源就足够了。 此外,从语法上讲,那在英语中也是错误的。

这是个错误还是只是另一个用户,但是……我该如何关闭目录(TOC)?我正在寻找关于最终用户如何使用私人消息的一些基本说明,当然,我去了新用户文档并打开了目录,看看是否有任何信息。

我使用的是 iPad 和 DiscourseHub。

我看到了这个:

kuva

目录(TOC)看起来没问题。但它覆盖了文本,我无法让它再次隐藏。所以,我到底做错了什么,或者根本没做错?:pleading_face: