Skip to content

Hashtags not linking with Custom Replace Function #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jacobedawson opened this issue Apr 7, 2015 · 9 comments
Closed

Hashtags not linking with Custom Replace Function #96

jacobedawson opened this issue Apr 7, 2015 · 9 comments

Comments

@jacobedawson
Copy link

Hi - I love Autolinker, it's really useful! I'm having an issue though using the 'replaceFn' so that I can replace mentions & hashtags with my own urls. The thing is, @mentions are working fine, but hashtags are not. Below is the code that I'm using (as part of a much larger function):

msg = Autolinker.link( msg, {
            replaceFn : function( autolinker, match ) {
                switch( match.getType() ) {

                    case 'twitter' :
                       //THIS WORKS PERFECTLY
                        var twitterHandle = match.getTwitterHandle();
                        return '<a href=\"/user/' + twitterHandle + '\">@' + twitterHandle + '</a>';

                    case 'hashtag' :
                        //THIS IS NOT BEING RETURNED EVEN WHEN THERE ARE HASHTAGS
                        var hashtag = match.getHashtag();
                        return '<a href=\"/hashtag/' + hashtag + '\">#' + hashtag + '</a>';

                }
            }
        });

If anyone can point out what I'm doing wrong / neglecting here I'd really appreciate it!!

Cheers,

Jake

@jacobedawson
Copy link
Author

Still looking for an answer for this one, I've tried several different things and looked through the docs, but I'm completely stumped :(

@ghost
Copy link

ghost commented Apr 22, 2015

the hashtag option defaults to false. I have worked around this by setting the option {hashtag: 'twitter'} The replaceFn() then executes as expected.

@jacobedawson
Copy link
Author

@MetaTribal Thanks a lot for replying - I'll change the code when I get home from work :) I appreciate your help!

Cheers,

Jake

@jacobedawson
Copy link
Author

Hi @MetaTribal - I tried this out but this actually took me back to square 1, because it is linking to twitter.com - the reason that I'm using the replaceFn is because I would like to have Autolinker replace hashtags with links to my own domain - in the above code you can see that I'm using a relative URL.

This works fine for @ mentions, but it isn't working for hashtags. Does that make sense? If you have any additional tips about how I can achieve this it would be great :)

Cheers,

Jake

@gregjacobs
Copy link
Owner

So sorry for the late response Jake..

Looking to build something a bit different for hashtags, as suggested in #130.

In the meantime, @MetaTribal's suggestion should have worked. Setting hashtag to 'twitter' should cause Autolinker to match hashtags in the input text, and provide them to the replaceFn. If you're still having an issue (I know it's a number of months later.. lol), feel free to reopen and post your input text and I'll take a look. Make sure you grab the latest version too first (v0.22.0) - might have been a bug in an older version

@adamgins
Copy link

@gregjacobs great package. I have been using in default state in production for ages. Thanks.
I wanted to customize some of the mentions, I am having a similar issues to this issue of getting the replaceFn to fire.

I have initialized the autolinker with

var Autolinker = require( 'autolinker' );
var autolinker = new Autolinker( {
    urls : {
        schemeMatches : true,
        wwwMatches    : true,
        tldMatches    : true
    },
    email       : true,
    phone       : true,
    mention     : 'twitter',
    hashtag     : 'twitter',

    stripPrefix : true,
    newWindow   : true,

    truncate : {
        length   : 0,
        location : 'end'
    },

    className : ''
} );

then I call it like so

var linkedText = autolinker.link( inputText, {
                replaceFn : function( match ) {
                    console.log( "href = ", match.getAnchorHref() );
                    console.log( "text = ", match.getAnchorText() );
                    console.log("INPUTTEXT:", inputText, "MATCH TYPE:", match.getType(), match )

                    switch( match.getType() ) {
                        case 'url' :
                            console.log( "url: ", match.getUrl() );

                            return true;  // let Autolinker perform its normal anchor tag replacement

                        case 'email' :
                            var email = match.getEmail();
                            console.log( "email: ", email );

                            if( email === "[email protected]" ) {
                                return false;  // don't auto-link this particular email address; leave as-is
                            } else {
                                return;  // no return value will have Autolinker perform its normal anchor tag replacement (same as returning `true`)
                            }

                        case 'phone' :
                            console.log( "Phone Number: ", match.getNumber() );

                            return '<a href="http://newplace.to.link.phone.numbers.to/">' + match.getNumber() + '</a>';

                        case 'mention' :
                            console.log( "Mention: ", match.getMention() );
                            console.log( "Mention Service Name: ", match.getServiceName() );

                            return '<a href="http://newplace.to.link.mention.handles.to/">' + match.getMention() + '</a>';

                        case 'hashtag' :
                            console.log( "Hashtag: ", match.getHashtag() );

                            return '<a href="http://newplace.to.link.hashtag.handles.to/">' + match.getHashtag() + '</a>';
                    }
                }
            });
            return linkedText;

But the console.logs below never fire. Any ideas pls?

 console.log( "href = ", match.getAnchorHref() );
                    console.log( "text = ", match.getAnchorText() );
                    console.log("INPUTTEXT:", inputText, "MATCH TYPE:", match.getType(), match )

@olafleur
Copy link
Collaborator

I would recommend opening another issue to not get your message lost. :)

@adamgins
Copy link

@olafleur thanks. created #199

@shubhdhawan
Copy link

@gregjacobs great package. I have been using in default state in production for ages. Thanks.
I wanted to customize some of the mentions, I am having a similar issues to this issue of getting the replaceFn to fire.

I have initialized the autolinker with

var Autolinker = require( 'autolinker' );
var autolinker = new Autolinker( {
    urls : {
        schemeMatches : true,
        wwwMatches    : true,
        tldMatches    : true
    },
    email       : true,
    phone       : true,
    mention     : 'twitter',
    hashtag     : 'twitter',

    stripPrefix : true,
    newWindow   : true,

    truncate : {
        length   : 0,
        location : 'end'
    },

    className : ''
} );

then I call it like so

var linkedText = autolinker.link( inputText, {
                replaceFn : function( match ) {
                    console.log( "href = ", match.getAnchorHref() );
                    console.log( "text = ", match.getAnchorText() );
                    console.log("INPUTTEXT:", inputText, "MATCH TYPE:", match.getType(), match )

                    switch( match.getType() ) {
                        case 'url' :
                            console.log( "url: ", match.getUrl() );

                            return true;  // let Autolinker perform its normal anchor tag replacement

                        case 'email' :
                            var email = match.getEmail();
                            console.log( "email: ", email );

                            if( email === "[email protected]" ) {
                                return false;  // don't auto-link this particular email address; leave as-is
                            } else {
                                return;  // no return value will have Autolinker perform its normal anchor tag replacement (same as returning `true`)
                            }

                        case 'phone' :
                            console.log( "Phone Number: ", match.getNumber() );

                            return '<a href="http://newplace.to.link.phone.numbers.to/">' + match.getNumber() + '</a>';

                        case 'mention' :
                            console.log( "Mention: ", match.getMention() );
                            console.log( "Mention Service Name: ", match.getServiceName() );

                            return '<a href="http://newplace.to.link.mention.handles.to/">' + match.getMention() + '</a>';

                        case 'hashtag' :
                            console.log( "Hashtag: ", match.getHashtag() );

                            return '<a href="http://newplace.to.link.hashtag.handles.to/">' + match.getHashtag() + '</a>';
                    }
                }
            });
            return linkedText;

But the console.logs below never fire. Any ideas pls?

 console.log( "href = ", match.getAnchorHref() );
                    console.log( "text = ", match.getAnchorText() );
                    console.log("INPUTTEXT:", inputText, "MATCH TYPE:", match.getType(), match )

I am having the same problem how did you resolved this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants