토픽 내에서 토픽 ID를 변수로 가져오는 방법이 있을까요?
사용 사례를 설명하자면, 지원 및 버그 추적 티켓을 위한 카테고리가 있습니다. 다음과 같은 템플릿으로 설정하고 싶습니다.
이것:
> 이 줄을 **삭제하지** 마세요: 지원 요청 #$(topicid)
이렇게 보입니다:
이 줄을 삭제하지 마세요: 지원 요청 #246
이 정보를 가져오는 다른 방법도 환영합니다. 다만 어떻게 해야 할지 잘 모르겠습니다.
토픽 내에서 토픽 ID를 변수로 가져오는 방법이 있을까요?
사용 사례를 설명하자면, 지원 및 버그 추적 티켓을 위한 카테고리가 있습니다. 다음과 같은 템플릿으로 설정하고 싶습니다.
이것:
> 이 줄을 **삭제하지** 마세요: 지원 요청 #$(topicid)
이렇게 보입니다:
이 줄을 삭제하지 마세요: 지원 요청 #246
이 정보를 가져오는 다른 방법도 환영합니다. 다만 어떻게 해야 할지 잘 모르겠습니다.
You’re trying to put the topic ID in the raw text of the topic?
What problem is that going to solve?
It will solve the problem of me telling my support staff that they need to first create a new topic, then save it, then look at the URL and get the ID #, then go back and edit the topic in order to put the # in the body manually.
What are you using the topic ID# in the body of the OP for in your use case here?
A unique id # for a ticket/bug.
Where is that ticket/bug going? The ID is in the URL; there it is persistent and never wrong. How does it help to have the topic ID in the text of the topic? And, as you point out, if it’s in the body of the message, anyone can change, delete, or type it wrong.
Is something else using the topic ID? If so, what and how?
It will be easier to create a custom component that appends this to the correct posts rather than filling a template.
If you edit the code of your theme under admin > themes & components… and add this to the JS tab, it should get you what you’re after with some adjustment:
import { apiInitializer } from "discourse/lib/api";
import Component from "@glimmer/component";
class TopicIdentifier extends Component {
get topicId() {
return this.args.post?.topic?.id;
}
get shouldShow() {
const firstPost = this.args.post?.post_number === 1; // first post in topic
const desiredCategories = [45, 3]; // a comma separated list of category IDs you want this to appear in
const isInCategory = desiredCategories.includes(
this.args.post?.topic.category.id
);
return firstPost && isInCategory;
}
<template>
{{#if this.shouldShow}}
<!-- you can edit the content below, {{this.topicId}} is where the topic ID will be filled -->
This topic's ID is
{{this.topicId}}
<!-- you can edit the content above -->
{{/if}}
</template>
}
export default apiInitializer((api) => {
api.decorateCookedElement((element, helper) => {
const wrapper = document.createElement("div");
wrapper.className = "custom-topic-id"
helper?.renderGlimmer(
wrapper,
<template><TopicIdentifier @post={{helper.model}} /></template>
);
element.appendChild(wrapper);
});
});
You can find the category ID from its URL to configure desiredCategories, for example, the support category here on Meta has the ID of 6
You can add CSS to style this how you’d like with the CSS tab… for example
.custom-topic-id {
display: inline-block;
background: yellow;
}
Yes, it can be typed wrong. Hence why if you use a variable you don’t have to worry about typing it wrong.
It helps because I’m the user and that’s how I want my workflow set up.
You cannot currently search Discourse by topic id. If the # is in the body of the topic, then it’s now part of the search results.
So I can tell a coworker, 'Hey search for bug #138 and let me know what you think."
Then it’s really easy for Nancy to just go to the site and do a search for 138 and get a result.
You don’t need to search:
/t/-/id – but really /t/id works.
They can just type /t/138 and not have to search. But if you are talking to a person, wouldn’t saying "search for bug about “some words they know” make more sense to use in conversation than a number?
And that was going to be my other suggestion.
I think I’d have used renderInOutlet rather than decorateCookedElement, but that’s probably because I don’t fully understand decorateCookedElement.
But this will don’t let you search for a bug/topic by topic_id.
I will test this out. Thanks for the info. Is that part of the body that becomes searchable?
But I want to search. It’s easier.
Go to site, then append /t/id to the URL at the top of the window VS go to site and type ID into the search field that’s right in the center of the page.
If I’m talking to a person, or typing a message, yes a number is more concise and less error prone than “Search for exact title that might be 5 words long”
I will look into that, but if it kills being able to use in a search field, then it won’t work for me.
I really love Discourse, and I have gotten a TON of useful feedback and help from everyone here.
What I don’t understand is that occasionally folks tell me that “You shouldn’t do it that way because this way is better.” Well, no, this way is better for you, but that way is better for me. And neither is wrong.
I will look into that, but if it kills being able to use in a search field, then it won’t work for me.
Yeah don’t bother then, unfortunately it only appends content to the topic when it’s rendered… it doesn’t add it to the database to make it searchable. I think what you’d ultimately need is a custom plugin or a custom script added to Discourse Automation for it to be fully automatic.
Searching by topic ID or including it in a template isn’t a common request, so it’s not something we support out of the box.
What I don’t understand is that occasionally folks tell me that “You shouldn’t do it that way because this way is better.” Well, no, this way is better for you, but that way is better for me. And neither is wrong.
I think it’s that regulars here know it’s not a feature we currently have, so we’re trying to support you by figuring out some potential work-arounds.
so we’re trying to support you by figuring out some potential work-arounds.
And often if you go back you can find a better solution.
If you really want to embed them topic id in the text so you can find it in search then you’d need a plugin and it could add ticket123 to the raw text (I think search looks at raw?). The topic id doesn’t exist before you create the topc, so you can’t insert it when you create it.
Oh! Maybe what you want is a plugin that would add a tag bug123 when a topic in a bug category gets created. Then you could refer to it as #bug123 and have it link anywhere you mention it and be able to search. If you’re self hosted or on enterprise that wouldn’t be too hard. I imagine someone in Marketplace would do it for $250 to $500.
Or, if you want to be able to “search” for a bug by number, you could add a theme component that let you type it in a ‘bug box’ and go straight there. Maybe that’s a solution that would work and it wouldn’t require a plugin.
결국 주제 ID를 더 절제된 형태로 표시하는 방식으로 마무리했습니다. 여전히 검색은 불가능하지만, 제가 정말로 원하는 기능이 실현될 때까지는 이대로 사용할 수 있을 것 같습니다.
<script type="text/discourse-plugin" version="0.5">
api.decorateWidget('post-meta-data:after', dec => {
if (dec.attrs.post_number === 1) {
const topic = dec.getModel().get('topic');
return dec.rawHtml(`<div class="post-info topic-id"><b> #${topic.get('id')}</b></div>`);
}
});
</script>
문제는 이 코드가 이제 구식이 되어 버렸다는 점이고, 어떻게 수정해야 할지 모르겠습니다.
준수하려면 어떻게 업데이트해야 하나요?
이를 어떻게 해야 하는지 보여주기 위해 https://github.com/literatecomputing/discourse-custom-components를 예제로 만들었습니다.
- settings.custom_text_block.forEach((component) => {
- api.renderInOutlet(
- component.outlet,
- <template><TextBlock @component={{component}} /></template>
- );
이는 컴포넌트를 삽입하는 매우 간단한 예시입니다. 따라서 <template> 앞에 코드를 추가하여 토픽 ID를 가져오고 표시 여부를 결정하는 로직을 구현해야 합니다.
플러그인 아웃렛으로 topic-list-before-link를 사용하는 것이 좋을 것 같습니다.
하지만 저는 검색을 하고 싶어요. 그게 더 쉬우니까요.
사이트에 접속한 후 창 상단의 URL에 /t/id를 붙이는 것 VS 사이트에 접속한 후 페이지 중앙에 있는 검색란에 ID를 입력하는 것.
제가 틀리지 않았다면, 검색란에 topic:id를 입력하면 검색 결과에 해당 토픽이 표시됩니다.
예를 들어, topic:374168 this로 검색하면 이 토픽의 게시글만 표시됩니다.
플러그인이 필요하고, 원본 텍스트(raw text)에 ticket123를 추가할 수 있을 겁니다(검색이 원본 텍스트를 참조한다고 생각하는데). 토픽 ID는 토픽을 생성하기 전에는 존재하지 않으므로, 생성 시점에 이를 삽입할 수 없습니다.
궁금한 점이 있는데요… 토픽의 원본(raw)을 업데이트하기 위해 AJAX 요청을 보내는 TC(토픽 컨트롤러)가 충분할까요?
예를 들어
topic:374168 this를 검색하면 이 토픽의 게시글만 표시됩니다.
이것은 이 토픽에서만 작동합니다. 다른 랜덤한 토픽을 시도해 보세요. "one two four two four zero"를 검색해 보세요. 그건 태그 배너 토픽 ID입니다.
topic:124240를 말씀하시는 건가요?填充語(‘hello’)는 단순히 예시였을 뿐입니다. 태그 배너의 경우 'hi’가 작동합니다. 지원 티켓에 공통적으로 사용되는 단어가 있거나, 일반적으로 자주 쓰이는 단어가 있다면 그것을 시도해 볼 수 있습니다. 단순히 워크어라운드를 제안해 본 것입니다
.
문제는 이 코드가 이제 구식이 되었다는 점이고, 어떻게 수정해야 할지 모르겠습니다.
Topic ID as a variable? - #7 by awesomerobot 에 있는 코드를 시도해 보셨나요?
또한, 하려는 작업이 주제 ID를 "검색"하는 것이라면, 주제 ID를 받아 해당 주제로 바로 이동하는 컴포넌트를 만들 수도 있을 것입니다.