테마 수정자: 간단한 소개

테마의 기능이 점점 더 복잡해짐에 따라, 코어 서버 사이드 동작을 조작할 수 있는 방법을 모색해 왔습니다. 플러그인만큼의 제어 권한은 부여되지 않지만, 테마가 조작할 수 있는 몇 가지 사전 정의된 훅을 제공할 수 있습니다.

소개합니다: 테마 수정자 (theme modifiers) :partying_face:

이들은 테마의 about.json 파일에서 modifiers 키를 사용하여 지정됩니다.

100% 최신 수정자 목록은 theme_modifier_set.rb 하단의 데이터베이스 스키마를 확인하세요. 현재까지 제공되는 수정자의 빠른 요약은 다음과 같습니다:

  • serialize_topic_excerpts boolean (기본값 false) - 주제 목록을 직렬화할 때 항상 발췌문을 포함합니다

  • csp_extensions string array - CSP에 지시문을 추가합니다. 이전의 “extend_content_security_policy” 테마 설정 방법과 동일하게 작동합니다. 하지만 기억하세요, 단순한 <script src=""> 태그는 자동으로 허용됩니다.

  • svg_icons string array - 아이콘 하위 집합에 포함되어야 할 아이콘 목록

  • topic thumbnails array of dimensions - 주제 섬네일 세트에서 추가 해상도를 요청합니다. 비동기로 생성되므로, 요청한 크기가 제공되지 않으면 원래 이미지로 폴백해야 합니다. 자세한 정보는 커밋 메시지에서 확인할 수 있습니다.

  • serialize_post_user_badges string array - 게시글 데이터와 함께 직렬화할 배지 이름 목록(배지 테이블의 항목과 일치해야 함). 설정되면 시스템은 클라이언트 사이드 렌더링을 위해 각 게시글에 지정된 사용자 배지를 포함합니다.

이 새로운 훅을 적극 활용하는 테마 중 하나는 Topic List Thumbnails 입니다. 작동 방식을 보려면 코드를 확인해 보세요.

설정에 종속적인 수정자

테마 수정자는 테마 설정에서 값을 가져오도록 구성할 수도 있으며, 이를 통해 사이트 운영자는 테마 코드를 수정하지 않고도 수정자 동작을 오버라이드할 수 있습니다. 수정자를 설정에 종속적으로 만들려면 about.json에서 다음 구문을 사용하세요:

{
  "modifiers": {
    "modifier_name": {
      "type": "setting",
      "value": "setting_name"
    }
  }
}

예를 들어, show_excerpts라는 이름의 테마 설정이 있고 이것이 serialize_topic_excerpts 수정자를 제어하도록 하려면:

settings.yml에서:

show_excerpts:
  default: false

about.json에서:

{
  "modifiers": {
    "serialize_topic_excerpts": {
      "type": "setting",
      "value": "show_excerpts"
    }
  }
}

show_excerpts 설정이 변경되면 수정자 값이 자동으로 일치하도록 업데이트됩니다. 이를 통해 사이트 운영자는 관리자 UI를 통해 테마 동작을 유연하게 커스터마이즈할 수 있습니다.


이 문서는 버전 관리됩니다 - 변경 사항을 github에서 제안해 주세요.

35개의 좋아요

David, probably a bit lazy of me to ask but is there any way to access this in a plugin:

Themes can request additional thumbnail sizes by using a modifier in their about.json file:

I will be attempting to migrate the TLP plugin to this new schema and it would be good to have the same access to features from a plugin, at least in the meantime.

2개의 좋아요

There isn’t at the moment, but I’ll look into it :eyes:

6개의 좋아요

David, what’s the right approach for BULK recreation of thumbnails?

I’ve just tried utilising on one of my sites and it seems to have processed about 10% of the Topics … then given up (or turned its nose up at the rest). Why I think it’s the former is that the Topics for whom Thumbnails were produced were the latest 10%.

Rebaking posts doesn’t seem to cut it. In fact, I did run a bulk rebake and wonder if that upset it …

I notice that image_url can be populated, but there are no thumbnails.

Any advice, appreciated!

1개의 좋아요

That column doesn’t do anything, and will be dropped very soon. image_upload_id is the one you want.

There should be no need for this. I deliberately designed it so that people can install new themes without having to mess around on the console. Thumbnails are generated asynchronously when needed. For example:

  • you add a new theme, which requests new resolutions
  • a user requests a topic, we serve the thumbnails that exist. If any sizes don’t exist, we schedule a sidekiq job.
  • next time someone requests the topic, the correct thumbnails will exist

If the requested thumbnail size is larger than the original, we won’t bother generate the thumbnail.

So the critical thing to bear in mind for this to work is:

There’s an example of this fallback logic in the thumbnail theme component I made - feel free to steal logic from there.

5개의 좋아요

The fallback is serialised as thumbnailsl[0]? Yes, I’m already handling that. (nice implementation btw, very easy to handle)

Is it possible some images are ‘not making the grade’ or fitting the criteria?

The behaviour we have in the TLP plugin will pick up one-box thumbnails. That’s not happening in every case here I think.

For example, if you get time, take a look at these examples:

I don’t think these thumbnails make the cut. The thumbnails are serialized as null

Yes that’s deliberate - we had a number of requests to remove small onebox thumbnails. For example, people were ending up with their github avatar as a topic thumbnail - which is rarely intended

https://github.com/discourse/discourse/commit/956d15d13fd8056cbf60ca64ebbd1edca00d0125

Note that for oneboxes where the image is the actual content (such as instagram/twitter/etc photos), they will be selected.

As for the youtube video, I fixed that yesterday.

https://github.com/discourse/discourse/commit/0c6f30d92a4090aa7447e2e579a17a180e49ce17

5개의 좋아요

Ah great, thanks for confirmation.

That’s odd, build was more recent, but some still seem to be being overlooked.

Yeah, that’s partly why I ended up implementing a thumbnail picker for the situations where the automated choice wasn’t optimal. I may still want to modify that behaviour, but I will try to do so in the plugin.

Thanks for your time David!

2개의 좋아요

OK, I’ve worked it out. After a somewhat fruitless byebug session I could not work out why older YT posts were not getting thumbnails.

Then is dawned on me. It’s because of this:

So I suggest that actually it might be necessary to rebake after setting this to a rather larger number (365?).

I think I’m right in saying that if something is not uploaded locally, it won’t have a thumbnail created? …

4개의 좋아요

:+1: correct, this only works for local uploads… we may need to rethink that “max days old” setting :thinking:

8개의 좋아요

I have a plan, will try and get it implemented this week. One question - do you need the values to be dynamic?

i.e. Will the resolutions be defined at boot? Or at runtime (e.g. via site settings)?

The former is easier… but the latter might be possible as well :thinking:

2개의 좋아요

Thanks for taking a look.

I just need a fixed way, exactly like the theme component.

A site setting would be nice though.

I will add for full disclosure: id like to migrate away from the plugin so parity with theme component solution would be more than enough.

2개의 좋아요

@merefield here you go:

https://github.com/discourse/discourse/commit/725e38f9d7c42975c9d75afe0a1f79499693667b

Hopefully the commit message explains how it works, but let me know if you have any questions

7개의 좋아요

Excellent. Just added it to TLP and looks like it’s working! Thanks for your help!

4개의 좋아요

4 posts were split to a new topic: Getting thumbnails from json endpoints

Can we make it work for images from remote server as well? For example, images from Blogger, Picasa, or Amazon S3?

Because Discourse supports hosting on Amazon S3 for big and large image site, now if everything needs to be hosted on the local server directly then this design methodology seems to be a drawback.

With this update, it’s not an easy fix for my site since we’re using other server to host the images. Now it’s too difficult to move to an affiliated server with many posts, while it’s too big for hosting on local sever.

This is only designed to work on Discourse ‘uploads’. Those can be on S3, or some other service, if you use

We recommend using the download_remote_images site setting to automatically download images which are hotlinked from other sites.

4개의 좋아요

Hi David, anything special one needs to do to ensure Pro icons can be used in a TC?

1개의 좋아요

Nothing special, no. It should work just the same as using pro icons elsewhere in Discourse. I guess you are using this plugin to enable pro icons?

If it doesn’t work let me know and I’ll take a look :eyes:

2개의 좋아요

Yep we are. We’ll have another dig. Thanks for response late your eve!

2개의 좋아요