# 主题摘要中的图片

**URL:** https://meta.discourse.org/t/images-in-topic-excerpts/151607
**Category:** Support
**Created:** [2020年五月15日 14:25 UTC](https://meta.discourse.org/t/images-in-topic-excerpts/151607 "2020-05-15T14:25:45Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![chandan.gupta](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chandan.gupta/32/180765_2.png) [@chandan.gupta](https://meta.discourse.org/u/chandan.gupta)
#### Post date: [2020年五月15日 14:25 UTC](https://meta.discourse.org/t/images-in-topic-excerpts/151607/1 "2020-05-15T14:25:46Z")

</div>

我试图将 Discourse 配置为无头 API 服务器。我需要在自定义构建的落地页上显示主题的完整文本和图片。通过设置 `always_include_topic_excerpts`，我已经能够获取完整文本，但摘录中不包含图片。

进一步深入研究后，我发现 `ExcerptParser.rb` 中的一些选项可以帮助我满足需求。如果我能修改 `Post.rb` 中的这一行，就能实现我的目标：[discourse/app/models/post.rb at 1059557ce11c474fdf568571423a88821b072141 · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/1059557ce11c474fdf568571423a88821b072141/app/models/post.rb#L485)

最佳的做法是什么？也许可以为此创建一个类似 `SiteSetting.post_excerpt_maxlength` 的站点设置？

理想情况下，我希望有三个布尔类型的设置：  
`post_excerpt_markdown_images`  
`post_excerpt_strip_images`  
`post_excerpt_strip_links`

感谢您的帮助！

---

<div class="post-metadata">

### Author: ![awesomerobot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/awesomerobot/32/142900_2.png) [@awesomerobot](https://meta.discourse.org/u/awesomerobot)
#### Post date: [2020年五月16日 02:11 UTC](https://meta.discourse.org/t/images-in-topic-excerpts/151607/2 "2020-05-16T02:11:15Z")

</div>

你为什么要通过摘要来获取完整的主题文本？你应该能够获取包含图片的完整渲染内容：

 ![Screen Shot 2020-05-15 at 10.08.24 PM](https://global.discourse-cdn.com/meta/original/3X/4/c/4c22f36ebc4db7f8135d2a18157d97fc30268c78.png)

---

<div class="post-metadata">

### Author: ![chandan.gupta](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chandan.gupta/32/180765_2.png) [@chandan.gupta](https://meta.discourse.org/u/chandan.gupta)
#### Post date: [2020年五月16日 02:22 UTC](https://meta.discourse.org/t/images-in-topic-excerpts/151607/3 "2020-05-16T02:22:26Z")

</div>

嗨，Kris，感谢回复。当我访问 /latest.json 时，我需要完整的主题文本。默认情况下，它只返回主题标题，除非是一些帖子（如公告），此时还会返回摘要。启用 `always_include_topic_excerpts` 设置后，我可以获取除链接和图片外的完整文本。我正在寻找一种方法，以便在摘要中包含链接和图片。

---

<div class="post-metadata">

### Author: ![chandan.gupta](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chandan.gupta/32/180765_2.png) [@chandan.gupta](https://meta.discourse.org/u/chandan.gupta)
#### Post date: [2020年五月16日 03:58 UTC](https://meta.discourse.org/t/images-in-topic-excerpts/151607/4 "2020-05-16T03:58:38Z")

</div>

已创建草稿 PR 以获取早期反馈：[FEATURE: Add site settings for images and links in excerpts by nrchandan · Pull Request #9799 · discourse/discourse · GitHub](https://github.com/discourse/discourse/pull/9799/files)

---

<div class="post-metadata">

### Author: ![Bcat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bcat/32/163148_2.png) [@Bcat](https://meta.discourse.org/u/Bcat)
#### Post date: [2020年五月16日 08:37 UTC](https://meta.discourse.org/t/images-in-topic-excerpts/151607/5 "2020-05-16T08:37:10Z")

</div>

我不是很明白，但想讨论一下：是移除所有链接和图片，还是添加它们？

---

<div class="post-metadata">

### Author: ![chandan.gupta](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chandan.gupta/32/180765_2.png) [@chandan.gupta](https://meta.discourse.org/u/chandan.gupta)
#### Post date: [2020年五月16日 09:10 UTC](https://meta.discourse.org/t/images-in-topic-excerpts/151607/6 "2020-05-16T09:10:50Z")

</div>

你好 @Bcat，我稍后会尝试提供一张前后对比的截图。

---

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [2020年五月16日 09:17 UTC](https://meta.discourse.org/t/images-in-topic-excerpts/151607/7 "2020-05-16T09:17:15Z")

</div>

感谢提交草稿 PR @chandan.gupta，但我们出于特定原因会移除摘要中的图片。

> [@Markdown appears in topic lists for pinned topics with image at top](https://meta.discourse.org/t/markdown-appears-in-topic-lists-for-pinned-topics-with-image-at-top/89359):
>
> See screenshot - when I pin a topic to the top of topic lists that has an image at the top, it features the markdown for the image. Not sure what the intended behavior is here but I have been having trouble educating my colleagues to avoid putting images at the top for announcements to prevent it and am honestly not sure it’s the right behavior. Seems to me it should either display a thumbnail of the image or suppress it. Thoughts? Suggestions?

此外，除非有广泛需求，我们也不愿额外添加站点设置。如果您确实需要在自己的站点上实现此功能，建议您为其创建一个插件。

---

<div class="post-metadata">

### Author: ![chandan.gupta](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chandan.gupta/32/180765_2.png) [@chandan.gupta](https://meta.discourse.org/u/chandan.gupta)
#### Post date: [2020年五月16日 09:23 UTC](https://meta.discourse.org/t/images-in-topic-excerpts/151607/8 "2020-05-16T09:23:08Z")

</div>

谢谢 @Johani，没问题。这说得通。所以这也可以通过插件实现，对吗？

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [2020年五月18日 05:51 UTC](https://meta.discourse.org/t/images-in-topic-excerpts/151607/9 "2020-05-18T05:51:38Z")

</div>

你可以通过插件实现，但这是一种混乱的修改，因为你在与核心默认设置对抗，会导致意想不到的结果。

---

<div class="post-metadata">

### Author: ![chandan.gupta](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chandan.gupta/32/180765_2.png) [@chandan.gupta](https://meta.discourse.org/u/chandan.gupta)
#### Post date: [2020年五月19日 06:30 UTC](https://meta.discourse.org/t/images-in-topic-excerpts/151607/10 "2020-05-19T06:30:27Z")

</div>

@sam，如果要让 `/latest.json` 同时返回 `posts[0].raw` 或 `posts[0].cooked`，这需要开发一个复杂的插件吗？这样我们就不需要借助“摘要”字段了，也能满足我们的需求。你能否指点一下实现此功能的参考文档？我明白这会涉及更多的数据库读取操作，但我可以接受这个性能开销。
