# Discourse中代码块格式问题——红色文本和意外高亮

**URL:** https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315
**Category:** Support
**Tags:** code
**Created:** [2024年八月12日 18:09 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315 "2024-08-12T18:09:22Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![alex10](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex10/32/575949_2.png) [@alex10](https://meta.discourse.org/u/alex10)
#### Post date: [2024年八月12日 18:09 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/1 "2024-08-12T18:09:22Z")

</div>

我在 Discourse 中遇到了代码块格式化问题。在发布 Python 代码时，代码的某些部分（例如 URL 和路径）会显示为红色或以意外方式格式化，如下图所示：

 ![Screenshot 2024-08-12 at 10.57.15 AM](https://global.discourse-cdn.com/meta/original/4X/5/0/5/5058e6bc4c259ce755d3893896e45d2bfe380edd.png)

**问题描述**

- 当我使用三个反引号创建代码块发布 Python 代码时，文本的特定部分（如 URL 或路径中的下划线）会显示为红色或格式错误。
- 我尝试过转义字符、使用 HTML 实体以及通过不指定语言来移除语法高亮，但问题仍然存在。

**代码示例**

以下是导致问题的代码片段（Python）：

import degirum as dg  
zoo = dg.connect(dg.CLOUD, “[https://cs.degirum.com](https://cs.degirum.com)”, “”)  
model = zoo.load\_model(“mobilenet\_v2\_ssd\_coco–300x300\_quant\_n2x\_orca\_1”)  
result = model(“[https://docs.degirum.com/images/samples/TwoCats.jpg](https://docs.degirum.com/images/samples/TwoCats.jpg)”)  
display(result.image\_overlay)

**我尝试过的方法**

- 使用反斜杠转义下划线。
- 使用 HTML 实体替换下划线。
- 通过在代码块中不指定 `python` 来移除语法高亮。
- 将代码作为纯文本发布，不进行任何格式化。

**设置信息**

- 在设置中，以下是高亮显示的语言：
- bash, c, cpp, csharp, css, diff, go, graphql, ini, java, javascript, json, kotlin, lua, makefile, markdown, objectivec, perl, php, php-template, plaintext, python, python-repl, r, ruby, rust, scss, shell, sql, swift, typescript, xml, yaml, wasm

**寻求支持**

- 是否有其他人遇到过此问题？
- 有没有推荐的方法可以防止 Discourse 错误解析代码的某些部分？
- 是否有任何设置或配置我应该检查，这些设置或配置可能会影响代码块的渲染？

---

<div class="post-metadata">

### Author: ![bryce](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bryce/32/377822_2.png) [@bryce](https://meta.discourse.org/u/bryce)
#### Post date: [2024年八月12日 19:02 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/2 "2024-08-12T19:02:01Z")

</div>

我对整个语法高亮系统/设置不太熟悉，但如果你只是想去掉红色文本，可以在你的网站主题中使用 CSS：

```css
code.language-python span {
    color: inherit;
}

```

红色用于指示字符串，所以如果你只想将它更改为其他颜色，可以这样做：

```css
code.language-python span.hljs-string {
    color: #FFF; /* 替换为目标颜色 */
}

```

除了红色文本之外，还有其他部分格式不正确吗？

---

<div class="post-metadata">

### Author: ![alex10](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex10/32/575949_2.png) [@alex10](https://meta.discourse.org/u/alex10)
#### Post date: [2024年八月12日 19:11 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/3 "2024-08-12T19:11:45Z")

</div>

感谢您的建议，@bryce！我将尝试调整我们网站主题中的 CSS 来解决红色文本的问题。

我将尝试调整我们网站主题中的 CSS 来解决红色文本的问题。

我认为主要问题是我原始截图中的字体没有格式化成代码样式——它缺少通常用于代码块的等宽字体样式。理想情况下，我希望代码能更像这样，具有正确的字体和字体颜色：

![Screenshot 2024-08-12 at 12.04.53 PM](https://global.discourse-cdn.com/meta/original/4X/b/d/4/bd4e23a035a8482ab8b771cd5cacb1bca6f31b03.png)

有没有办法确保代码以等宽字体显示，类似于此示例中的字体？

除此之外，我没有注意到任何其他主要的格式问题，但我会继续测试，如果发现其他问题会告知您。

再次感谢您的帮助！

---

<div class="post-metadata">

### Author: ![bryce](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bryce/32/377822_2.png) [@bryce](https://meta.discourse.org/u/bryce)
#### Post date: [2024年八月12日 19:35 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/4 "2024-08-12T19:35:33Z")

</div>

您可以通过覆盖这些 CSS 变量来更改突出显示颜色：

1. `--hljs-comment`：用于代码注释
2. `--hljs-number`：用于代码中的数字值
3. `--hljs-string`：用于代码中的字符串值
4. `--hljs-literal`：用于代码中的字面量值
5. `--hljs-tag`：用于 HTML/XML 标签
6. `--hljs-attribute`：用于 HTML/XML 标签中的属性
7. `--hljs-symbol`：用于代码中的符号
8. `--hljs-bg`：用于代码块的背景颜色
9. `--hljs-builtin-name`：用于内置函数名称

例如，要覆盖字符串颜色：

```css
:root {
   --hljs-string: pink;
}

```

看起来 hljs（使用的突出显示库）可能不支持突出显示您示例中的 Python 标识符，例如 `degirum`，但您可以通过更改其他颜色来获得非常接近的效果。

至于字体，这很奇怪——代码块默认应该以等宽字体显示。我建议您仔细检查您的主题，确保没有任何内容被覆盖。

---

<div class="post-metadata">

### Author: ![alex10](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex10/32/575949_2.png) [@alex10](https://meta.discourse.org/u/alex10)
#### Post date: [2024年八月12日 19:40 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/5 "2024-08-12T19:40:33Z")

</div>

为了澄清一下，我们目前使用的是默认主题，没有添加任何其他组件，所以等宽字体默认未应用于代码块，这有些奇怪。看起来没有任何东西在覆盖它。

供参考，我正在使用 macOS 14.6.1 上的 Chrome 版本 127.0.6533.100 进行操作。

---

<div class="post-metadata">

### Author: ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)
#### Post date: [2024年八月12日 19:57 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/6 "2024-08-12T19:57:53Z")

</div>

您能在 [try.discourse.org](http://try.discourse.org) 上重现该问题吗？

我试过了，但链接没有被高亮显示。

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

 ![image](https://global.discourse-cdn.com/meta/original/4X/9/4/1/94134d223fab3f5efb53fc19b65a3dfcf2e4b06b.png)

---

<div class="post-metadata">

### Author: ![alex10](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex10/32/575949_2.png) [@alex10](https://meta.discourse.org/u/alex10)
#### Post date: [2024年八月12日 20:29 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/7 "2024-08-12T20:29:33Z")

</div>

我在 [try.discourse.org](http://try.discourse.org) 上测试了该代码，结果显示在下面的截图中：

 ![Screenshot 2024-08-12 at 1.21.45 PM](https://global.discourse-cdn.com/meta/original/4X/a/6/e/a6e56933797af80aa3491270471ca9f48c3e6d40.png)

字体看起来是正确的，链接也按预期高亮显示。但是，在我们的社区中，字体显示不正确，链接也没有按应有的方式高亮显示，我仍然遇到这个问题。

我不确定为什么会出现这种差异。

供参考，[这里](https://community.degirum.com/t/pysdk-quick-start-guide/71?u=alex)是我们社区页面的链接。

---

<div class="post-metadata">

### Author: ![Jagster](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jagster/32/192154_2.png) [@Jagster](https://meta.discourse.org/u/Jagster)
#### Post date: [2024年八月12日 20:40 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/8 "2024-08-12T20:40:05Z")

</div>

安全模式看起来还不错，我想。至少在字体方面。

 ![图像显示的是电脑屏幕的截图，其中包含一段代码片段。(由AI标注)](https://global.discourse-cdn.com/meta/original/4X/d/a/4/da45fd6dfb2fd514348e88adab59c36c0dae16fc.jpeg)

所以问题出在某个插件、主题或组件上。

---

<div class="post-metadata">

### Author: ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)
#### Post date: [2024年八月12日 20:42 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/9 "2024-08-12T20:42:35Z")

</div>

> [@alex10](#):
>
> 我在 [try.discourse.org](http://try.discourse.org) 上测试了代码，结果如下面的截图所示：

那是 Java 高亮显示

 ![image](https://global.discourse-cdn.com/meta/original/4X/4/1/6/41640113d28b4dc63edd8e94e9a00bc65c2650a3.png)

而在你的论坛上，它是 Makefile

 ![image](https://global.discourse-cdn.com/meta/original/4X/a/9/1/a913131de818ae0d523dfa4b5dd665466303fbdf.png)

这里有一篇关于 [Selecting the programming language used in code blocks](https://meta.discourse.org/t/selecting-the-programming-language-used-in-code-blocks/19247) 的指南  
你也可以在站点设置中选择默认语言

你的论坛截图是在非安全模式下截取的，所以对我来说字体是等宽的。

---

<div class="post-metadata">

### Author: ![alex10](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex10/32/575949_2.png) [@alex10](https://meta.discourse.org/u/alex10)
#### Post date: [2024年八月12日 20:52 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/10 "2024-08-12T20:52:47Z")

</div>

感谢两位花时间帮助我解决这个问题。很高兴知道其他人看到的字体是等宽字体。

由于在我这边显示不正确，这可能是我设置或环境特有的问题。在 Chrome 和 Safari（都是最新版本）上，它在我这边显示都不正确。在我组织里的其他人使用 PC 也显示不正确。

---

<div class="post-metadata">

### Author: ![alex10](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex10/32/575949_2.png) [@alex10](https://meta.discourse.org/u/alex10)
#### Post date: [2024年八月12日 20:59 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/11 "2024-08-12T20:59:17Z")

</div>

我在个人电脑上检查了，问题仍然存在。代码字体仍然没有显示为等宽字体，链接也没有正确高亮显示。

这似乎排除了我主要设置的问题，因为问题在不同设备上是一致的。

---

<div class="post-metadata">

### Author: ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)
#### Post date: [2024年八月12日 21:05 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/12 "2024-08-12T21:05:17Z")

</div>

您是否尝试过在安全模式下禁用主题自定义？

[https://community.degirum.com/t/pysdk-quick-start-guide/71?safe\_mode=no\_themes](https://community.degirum.com/t/pysdk-quick-start-guide/71?safe_mode=no_themes)

---

<div class="post-metadata">

### Author: ![alex10](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex10/32/575949_2.png) [@alex10](https://meta.discourse.org/u/alex10)
#### Post date: [2024年八月12日 21:07 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/13 "2024-08-12T21:07:23Z")

</div>

我在安全模式下进行了检查，问题在那里似乎已解决——代码字体显示为等宽字体，链接也已正确高亮显示。

这很令人困惑，因为我们使用的是默认主题，没有添加任何其他组件，所以我不知道在正常模式下可能是什么原因导致了这个问题。

---

<div class="post-metadata">

### Author: ![alex10](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex10/32/575949_2.png) [@alex10](https://meta.discourse.org/u/alex10)
#### Post date: [2024年八月12日 21:13 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/14 "2024-08-12T21:13:27Z")

</div>

这是我们的默认主题设置：

 ![Screenshot 2024-08-12 at 2.12.48 PM](https://global.discourse-cdn.com/meta/original/4X/7/7/a/77aa06893505567f37a3eb9e35697de35cdc2dd5.png)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2024年八月12日 21:18 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/15 "2024-08-12T21:18:35Z")

</div>

这就是我所期望的。这些字符串是不同颜色的，这样您就可以快速区分它们是字符串。在您的文本编辑器中看起来不就是这样吗？

我认为您可能只想将 `SiteSetting.default_code_lang` 更改为“text”或“”。还有一个 `highlighted_languages` 设置。我很确定在这里他们已经将默认代码语言更改为类似 text 的内容，所以

它看起来是这样的：

```plaintext
result=model("https://big.bang")

```

如果我强制使用 ` ```python`，它看起来是这样的

```python
result=model("https://big.bang")

```

---

<div class="post-metadata">

### Author: ![alex10](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex10/32/575949_2.png) [@alex10](https://meta.discourse.org/u/alex10)
#### Post date: [2024年八月12日 21:28 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/16 "2024-08-12T21:28:22Z")

</div>

默认代码语言设置为自动。我刚将其更改为文本，但对我这边没有任何影响。

这是我们精选的语言：

 ![Screenshot 2024-08-12 at 2.27.46 PM](https://global.discourse-cdn.com/meta/original/4X/6/8/c/68cfcb776b1f993976ee3a5e48c70c84a32aa251.png)

---

<div class="post-metadata">

### Author: ![alex10](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex10/32/575949_2.png) [@alex10](https://meta.discourse.org/u/alex10)
#### Post date: [2024年八月12日 21:31 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/17 "2024-08-12T21:31:40Z")

</div>

我的文本编辑器看起来是这样的：

 ![Screenshot 2024-08-12 at 2.31.07 PM](https://global.discourse-cdn.com/meta/original/4X/c/6/9/c694527f8cee89d58e186c1d25250db2da217e2d.png)

---

<div class="post-metadata">

### Author: ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)
#### Post date: [2024年八月12日 21:31 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/18 "2024-08-12T21:31:43Z")

</div>

当我查看时，自动检测认为它是 makefile，而不是 python。

> [@Moin](#):
>
> 在你的论坛上是 makefile
> 
> ![image](https://global.discourse-cdn.com/meta/original/4X/a/9/1/a913131de818ae0d523dfa4b5dd665466303fbdf.png)

你是否在你的帖子中添加了[代码块的编程语言](https://meta.discourse.org/t/selecting-the-programming-language-used-in-code-blocks/19247)？

---

<div class="post-metadata">

### Author: ![alex10](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alex10/32/575949_2.png) [@alex10](https://meta.discourse.org/u/alex10)
#### Post date: [2024年八月12日 21:32 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/19 "2024-08-12T21:32:53Z")

</div>

当我在代码块中添加 Python 时，它看起来是这样的：

 ![Screenshot 2024-08-12 at 2.32.30 PM](https://global.discourse-cdn.com/meta/original/4X/0/c/4/0c422b60845abad0a348fa60520234d0b1dab3c8.png)

由于我负责管理社区，我的目标是能够看到最终用户应该看到的样子。正如您在右侧看到的，它看起来不对。

---

<div class="post-metadata">

### Author: ![Moin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/moin/32/554653_2.png) [@Moin](https://meta.discourse.org/u/Moin)
#### Post date: [2024年八月12日 21:47 UTC](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315/20 "2024-08-12T21:47:30Z")

</div>

在通用 CSS 中添加了什么？

[下一頁](https://meta.discourse.org/t/issue-with-code-block-formatting-in-discourse-red-text-and-unintended-highlighting/321315.md?page=2)
