# 将 digest\_custom\_html 作为 HTML 处理（之前：覆盖 digest.html.erb）

**URL:** <https://meta.discourse.org/t/fixing-digest-custom-html-to-be-treated-as-html-was-overriding-digest-html-erb/361988>\
**Category:** Development\
**Created:** [2025年四月15日 22:22 UTC](https://meta.discourse.org/t/fixing-digest-custom-html-to-be-treated-as-html-was-overriding-digest-html-erb/361988 "2025-04-15T22:22:18Z")\
**Posts on this page:** 1\
**Showing post:** 1

<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:** [2025年四月15日 22:22 UTC](https://meta.discourse.org/t/fixing-digest-custom-html-to-be-treated-as-html-was-overriding-digest-html-erb/361988/1 "2025-04-15T22:22:19Z")

</div>

TL;DR：

我想在摘要中添加一些内容。我以前可以覆盖模板，但现在不行了，原因不明。

因为覆盖模板是 **不好的** ，所以在摘要中有一些地方会被 `digest_custom_html` 替换，但它会插入文本，所以这些地方需要附加 `.html_safe`。至少有一个地方放错了（名字里有“above”，但实际上是在下面）。

我认为我有能力为此做一个 PR，除非因为它太简单了，别人做起来会更容易。

### 更长、更痛苦的故事。

我在这里做过：[https://github.com/pfaffman/discourse-add-to-summary，但这似乎已经不起作用了。](https://github.com/pfaffman/discourse-add-to-summary%EF%BC%8C%E4%BD%86%E8%BF%99%E4%BC%BC%E4%B9%8E%E5%B7%B2%E7%BB%8F%E4%B8%8D%E8%B5%B7%E4%BD%9C%E7%94%A8%E4%BA%86%E3%80%82)

我认为我想做的是，在不修改核心代码的情况下，覆盖 `digest.html.erb` 模板。我以前可以通过将其放在 `app/views/user_notifications/digest.html.erb` 来实现，并且该文件会被处理而不是核心文件。但这似乎不再起作用了。

现在，我们有一些很棒的 `digest_custom_html`，如下所示：

> <https://github.com/discourse/discourse/blob/main/app/views/user_notifications/digest.html.erb#L277>

聪明的读者会注意到，热门话题大约在第 78 行开始，远早于第 277 行。我不确定我花了多少时间因为看错了地方而认为什么都没有发生。但这是题外话。

我确实设法在 `plugin.rb` 的 `after_initialize` 中做到了这一点。

```plaintext
  require_dependency "user_notifications"
  module ::UserNotificationsHelperOverride
    def digest_custom_html(position_key)
      puts "doing improved the digest: #{position_key}"
      if position_key == "below_popular_topics"
        puts "doing the custom html for above_popular_topics"

        # Custom HTML for the popular topics position
        "<div>MY COOOOOOOL TEXT</div>"
      else
        puts "doing the super for #{position_key}"
        super
      end
    end
  end

```

它确实在模板中我期望被更改的地方添加了文本！

可惜的是，它没有被当作 HTML，而是被当作纯文本。所以。。。

我在 all\_the\_plugins 中查找，没有发现任何人使用这段代码的例子。

如果我将类似这样的行更改为

```plaintext
        <%= digest_custom_html("below_popular_topics") %>

```

并替换为

```plaintext
        <%= digest_custom_html("below_popular_topics").html_safe %>

```

这是否会是一个 PR 欢迎的更改？而且，既然我已经在做了，也许可以确保 `position_key` 的名称更符合它们的位置？

---

_[View the full topic](https://meta.discourse.org/t/fixing-digest-custom-html-to-be-treated-as-html-was-overriding-digest-html-erb/361988)._
