컴포넌트의 새 JS 탭으로 스크립트를 올바르게 이동하는 방법은 무엇인가요?

[Admin Notice] Theme ‘_Base’ contains code which needs updating. (id:discourse.script-tag-discourse-plugin) (learn more)

For one on my forum, I got rid of this message simply by moving my JS script to the new JS customization tab.

On my other forum, it doesn’t work that easily.

It’s an old override that shows the composer’s tag field only in categories that accept tags:

This script was heavily based on a similar (and old, 2019 or older) modification posted on meta, but I can’t find the original post anymore.

Anyway, there are two questions in one:

  1. How to properly move this in the JS tab? Is it possible, or should I create a theme component?

  2. Is there a more elegant way to hide/show the composer’s tag selector only in certain categories? I use tags for only 1 category on my forum (an ads category with #search and #offer tags)

I guess you could use CSS to show/hide the tag field based on the category class on the body, but whether that’s more elegant is up for debate :slight_smile:

It doesn’t solve it for staff [1], but if you set the tags as restricted to that category, they won’t be usable (by non-staff) on other categories. From a category /edit/tags:

Tags and tag groups specified above will only be available in this category and other categories that also specify them. They won’t be available for use in other categories.


  1. unless we Make category tag rules / restrictions apply to moderators too ↩︎

오랜만이어서 어떻게 동작하는지 확인해 보았습니다.

  • 새 주제를 생성할 때, 선택된 카테고리와 관계없이(관리자를 제외한 사용자에게 사용 가능한 태그가 없는 카테고리일 경우에도) 작성기에 태그 입력 필드가 표시됩니다. 저는 이를 방지하고 싶습니다.
  • CSS만으로는 해결할 수 없습니다. 작성기는 어떤 페이지에서든 열릴 수 있으며, 작성기의 태그 입력 필드를 숨기는 데 도움이 되는 원하는 카테고리를 대상으로 하는 상위 CSS 선택자가 없기 때문입니다. 이것이 제가 JavaScript를 사용한 이유라고 생각합니다.

따라서 제가 무언가를 놓치고 있지 않다면, 제 목표를 달성하기 위해 여전히 스크립트가 필요합니다.

이 기능에 대한 원래의 Contribute > Feature 주제는 여기 있었습니다:

이 기능이 개발되기에 충분한 관심을 받지 못했고, sam에 따르면 여기서는 #pr-welcome이 원하지 않는다고 합니다.

결국 커스텀 JS 솔루션(그리고 나서 수정해야 하는)에 머물러 있게 되었습니다 :smiling_face_with_tear:

It should be possible to just copy/paste it. Stuff in the JS tab is run in exactly the same way as <script type='text/discourse-plugin>. If it’s not working, can you share the code?

With some crazy modern CSS, it’s possible:

.d-editor-container:has(.category-input .select-kit-header[data-name="Support"]) .tags-input {
    display: none !important;
}

^^ this hides the tag input for the “Support” category on Meta.

You can swap the [data-name="Support"] for :not(data-name="Support") to reverse the logic.

Holy moly! Looks like my 2012 CSS skills aren’t cutting it anymore. Which is kinda weird, because I could’ve sworn 2012 was literally yesterday!


I went for

.d-editor-container:not(:has(.category-input .select-kit-header[data-name="Trading Post"])) .tags-input {
    display: none;
}

So it hides in every category but Trading Post.

Thanks all!