Images in topic excerpts

I am trying to set up Discourse as a headless API server. I need to show the full text of the topic and the images in my custom-built landing page. I am able to get the full text with always_include_topic_excerpts setting. However, the excerpt doesn’t have images.

Digging a bit deeper, found out that the ExcerptParser.rb has options that can help me meet my requirements. If I can change this line in Post.rb, it will meet my needs: discourse/post.rb at 1059557ce11c474fdf568571423a88821b072141 · discourse/discourse · GitHub

What would be the best way of going about it? Maybe create a SiteSetting for this, similar to SiteSetting.post_excerpt_maxlength?

Ideally, I would want these three boolean settings:
post_excerpt_markdown_images
post_excerpt_strip_images
post_excerpt_strip_links

Thank you for your help!

Is there a reason you’re trying to pull the full topic text via the excerpt? You should be able to get the full cooked content, which includes images:

Screen Shot 2020-05-15 at 10.08.24 PM

3 Likes

Hi Kris, thanks for responding. I need the full topic text when I hit /latest.json. By default, it only returns the topic title with the exception of some posts (announcements?) where it also returns an excerpt. With the always_include_topic_excerts setting on, I am able to get the full text except links and images. I am looking for a way to get links and images in the excerpt.

Created a draft PR to get an early feedback: FEATURE: Add site settings for images and links in excerpts by nrchandan · Pull Request #9799 · discourse/discourse · GitHub

I don’t really understand but would like to discuss, either remove all links and images or add them?

Hi @Bcat, let me try to get a before and after screenshot shortly.

1 Like

Thanks for the draft PR @chandan.gupta but we strip images from excerpts for a reason

and we also don’t like to add extra site settings unless there’s a lot of demand for them. If you really need this on your site, then I suggest creating a plugin for it.

5 Likes

Thanks @Johani, not a problem. That makes sense. So this is doable via plugins as well, correct?

1 Like

You could do it from a plugin, but this is a messy change, you are fighting with core defaults so it will have unintended results.

4 Likes

@sam would it be a complex plugin to return the posts[0].raw or posts[0].cooked along with /latest.json? This way, we wouldn’t need to piggyback on “excerpt”, and it would also satisfy our needs. Could you point me to any reference to implement this? I understand that this would involve a lot more database reads, but I am ok with this performance cost.

1 Like