How to add hashtag-like functionality

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to add hashtag-like functionality

mark
This post was updated on .
I figured out how to add hashtag-like functionality on my forum (which is on n8.nabble.com). Here's how:

Make a new macro called editor_tag_button. Put the following text in it:

<macro name="editor_tag_button">
    <n.put_in_head.>
        <script type="text/javascript">
            Nabble.tag = function() {
                var textarea = Nabble.get(textareaID);
                var s = this.getSelection(textarea);
                if( s != "" || (s=prompt("Enter the tag text:","")) != null ) {
                    if (s!="") {
                        s="\n"+s.trim()+"\n"
                        s=s.replace(/[\n;,]+ */gm, "\n");
                        s=s.trim();
                        s=s.replace(/ +/g, "_");
                        var ssplit=s.split("\n");
                        var s3="";
                        var href1='<a href="'+'/template/NamlServlet.jtp?macro=search_page&node=1&query=';
                        var href2='&n=1">';
                        var href3="</a>";
                        ssplit.forEach( function(s2) {
                            if (s2.includes("_")==false) {
                                var spart=href1+s2+"_"+href2+s2+"_"+href3;
                                s3+=spart+"\n";
                            }
                            else {
                                var spart=href1+s2+href2+s2+href3;
                                s3+=spart+"\n";
                            }
                        } );
                        s=s3.trim();
                    }
                    this.setSelection( textarea, s );
                }
                textarea.focus();
            };
        </script>
    </n.put_in_head.>
    <td class="nowrap">
        <button type="button" onclick="Nabble.tag()" class="toolbar bold" style="height:1.65em" title="[t]tag[/t]">
            Tag
        </button>
        <n.tooltip use_title="true"/>
    </td>
</macro>

Edit the editor_toolbar macro. Add this line with the other buttons:
<n.editor_tag_button/>

That's it.

Of course, since this relies on underscores, it might not be as good for programming forums (which use underscores all over the place), but it should be fine for most other forums.

There might be some bugs, still, but I don't know of any off-hand.

You can add multiple tags at once. Delimit them by commas, semi-colons, or new lines. When you do this, they'll all be delimited by new lines in the end (whether you like that or not). All spaces within a tag will be converted into underscores.

Here's some simpler code for editor_tag_button, if you don't want it to be able to do multiple tags at once:

<macro name="editor_tag_button">
    <n.put_in_head.>
        <script type="text/javascript">
            Nabble.tag = function() {
                var textarea = Nabble.get(textareaID);
                var s = this.getSelection(textarea);
                if( s != "" || (s=prompt("Enter the tag text:","")) != null ) {
                    s=s.trim();
                    s=s.replace(/ +/g, "_");
                    if(s.includes("_")==false && s!="") {
                        s=s+"_";
                    }
                    this.setSelection( textarea, '<a href="'+'/template/NamlServlet.jtp?macro=search_page&node=1&query='+s+'&n=1">' + s + "</a>" );
                }
                textarea.focus();
            };
        </script>
    </n.put_in_head.>
    <td class="nowrap">
        <button type="button" onclick="Nabble.tag()" class="toolbar bold" style="height:1.65em" title="[t]tag[/t]">
            Tag
        </button>
        <n.tooltip use_title="true"/>
    </td>
</macro>

With this system, tags don't start with #, but all tags contain at least one underscore (single-word tags end with an underscore).

Words with underscores are easily unique in Nabble searches. So, I just made it link to the searches for those tags with underscores, and it works out well.

I couldn't get regular for and while loops to work in NAML JavaScript, for some reason. So this took a while. However, the for each loop worked when I tried it! (I didn't know how to do for each loops in JavaScript until after I tried other loops for a while.)

I used the code for the bold tag as a model for figuring this out (NAML-wise).

Anyway, anyone is free to use and/or adapt this code with or without attribution. But I don't make any guarantees.
Reply | Threaded
Open this post in threaded view
|

Re: How to add hashtag-like functionality

mark
There was a bug in the code that I just fixed with an edit, in case anyone tried it. The underscores are supposed to go on the end of single-word tags (not the beginning).