Trigger search onclick (hashtags)

Preface: this does not feel like it’s within the scope of Discourse. That’s why I opted for uncategorized.


If you start a word with # (hashtag) in Discourse you get a small menu that’s populated with categories. For example I can start typing #un then I can select #uncategorized and a link to that category is automatically added.

The output html is:

<a href="/c/17-uncategorized" class="hashtag">#<span>uncategorized</span></a>

However, if you continue to type and not select anything from the menu, something else happens.
For example if I type #uneven and then continue to type normally, the word is rendered with different html.

in the composer preview:

<span class="hashtag hashtag-tested tag-hashtag-tested">#uneven</span>

in the cooked post / reply:

<span class="hashtag">#uneven</span>

Note that the <span> wraps around the entire term and is unclickable.

In essence, Discourse does a wonderful job of recognizing hashtags, so I’m wondering if that can be built upon in the form of a plugin.

The concept:

With hashtag recognition out of the way,

could we not trigger a search when a hashtag is clicked that only returns the latest exact matches for the entire contents of the <span>? There’s no need for anything extra, just a native search operation with parameters (?)

Something along the lines of:

$("span.hashtag").on("click", function() {
	// do something

Combined with a little bit of CSS like so:

span.hashtag {
	cursor: pointer;
	color:  /* link color */

span.hashtag:hover {
	color:  /* link hover color */

to create basic hashtag functionality in Discourse. It seems feasible - again, in the form of a plugin - but there maybe limitations I am unaware of.

What do you think?

1 Like

Something like this?
#search for this word.


Yes! That’s exactly it.

There would be a lot of room for improvement at later stages but your example is the bare bones of the concept I was trying to describe.

1 Like

Hmm this actually feels like a bug to me, @tgxworld

Hmm I’m not following where the bug is. Is it the wrapping of an invalid hashtag with a span?

Apparently, let’s see if I can #repro this


<p>Apparently, let’s see if I can <span class="hashtag">#repro</span> this</p>

Is an #invalid hashtag meant to be clickable?. From what I’m reading, this sounds like a feature request where #something would be cooked as <a href="/search?q=something">#something</a> #something in markdown.

No, it is not meant to be anything. That markup should not exist in the baked post.

The markup is used for caching purposes so that we don’t check the same hashtags with the server each time the preview is updated. You can see that in @wrongusername as well.


OK, if it has a valid use, that is fine… I don’t have a big objection to it in that case.

1 Like

talking about the devil …
maybe that’s what I was looking for …

Actually I wanted to investigate an addon that has hashtags in the comments that works just like it happens on twitter, making a list of interconnected comments hasthaged …

This need arises because on we often discuss possible ideas and improvements to blender, and then it would be useful to use hashtag that collects and lists all these small ideas, but at the same time this type of hastags are not of first level importance and do not mix with the hashtags that categorize the site structure …

You would use tags for that.