Trigger search onclick (hashtags)


(Joe) #1

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

Background:

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?


(Mittineague) #2

Something like this?
#search for this word.


(Joe) #3

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.


(Jeff Atwood) #4

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


(Alan Tan) #5

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


(Jeff Atwood) #6

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

yep

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

(Alan Tan) #7

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.


(Jeff Atwood) #8

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


(Alan Tan) #9

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.


(Jeff Atwood) #10

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