# 如何为代码块添加行号？

**URL:** https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970
**Category:** Support
**Created:** [2024年十月8日 22:31 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970 "2024-10-08T22:31:44Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![jmsachs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jmsachs/32/107682_2.png) [@jmsachs](https://meta.discourse.org/u/jmsachs)
#### Post date: [2024年十月8日 22:31 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970/1 "2024-10-08T22:31:44Z")

</div>

我在 2021 年找到了这篇帖子 [https://meta.discourse.org/t/is-it-possible-to-show-line-numbers-in-code-block/204178，但不知道如何应用。\n\n-](https://meta.discourse.org/t/is-it-possible-to-show-line-numbers-in-code-block/204178%EF%BC%8C%E4%BD%86%E4%B8%8D%E7%9F%A5%E9%81%93%E5%A6%82%E4%BD%95%E5%BA%94%E7%94%A8%E3%80%82%5Cn%5Cn-) Discourse 是否内置了此选项？如果是，我该如何在 Markdown 中指定？\n- 如果它不是 Discourse 内置的而是插件，那么关于此插件的信息在哪里？

---

<div class="post-metadata">

### Author: ![Unit\_72](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/unit_72/32/453674_2.png) [@Unit\_72](https://meta.discourse.org/u/Unit_72)
#### Post date: [2024年十月8日 22:35 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970/2 "2024-10-08T22:35:05Z")

</div>

_我问了人工智能，不确定这是否有帮助：_

要在 Discourse 的代码块中显示行号，你需要这样做：

**Discourse 没有内置的功能来显示代码块中的行号。** 但是，有一个插件可以帮助你实现这一目标。

该插件名为“Code Review”（以前称为“Code Line Numbers”）。要使用此插件，你需要：

1. **检查插件是否已安装** ：联系你的 Discourse 管理员或查看管理面板，以确定你的 Discourse 实例上是否已安装“Code Review”插件。

2. **安装插件** ：如果插件未安装，请让你的管理员从 Discourse 插件存储库安装它。或者，如果你可以访问管理面板，则可以自行安装。

3. **启用行号** ：安装插件后，你需要为代码块启用行号。你可以通过将 `line_numbers` 属性添加到代码块 Markdown 来实现。例如：

如果你仍然遇到问题，请随时提问，我会尽力帮助你！

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2024年十月8日 22:42 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970/3 "2024-10-08T22:42:51Z")

</div>

该主题包含他们使用的 CSS，您可以在自己的本地组件中进行试验。

请参阅此帖子及之后的内容：

> [@Is it possible to show line numbers in code block?](https://meta.discourse.org/t/is-it-possible-to-show-line-numbers-in-code-block/204178/5?u=merefield):
>
> I’ve been experimenting with line numbers for both wrapping and scrolling on the Docker forum. This is not live yet, but I’d like to know if someone has any comments. So, some screenshots first: Wrapped content Above, line numbers help identifying when one line stops and another starts. Of course, there are other ways to render that, but this topic is about line numbers. nerd_faceScrolled content With limited padding on the side(s) where content is hidden, to indicate that scr…

可惜的是，没有人将其打包成 GitHub 上的主题组件以便于使用？

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2024年十月8日 22:45 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970/4 "2024-10-08T22:45:22Z")

</div>

你试过了吗？我认为最好在发布之前检查一下人工智能提供的解决方案是否是真理。

---

<div class="post-metadata">

### Author: ![Unit\_72](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/unit_72/32/453674_2.png) [@Unit\_72](https://meta.discourse.org/u/Unit_72)
#### Post date: [2024年十月8日 22:46 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970/5 "2024-10-08T22:46:59Z")

</div>

我提到我问了人工智能，因为它似乎非常有帮助，而且在寻求帮助方面没有得到充分利用。虽然我对这个插件了解不多，但我仍然想提供帮助。

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2024年十月8日 22:48 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970/6 "2024-10-08T22:48:05Z")

</div>

AI 会产生幻觉，并且它可能依赖的旧信息会过时并失去相关性——因此值得检查。

---

<div class="post-metadata">

### Author: ![jmsachs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jmsachs/32/107682_2.png) [@jmsachs](https://meta.discourse.org/u/jmsachs)
#### Post date: [2024年十月8日 22:48 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970/7 "2024-10-08T22:48:36Z")

</div>

哪个AI？ _(为满足10个字符的最低要求而添加的免费内容_)

---

<div class="post-metadata">

### Author: ![jmsachs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jmsachs/32/107682_2.png) [@jmsachs](https://meta.discourse.org/u/jmsachs)
#### Post date: [2024年十月8日 22:51 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970/9 "2024-10-08T22:51:50Z")

</div>

![image](https://global.discourse-cdn.com/meta/original/4X/f/4/2/f42281d01ec9bbd1ce4aaa84741a036402643661.png)

啊，好吧……看来大家都在追赶AI的潮流了，😕

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2024年十月8日 23:09 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970/11 "2024-10-08T23:09:32Z")

</div>

编辑：好的，我快速制作了一个主题组件。稍后我会添加一些设置来控制背景和文本颜色。

> [@代码块行号](https://meta.discourse.org/t/code-block-line-numbers/330130):
>
> information_source摘要 为帖子中的多行代码块添加行号eyeglasses预览 [在 Discourse 主题创建器中预览](https://discourse.theme-creator.io/theme/Lilly/codeblock-line-numbering)hammer_and_wrench仓库 [https://github.com/Lillinator/code-block-line-numbers](https://github.com/Lillinator/code-block-line-numbers)question安装指南 [如何安装主题或主题组件](https://meta.discourse.org/t/how-do-i-install-a-theme-or-theme-component/63682)open_book初次使用 Discourse 主题？ [Discourse 主题使用入门指南](https://meta.discourse.org/t/beginners-guide-to-using-discourse-themes/91966) 安装此主题组件 简介 这是一个简单的主题组件，可为 Discourse 帖子中的代码块添加行号。它不会为内联代码、少于 2 行的代码块或引用的代码块添加行号。同时，它也会忽略 GitHub onebox 中的代码。此外，该组件还允许你更改字体大小，并添加一个标题，该标题将显示在代码语言之前（如果它在第一个代码围栏后指定）。 感谢 @Don 在此方面也做出的出色工作。 white_check_mark 适用于桌面端和移动端视图。 desktop_computer …

 ![该图像显示了一个基于文本的界面，其中包含用于 JavaScript 函数的代码片段，展示了 for 循环和事件监听器基类型的使用。（由 AI 题注）](https://global.discourse-cdn.com/meta/original/4X/f/0/0/f002fed5fd07bcef1c99351e6ec7dcb2a8716ee4.png)

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2024年十月14日 17:56 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970/13 "2024-10-14T17:56:06Z")

</div>

3 篇帖子已合并到现有主题：[代码块行号](https://meta.discourse.org/t/code-block-line-numbers/330130/6)

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2024年十月14日 17:37 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970/16 "2024-10-14T17:37:41Z")

</div>

帖子已合并到现有主题：[代码块行号](https://meta.discourse.org/t/code-block-line-numbers/330130/6)

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2024年十一月13日 17:56 UTC](https://meta.discourse.org/t/how-to-add-line-numbers-to-code-blocks/329970/17 "2024-11-13T17:56:41Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
